简体   繁体   中英

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.

I managed to do that with the pages but I am trying to copy also all metadata, form fields, acrofields etc.

How will I be able to do that using 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. You can make identical copies with System.IO.File.Copy.

You are then free to open it with iTextSharp to make further adjustments to the copy.

You use a PdfCopy based solution.

For your task, though, ie to take a single PDF and apply some changes to it, the appropriate solution is PdfStamper based. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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