I have PDF files that have to be merge into one. This is not a issue if I have all the PDF files at one time. However the PDF files come in in stages and because of the workflow we need to merge them as they arrive.
So the workflow looks like this:
1: If no PDF file exist create one and merge the first set of pages into the new PDF file then close.
2: If a PDF already exist(target one) open it up and merge the then new pages into the target PDF files. then close
3: repeat.
Below is the code I have but it just overwrites the pages that were previously inserted, so my question is how do I insert PDF files into an already existing PDF file using PDFSmartCopy.
I need to use PDFSmartCopy because I need to optimized the fonts. I found this nice document on StackOverflow that shows how to how to append but they are using different techniques other than PDFSmartCopy.
Note: I'm not sure if I need to create a intermediate PDF file to hold the target PDF pages then delete the target PDF and then save the intermediate PDF as the target PDF. However, before I go and do that, I was wondering is there was a way to do it without the intermediate step.
using (FileStream stream = new FileStream(targetFile, FileMode.OpenOrCreate))
{
Document pdfDoc = new Document(PageSize.LETTER);
PdfSmartCopy pdf = new PdfSmartCopy(pdfDoc, stream);
pdfDoc.Open();
foreach (string file in files)
{
PdfReader reader = new PdfReader(file);
pdf.AddDocument(reader);
pdf.FreeReader(reader) ;
reader.Close();
}
}