合并两个PdfDocument对象 [英] Merging two PdfDocument objects

查看:182
本文介绍了合并两个PdfDocument对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,如何使用itext7 .net库合并两个PdfDocument对象?

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

还尝试将嵌套的pdf写入磁盘--->读取->另一个异常

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

任何想法!

推荐答案

首先:您的代码

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

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

起作用!!它不会引发任何异常,而是不会将任何内容复制到GlobalPdfDocument.这是正确的,因为InitialPdfDoc是新创建的,尚不包含任何页面.

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.

但是让我们假设您根本没有提供足够的代码来重现此问题,假设您的实际代码在调用CopyPagesTo方法之前为InitialPdfDoc添加了一些内容,例如

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.

该异常清楚地说明了代码的问题:您的文档InitialPdfDoc被写入(即它具有关联的PdfWriter),因此无法从中复制页面.

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.

这种无法从写入的文档复制页面的限制是由于iText体系结构引起的:写入文档时,iText尝试尽快将新内容推送到PdfWriter输出流中然后忘记它.这样一来,iText即可轻松生成大型结果PDF,而无需占用大量内存.缺点是您面临的限制.

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.

因此,一个明显的解决方案是完成创建要复制的PDF,在没有编写器的情况下将其读入文档对象,然后从那里复制:

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);

这篇关于合并两个PdfDocument对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆