-1

我正在尝试通过使用 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();
        }
    }

任何想法?

谢谢

4

3 回答 3

3

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

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

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

您使用 PdfWriter 的事实表明您没有阅读文档

此外,您的问题听起来好像您不知道 Lowagie 是一个人的名字。实际上,这是我的名字,当有人说Lowagie iText API not working时很尴尬。首先,因为我一直要求停止使用那些旧版本的 iText,还因为这听起来像是个人指责,将产品与人类混淆了。请参阅lowagie 和 iText 有什么区别?

于 2013-05-03T08:46:57.490 回答
2

请使用 iText 的 Android 端口:

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

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

于 2013-05-03T07:48:20.520 回答
1

下面是合并两个 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(); 
    }
于 2013-09-05T06:14:45.047 回答