How do I efficiently merge two (or more) PDFs?

44 views
Skip to first unread message

Support

unread,
Oct 10, 2008, 6:21:53 PM10/10/08
to PDFTron PDFNet SDK
Q: Is it normal for a merged 110 page document to be 60 times larger
than
if I create the document from the 110 page original? I am merging the
PDF's and it comes out a whole lot larger(120MB vs 2MB) than if I take
the individual originals(html) and create the PDF. I'm using
PDFDoc.ImportPages and PDFDoc.PagePushBack. My code looks as follows:

PDFNet.Initialize();
Boolean found = PDFNet.SetResourcesPath(resource_path);

ArrayList pageList = new ArrayList();
foreach (PrintDocument document in list) {
PDFDoc pdf;
if (!File.Exists(pdfLocation)) pdf = new PDFDoc();
else pdf = new PDFDoc(pdfLocation);

PDFDoc pdfdoc = new PDFDoc(document.PDF, document.PDF.Length);
for (PageIterator itr = pdfdoc.GetPageIterator(); itr.HasNext();
itr.Next())
pageList.Add(itr.Current());

pdf.ImportPages(pageList);
for (int i = 0; i != pageList.Count; ++i)
pdf.PagePushBack((Page)pageList[i]);

pdf.Save(pdfLocation, pdftron.SDF.SDFDoc.SaveOptions.e_incremental);


---------------
A: It is unusual to have this type of file increase. If you are
merging a 10 MB PDF with a target PDF the file size of the merged
document should not increase more than 10 MB.

The problem is that although you imported pages you are still
inserting pages from the original PDF instead of from the imported
page set. The following is correct snippet shows how to properly merge
pages from two PDFs.

PDFDoc in_doc = new PDFDoc("in.pdf");
in_doc.InitSecurityHandler();

ArrayList copy_pages = new ArrayList();
for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext();
itr.Next()) copy_pages.Add(itr.Current());

PDFDoc doc = new PDFDoc("target.pdf");
doc.InitSecurityHandler();
ArrayList imported_pages = doc.ImportPages(copy_pages);
for (int i=0; i!=imported_pages.Count; ++i)
doc.PagePushBack((Page)imported_pages[i]);

doc.Save("merged.pdf", pdftron.SDF.SDFDoc.SaveOptions.e_linearized);
doc.Close();
in_doc.Close();
Reply all
Reply to author
Forward
0 new messages