Merging two PdfDocument objects

风流意气都作罢 提交于 2021-01-28 09:02:21

问题


Simple question , how to merge two PdfDocument objects using itext7 .net Library ?

this is how am trying but it's not working

PdfDocument GlobalPdfDocument = new PdfDocument(new PdfWriter(multiContentPdf));

PdfDocument InitialPdfDoc = new PdfDocument(new PdfWriter(memStreamOfAddressAndBarcode));
InitialPdfDoc.CopyPagesTo(1, InitialPdfDoc.GetNumberOfPages(), GlobalPdfDocument);

also this code throw same exception

Merger.Merge(InitialPdfDoc,1,InitialPdfDoc.GetNumberOfPages());

Cannot copy indirect object from the document that is being written

tried also to write the nested pdf to the disk ---> read --> another exception

any ideas !!


回答1:


First of all: Your code

PdfDocument GlobalPdfDocument = new PdfDocument(new PdfWriter(multiContentPdf));

PdfDocument InitialPdfDoc = new PdfDocument(new PdfWriter(memStreamOfAddressAndBarcode));
InitialPdfDoc.CopyPagesTo(1, InitialPdfDoc.GetNumberOfPages(), GlobalPdfDocument);

does work! It does not throw any exception but instead copies nothing to the GlobalPdfDocument. Which is correct because InitialPdfDoc is freshly created and does not contain any pages yet.

But let's assume you simply did not supply enough code to reproduce the issue, let's assume your actual code adds some content to InitialPdfDoc before calling that CopyPagesTo method, e.g.

PdfDocument GlobalPdfDocument = new PdfDocument(new PdfWriter(multiContentPdf));

PdfDocument InitialPdfDoc = new PdfDocument(new PdfWriter(memStreamOfAddressAndBarcode));
InitialPdfDoc.AddNewPage();
InitialPdfDoc.CopyPagesTo(1, InitialPdfDoc.GetNumberOfPages(), GlobalPdfDocument);

Now there indeed is the claimed exception:

iText.Kernel.PdfException : Cannot copy indirect object from the document that is being written.

The exception clearly states what is wrong with the code: Your document InitialPdfDoc is written to (i.e. it has an associated PdfWriter), and therefore pages cannot be copied from it.

This restriction that pages cannot be copied from documents written to is due to the iText architecture: When a document is written to, iText attempts to push this new content out into the PdfWriter output stream as soon as possible and then forget about it. This allows iText to easily produce large result PDFs without requiring a large amount of memory. The downside is the restriction you're confronted with.

Thus, the obvious solution is to finish creating the PDF to copy from, reading it into a document object without a writer, and copying from there:

PdfDocument GlobalPdfDocument = new PdfDocument(new PdfWriter(multiContentPdf));

PdfWriter Writer = new PdfWriter(memStreamOfAddressAndBarcode);
Writer.SetCloseStream(false);                     // Prevent the Writer from closing the MemoryStream
PdfDocument InitialPdfDoc = new PdfDocument(Writer);
InitialPdfDoc.AddNewPage();
InitialPdfDoc.Close();                            // Closing the document finishes the result PDF

memStreamOfAddressAndBarcode.Position = 0;        // Re-position the stream to the start of the PDF
InitialPdfDoc = new PdfDocument(new PdfReader(memStreamOfAddressAndBarcode)); // Create r/o document

InitialPdfDoc.CopyPagesTo(1, InitialPdfDoc.GetNumberOfPages(), GlobalPdfDocument);


来源:https://stackoverflow.com/questions/53783148/merging-two-pdfdocument-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!