簡體   English   中英

使用iTextSharp制作兩個相同的PDF

[英]Making two identical PDFs using iTextSharp

我想克隆一個pdf,並在復制期間或之后的某個時刻對文檔稍作更改。

我設法用頁面做了,但我也試圖復制所有元數據,表單字段,acrofields等。

我怎么能用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);

您不能使用iTextSharp制作相同字節的副本。 您可以使用System.IO.File.Copy制作相同的副本。

然后,您可以使用iTextSharp自由打開它以進一步調整副本。

您使用基於PdfCopy的解決方案。

但是,對於您的任務,即采用單個PDF並對其應用一些更改,適當的解決方案是基於PdfStamper 這看起來像這樣:

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