Merge PDFs
How to Combine PDF Files
Correctly merging PDFs means removing duplicate resources – fonts, images, metadata, content, links and annotations, for example – to keep file sizes to a minimum. Adobe PDF Library automates the process of selecting information from a variety of sources to create PDFs with customized content.
Get Free Trial
C++
C#
Java
C++
APDFLDoc doc1(csInputFileName1.c_str(), true);
APDFLDoc doc2(csInputFileName2.c_str(), true);
ASInt32 InsertFlags = PDInsertBookmarks | PDInsertDoNotMergeFonts | PDInsertDoNotResolveInvalidStructureParentReferences | PDInsertDoNotRemovePageInheritance;
PDDocInsertPages(doc1.getPDDoc(), PDLastPage, doc2.getPDDoc(), 0, PDAllPages, InsertFlags, NULL, NULL, NULL, NULL);
ASInt32 saveFlags = PDSaveFull | PDSaveCollectGarbage;
ASInt32 saveFlags2 = PDSaveCompressed| PDSaveAddFlate; //different enums of saveFlags, don't mix.
doc1.saveDoc(csOutputFileName.c_str(), saveFlags, saveFlags2);
C#
using (Document doc1 = new Document(sInput1))
{
using (Document doc2 = new Document(sInput2))
{
try
{
doc1.InsertPages(Document.LastPage, doc2, 0, Document.AllPages, PageInsertFlags.Bookmarks |
// For best performance processing large documents, set the following flags.
PageInsertFlags.DoNotMergeFonts | PageInsertFlags.DoNotResolveInvalidStructureParentReferences | PageInsertFlags.DoNotRemovePageInheritance);
}
catch(LibraryException ex)
{
if (!ex.Message.Contains("An incorrect structure tree was found in the PDF file but operation continued"))
{
throw ex;
}
}
// For best performance processing large documents, set the following flags.
doc1.Save(SaveFlags.Full | SaveFlags.SaveLinearizedNoOptimizeFonts | SaveFlags.Compressed, sOutput);
}
}
Java
Document doc1 = new Document(sInput1);
Document doc2 = new Document(sInput2);
try
{
doc1.insertPages(Document.LAST_PAGE, doc2, 0, Document.ALL_PAGES, EnumSet.of(PageInsertFlags.BOOKMARKS,
// For best performance processing large documents, set the following flags.
PageInsertFlags.DO_NOT_MERGE_FONTS, PageInsertFlags.DO_NOT_RESOLVE_INVALID_STRUCTURE_PARENT_REFERENCES, PageInsertFlags.DO_NOT_REMOVE_PAGE_INHERITANCE));
}
catch(LibraryException ex)
{
if (!ex.getMessage().contains("An incorrect structure tree was found in the PDF file but operation continued"))
{
throw ex;
}
System.out.println(ex.getMessage());
}
// For best performance processing large documents, set the following flags.
doc1.save(EnumSet.of(SaveFlags.FULL, SaveFlags.SAVE_LINEARIZED_NO_OPTIMIZE_FONTS, SaveFlags.COMPRESSED), sOutput);