Create two identical PDF files using iTextSharp

I want to clone a PDF file and make some changes to the document at some point during or after copying.

I managed to do this with pages, but I'm trying to copy also all metadata, form fields, acrofields, etc.

How can I do this with 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);
+5
source share
2 answers

You cannot make copies with the same byte using iTextSharp. You can make identical copies using System.IO.File.Copy.

You can then open it with iTextSharp to make additional adjustments to the copy.

0
source

You are using a solution based on 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();
0

All Articles