I\'ve scoured the Web looking for examples on how to do this. I\'ve found a few that seem to be a little more involved then they need to be. So my question is, using iTextShar
I really may be missing something, but I did something much simpler. I concede this solution probably won't update bookmarks (as in the best answer here so far), but it works flawlessly for me. Since I was merging documents with fillable forms, I used PdfCopyFields instead of PdfCopy.
Here is the code (I've stripped all error handling to make the actual code more visible, add a try..finally to close opened resources if you plan on using the code):
void MergePdfStreams(List Source, Stream Dest)
{
PdfCopyFields copy = new PdfCopyFields(Dest);
foreach (Stream source in Source)
{
PdfReader reader = new PdfReader(source);
copy.AddDocument(reader);
}
copy.Close();
}
You can pass any stream, be it a FileStream, a MemoryStream (useful when reading the PDF from databases, no need for temporary files, etc.)
Sample usage:
void TestMergePdfStreams()
{
List sources = new List()
{
new FileStream("template1.pdf", FileMode.Open),
new FileStream("template2.pdf", FileMode.Open),
new MemoryStream((byte[])someDataRow["PDF_COLUMN_NAME"])
};
MergePdfStreams(sources, new FileStream("MergedOutput.pdf", FileMode.Create));
}