简体   繁体   English

使用iTextSharp制作两个相同的PDF

[英]Making two identical PDFs using iTextSharp

I want to clone a pdf, and make slight changes to the document at some point during or after copying. 我想克隆一个pdf,并在复制期间或之后的某个时刻对文档稍作更改。

I managed to do that with the pages but I am trying to copy also all metadata, form fields, acrofields etc. 我设法用页面做了,但我也试图复制所有元数据,表单字段,acrofields等。

How will I be able to do that using iTextSharp ? 我怎么能用iTextSharp做到这一点?

Document document = new Document(); 
FileStream fs = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
PdfCopy copy = new PdfCopy(document, fs);
document.Open();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
    PdfImportedPage importedPage = copy.GetImportedPage(reader, i);
    copy.AddPage(importedPage);
}
copy.Outlines = SimpleBookmark.GetBookmark(reader);                

fs.Flush();

PdfCopyFields copyf = new PdfCopyFields(fs);

You cannot make identical-byte copies with iTextSharp. 您不能使用iTextSharp制作相同字节的副本。 You can make identical copies with System.IO.File.Copy. 您可以使用System.IO.File.Copy制作相同的副本。

You are then free to open it with iTextSharp to make further adjustments to the copy. 然后,您可以使用iTextSharp自由打开它以进一步调整副本。

You use a PdfCopy based solution. 您使用基于PdfCopy的解决方案。

For your task, though, ie to take a single PDF and apply some changes to it, the appropriate solution is PdfStamper based. 但是,对于您的任务,即采用单个PDF并对其应用一些更改,适当的解决方案是基于PdfStamper That would look like this: 这看起来像这样:

PdfReader reader = ...;
[...apply changes using PdfReader methods...]
FileStream fs = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
PdfStamper stamper = new PdfStamper(reader, fs);
[...apply changes using PdfStamper methods...]
stamper.Close();

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

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