【问题标题】:How to convert PDF to JPG with PDFTron如何使用 PDFTron 将 PDF 转换为 JPG
【发布时间】:2016-05-03 03:03:47
【问题描述】:

我的目标是将可填写的 pdf 文档转换为图像,然后将其保存到我在 Kumulos 上的数据库中。我在使用 PDFTron 转换 pdf 文档时遇到问题。我得到的错误是该文件不存在。但是,我可以提取文件并在应用程序中查看它。

switch (Global.g){
        case 1:
            InputStream is = res.openRawResource(R.raw.incident_report);
            try{
                doc = new PDFDoc(is);
            }catch (PDFNetException e){
                doc = null;
                e.printStackTrace();
            }catch (IOException e){
                doc = null;
                e.printStackTrace();
            }
            try {
                mPDFViewCtrl.setDoc(doc);
                save_Button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try{
                            String output_path = "../../raw/Output/";

                            PDFDraw draw=new PDFDraw();  // PDFDraw class is used to rasterize PDF pages.
                            ObjSet hint_set=new ObjSet();

                            PDFDoc doc=new PDFDoc((mPDFViewCtrl + "incident_report.pdf"));
                            // Initialize the security handler, in case the PDF is encrypted.
                            doc.initSecurityHandler();

                            draw.setDPI(72); // Set the output resolution is to 72 DPI.

                            // Use optional encoder parameter to specify JPEG quality.
                            Obj encoder_param=hint_set.createDict();
                            encoder_param.putNumber("Quality", 80);

                            // Traverse all pages in the document.
                            for (PageIterator itr=doc.getPageIterator(); itr.hasNext();) {
                                Page current=(Page)(itr.next());
                                String filename=output_path+"incident_report"+current.getIndex() + ".jpg";
                                System.out.println(filename);
                                draw.export(current, filename, "JPEG", encoder_param);
                            }

                            doc.close();

                        }catch(PDFNetException e){
                            e.printStackTrace();
                        }
                        finish();
                    }
                });

                cancel_Button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            } catch (PDFNetException e) {
                e.printStackTrace();
            }
            break;

【问题讨论】:

    标签: android pdftron


    【解决方案1】:

    以下行看起来不正确。

    PDFDoc doc=new PDFDoc((mPDFViewCtrl + "incident_report.pdf"));
    

    应该是

    PDFDoc doc=mPDFViewCtrl.getDoc();
    

    这应该可以解决您无法阅读的错误。

    查看您的代码虽然还有许多其他问题。

    1. 不要在第一个 PDFDoc 对象上调用 initSecurityHandler
    2. 您应该获得一个读锁,因为您正在访问 PDFViewCtrl 类之外的文档(它不知道您正在这样做)。在这种特殊情况下,您可能会不用,因为您没有修改文档,但最好获得读取锁定。

    这是修改后的代码

    InputStream is = res.openRawResource(R.raw.incident_report);
    try{
        doc = new PDFDoc(is);
        doc.initSecurityHandler(); // handle passwordless but encrypted files
    }catch (PDFNetException e){
        doc = null;
        e.printStackTrace();
    }catch (IOException e){
        doc = null;
        e.printStackTrace();
    }
    try {
        mPDFViewCtrl.setDoc(doc);
        save_Button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                try{
                    String output_path = "../../raw/Output/";
                    PDFDraw draw=new PDFDraw();
                    draw.setDPI(72);
                    ObjSet hint_set=new ObjSet();
                    Obj encoder_param=hint_set.createDict();
                    encoder_param.putNumber("Quality", 80);
    
                    // Get PDF being viewed and get read lock
                    PDFDoc doc=mPDFViewCtrl.getDoc();
                    mPDFViewCtrl.docLockRead();
    
                    for (PageIterator itr=doc.getPageIterator(); itr.hasNext();) {
                        Page current=(Page)(itr.next());
                        String filename=output_path+"incident_report"+current.getIndex() + ".jpg";
                        System.out.println(filename);
                        draw.export(current, filename, "JPEG", encoder_param);
                    }
                }catch(PDFNetException e){
                    e.printStackTrace();
                }finally {
                    mPDFViewCtrl.docUnlockRead(); // release read lock
                }
                finish();
            }
        });
    
        cancel_Button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    } catch (PDFNetException e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 嗯仍然出现错误。我认为它是我的 output_path I/System.out: ../../raw/Output/incident_report1.jpg intelwatch W/System.err: Exception: intelwatch W/System.err: Message: No such file or directory intelwatch W/ System.err:条件表达式:m_stream != NULL
    • 如果您认为 PDFDraw.export 在访问设备上的文件夹时遇到问题,那么您可以使用返回 android.graphics.Bitmap 对象的 PDFDraw.getBitmap(Page)。然后,您可以根据需要使用它来写入磁盘。如果这有效,但 PDFDraw.export 中的相同路径无效,请向 PDFTron 支持提交工单。
    猜你喜欢
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2012-01-27
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多