繁体   English   中英

Android:使用iText API合并PDF文件不起作用

[英]Android : Merge PDFs file using iText API not working

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

任何想法?

谢谢

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

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

之前已经对此进行了多次解释:

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

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

请使用iText的Android端口:

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

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

以下是合并两个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(); 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM