Q: How do I merge PDFs using PDFTron PDFNet?
I found pdfdoc.ImportPages() and pdfdoc.PagePushBack(), but I am not
sure how to use them.
-------------------
A: As part of the next PDFNet update we will add a new utility
function to simplify PDF merging and splitting (i.e.
pdfdoc.Merge(PDFDoc), and pdfdoc.Split()). You can achieve the same
with the current version as shown below:
Here is a sample C# utility function to merge two PDF documents:
static void MergePDF(PDFDoc new_doc, string filename)
{
using (PDFDoc in_doc = new PDFDoc(filename)) {
in_doc.InitSecurityHandler();
ArrayList copy_pages = new ArrayList();
for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext();
itr.Next()){
copy_pages.Add(itr.Current());
}
ArrayList imported_pages = new_doc.ImportPages(copy_pages);
for (int i=0; i!=imported_pages.Count; ++i) {
new_doc.PagePushBack((Page)imported_pages[i]); // Order pages in
reverse order.
}
}
The following snippet shows now to merge multiple PDF documents:
PDFNet.Initialize();
using (PDFDoc new_doc = new PDFDoc()) {
MergePDF(new_doc, @"c:\mypdfs\1.pdf");
MergePDF(new_doc, @"c:\mypdfs\2.pdf");
MergePDF(new_doc, @"c:\mypdfs\3.pdf");
...
new_doc.Save("merged.pdf", , SDFDoc.SaveOptions.e_linearized);
}
For a working sample please take a look at the last code snippet (6)
in PDFPage sample:
http://www.pdftron.com/pdfnet/samplecode.html#PDFPage