【问题标题】:Android : Merge PDFs file using iText API not workingAndroid:使用 iText API 合并 PDF 文件不起作用
【发布时间】:2013-04-27 12:42:29
【问题描述】:

我正在尝试通过使用 iText API 将两个或多个 PDF 文档合并为一个来合并 pdf 文件。但结果我得到了 0 字节大小的合并 pdf。我发布了我的代码,如下所示。我尝试使用 iText。 jar 文件,但给出相同的 0 大小的 pdf。

得到了这个:-"找不到类 'com.itextpdf.text.pdf.PdfPrinterGraphics2D',引用自方法 com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes”。 我仍然没有取得任何成功。

代码:

public class ItextMerge {
        public static void main() {
            List<InputStream> list = new ArrayList<InputStream>();
            try {
                // Source pdfs
                list.add(new FileInputStream(new File("mnt/sdcard/nocturia.pdf")));
                list.add(new FileInputStream(new File("mnt/sdcard/Professional Android Application Development.pdf")));

                // Resulting pdf
                OutputStream out = new FileOutputStream(new File("mnt/sdcard/newmerge.pdf"));

                doMerge(list, out);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * Merge multiple pdf into one pdf
         * 
         * @param list
         *            of pdf input stream
         * @param outputStream
         *            output file output stream
         * @throws DocumentException
         * @throws IOException
         */
        public static void doMerge(List<InputStream> list, OutputStream outputStream)
                throws DocumentException, IOException {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            PdfContentByte cb = writer.getDirectContent();

            for (InputStream in : list) {
                PdfReader reader = new PdfReader(in);
                for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                    document.newPage();
                    //import the page from source pdf
                    PdfImportedPage page = writer.getImportedPage(reader, i);
                    //add the page to the destination pdf
    //                cb.addTemplate(page, 0, 0);
    //                cb.addTemplate(page, 0, 0);
                }
            }

            outputStream.flush();
            document.close();
            outputStream.close();
        }
    }

有什么想法吗?

谢谢

【问题讨论】:

    标签: java android pdf itext


    【解决方案1】:

    我赞成迈克尔的回答,因为它是您问题的正确答案。

    但是,在阅读您的代码时,您还没有意识到另一个问题:您使用了错误的代码来合并 PDF。你应该使用PdfCopyPdfSmartCopy,而不是PdfWriter

    这个之前已经解释过很多次了:

    您使用 PdfWriter 的事实表明您没有阅读 the documentation

    此外,您的问题听起来好像您不知道 Lowagie 是一个人的名字。实际上,这是我的名字,当有人说,Lowagie iText API 不工作时,这很尴尬。首先,因为我一直在向 iText 的stop using those old versions 提问,但也因为这听起来像是个人指控,将产品与人类混淆了。见What is the difference between lowagie and iText?

    【讨论】:

    • 收到此错误com.itextpdf.license.LicenseKeyException:未加载许可证文件。并生成 0size 的新 pdf 文件
    • 我从问题中删除了你的名字并替换了 PdfSmartCopy 但仍然没有成功。
    • 不要将错误归咎于 PdfSmartCopy。异常告诉您需要获取许可证文件:demo.itextsupport.com/newslicense,并且您需要将其加载到您的代码中:LicenseKey.loadLicenseFile(path_to_licensekey);其中 path_to_licensekey 是您存储许可证密钥的路径。
    【解决方案2】:

    请使用iText的Android版:

    http://repo.itextsupport.com//android_gae/com/itextpdf/itextgoogle/

    您需要试用许可证才能在 Android 上使用 iText; http://demo.itextsupport.com/newslicense/

    【讨论】:

    • 当我使用 itextgoogle-5.4.0.jar 时,它给了我错误“Caused by: java.lang.NoClassDefFoundError: org.spongycastle.crypto.engines.AESFastEngine”
    • 收到此错误 com.itextpdf.license.LicenseKeyException:未加载许可证文件。并生成 0size 的新 pdf 文件
    • 我在 libs 文件夹中添加了 sc-light-jdk15on-1.47.0.2-sources.jar 但仍然没有成功
    • 您需要添加实际编译的海绵堡,而不是源代码 jar,这些对任何应用程序都没有多大作用。要加载您需要包含的许可证文件,LicenseKey.loadLicenseFile(String path)
    【解决方案3】:

    以下是合并两个pdf文件的简单代码。

        try{
            String doc1 = FOLDER_PATH + "Doc1.pdf";
            String doc2 = FOLDER_PATH + "Doc2.pdf";
            String resultDocFile = FOLDER_PATH + "ResultDoc.pdf";
    
            PdfReader reader1 = new PdfReader(doc1);
            Document resultDoc = new Document();
            PdfCopy copy = new PdfCopy(resultDoc, new FileOutputStream(resultDocFile));
            resultDoc.open();
            //Copying First Document
            for(int i = 1; i <= reader1.getNumberOfPages(); i++)    {
                copy.addPage(copy.getImportedPage(reader1, i));
            }
            PdfReader reader2 = new PdfReader(doc2);
            //Copying Second Document
            for(int i = 1; i <= reader2.getNumberOfPages(); i++)    {
                copy.addPage(copy.getImportedPage(reader2, i));
            }
            resultDoc.close();
        } catch (Exception e){
            e.printStackTrace(); 
        }
    

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2014-10-25
      • 2015-12-23
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多