简体   繁体   English

itext 7 如何合并PDF?

[英]itext 7 how to merge Pdfs?

I have a Problem and need your help.我有一个问题,需要你的帮助。 I'm running itext 7.0.2.我正在运行 itext 7.0.2。 java and i'm trying to merge a list ArrayList of type PdfDocument to one single pdf. java,我正在尝试将 PdfDocument 类型的列表 ArrayList 合并到一个 pdf 中。

I tried the following:我尝试了以下方法:

        ByteArrayOutputStream bosGes = new ByteArrayOutputStream();
        PdfWriter writerGes = new PdfWriter(bosGes);
        PdfDocument docGes = new PdfDocument(writerGes);
        ArrayList<PdfDocument> pdfs = AListOfSomePreviousCreatedPdfDocuments;

        for (int i=0; i < pdfs.size(); i++) {
            PdfDocument d = pdfs.get(i);
            PdfPage p = d.getFirstPage().copyTo(docGes);
            docGes.addPage(p);
            d.close();
        }

Resulting in this error:导致这个错误:

com.itextpdf.kernel.PdfException: Cannot copy indirect object from the document that is being written. com.itextpdf.kernel.PdfException:无法从正在写入的文档中复制间接对象。

Then I tried the following:然后我尝试了以下方法:

        ByteArrayOutputStream bosGes = new ByteArrayOutputStream();
        PdfWriter writerGes = new PdfWriter(bosGes);
        PdfDocument docGes = new PdfDocument(writerGes);

        for (int i=0; i < pdfs.size(); i++) {
            PdfDocument d = pdfs.get(i);
            docGes.addPage(d.getFirstPage());
            d.close();
        }

com.itextpdf.kernel.PdfException: Page com.itextpdf.kernel.pdf.PdfPage@1d85315f cannot be added to document com.itextpdf.kernel.pdf.PdfDocument@32ae81bb, because it belongs to document com.itextpdf.kernel.pdf.PdfDocument@4b8d06af. com.itextpdf.kernel.PdfException:页面 com.itextpdf.kernel.pdf.PdfPage@1d85315f 无法添加到文档 com.itextpdf.kernel.pdf.PdfDocument@32ae81bb,因为它属于文档 com.itextpdf.kernel.pdf。 PdfDocument@4b8d06af。

What I have to do, to merge this PdfDocuments (each one has only one page) to one single Pdf?我必须做什么,将这个 PdfDocuments(每个只有一页)合并到一个 Pdf?


OK, I'm going nuts.好吧,我要疯了。 Explaining it from the beginning.从头开始解释。

I create pdf-forms like this我创建这样的 pdf 表单

        ArrayList<PdfDocument> pdfs = new ArrayList<PdfDocument>();
        while (rs.next()) {
            URL fullPath = context.getResource("/formulare/kontrollbogen.pdf");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PdfWriter writer = new PdfWriter(bos);
            PdfReader reader = new PdfReader(fullPath.getFile());
            PdfDocument doc = new PdfDocument(reader, writer);
            PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);

            String firma = rs.getString("firma");
            if (firma != null && !firma.equals("")) {
                form.getField("firma").setValue(firma);
            }
                .
                .
                .
            pdfs.add(doc);
       }

Then I want to merge this PdfDocuments in my ArrayList like this:然后我想像这样在我的 ArrayList 中合并这个 PdfDocuments:

        ByteArrayOutputStream bosGes = new ByteArrayOutputStream();
        PdfWriter writerGes = new PdfWriter(bosGes);
        PdfDocument resultDoc = new PdfDocument(writerGes);

        for (int i=0; i < pdfs.size(); i++) {
            PdfReader r = pdfs.get(i).getReader();
            PdfDocument sourceDoc1 = new PdfDocument(r);
            int n1 = sourceDoc1.getNumberOfPages();             

            for (int j = 1; j <= n1; j++) {
                PdfPage page = sourceDoc1.getPage(j).copyTo(resultDoc);
                resultDoc.addPage(page);
            }
            sourceDoc1.close();
        }

        resultDoc.close();

Doesn't work.不起作用。 Result is a pdf with more pages but forms are not filled out.结果是包含更多页的 pdf,但未填写表格。

So I tried RTFM and tried Bruno's example with merging flattened forms but result is always a pdf with empty forms.所以我尝试了 RTFM 并尝试了布鲁诺的示例合并扁平表单,但结果始终是带有空表单的 pdf。

Can anyone help to convert the pdf-forms (all the same form) in my ArrayList to one single pdf?任何人都可以帮助将我的 ArrayList 中的 pdf 表单(所有相同的表单)转换为一个 pdf 吗?


I tried Bruno's tip but it doesn't work.我试过布鲁诺的提示,但没有用。 What I did is:我所做的是:

        //Initialize PDF document with output intent
        PdfDocument pdf = new PdfDocument(writerGes);
        PdfMerger merger = new PdfMerger(pdf);
        //Add pages from the first document
        PdfDocument firstSourcePdf = pdfs.get(0);
        merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
        //Add pages from the second pdf document
        PdfDocument secondSourcePdf = pdfs.get(1);
        merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
        firstSourcePdf.close();
        secondSourcePdf.close();
        pdf.close();

but it ends up with this error again:但它最终再次出现此错误:

com.itextpdf.kernel.PdfException: Cannot copy indirect object from the document that is being written. com.itextpdf.kernel.PdfException:无法从正在写入的文档中复制间接对象。

Is this perhaps because my pdf's are forms and I have to reset the values when merging?这可能是因为我的 pdf 是表格,合并时我必须重置值?

this is how I merged 2 pdf documents together这就是我将 2 个 pdf 文档合并在一起的方式

if (1 <= listOfFiles.Count)
                    {

                        masterSrc = subPath + "/" + date + " - " + dateEnd + ".pdf";

                        PdfDocument merged = new PdfDocument(new PdfWriter(Server.MapPath(masterSrc)));
                        
                        for (int a = 0; a < listOfFiles.Count; a++)
                        {
                            PdfDocument origPdf2 = new PdfDocument(new PdfReader(Server.MapPath(listOfFiles[a])));
                            PdfMerger merger = new PdfMerger(merged);
                            merger.Merge(origPdf2, 1, origPdf2.GetNumberOfPages());

                        }
                        merged.Close();
                    }

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

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