Merge PDF files together programmatically
Foxit Quick PDF Library supports the merging of two or more PDF documents into one PDF document. The primary functions available for this task are MergeDocument, MergeFileList and MergeFileListFast. The function that you use for merging will depend on what your exact requirements are.
Merge a list of PDF files
If you want to merge a list (two or more) of PDF files together then the MergeFileList function is what you need. This function Merges all the files in a named file list and saves the resulting merged document to the specified file.
Outlines (bookmarks), form fields and annotations from all the documents will be present in the merged document. The file list can be cleared using the ClearFileList function. Depending on the content and size of the documents that you are merging, the MergeFileListFast function can be used to obtain faster merging speeds.
/* Combine a list of PDFs together quickly */ // Add a range of files to a list. DPL.AddToFileList("FilesToMerge", "sample1.pdf"); DPL.AddToFileList("FilesToMerge", "sample2.pdf"); DPL.AddToFileList("FilesToMerge", "sample3.pdf"); // Merge the list of files together. DPL.MergeFileList("FilesToMerge", "merged_file_list.pdf");
Combine two PDFs together
If you want to merge two PDF files together then the MergeDocument function can be used. This function allows you to join one PDF document to another PDF document. During the merging process any form fields and annotations from the second document are preserved but outlines (bookmarks) are not.
/* Combine a list of PDFs together quickly */ /* Combine two PDFs together */ // Load the first document from the samples folder. DPL.LoadFromFile("sample1.pdf", ""); PrimaryDoc = DPL.SelectedDocument(); // Load the second document from the samples folder. DPL.LoadFromFile("sample2.pdf", ""); SecondaryDoc = DPL.SelectedDocument(); // Select the first document DPL.SelectDocument(PrimaryDoc); // Merge the second document with the // previously selected document. DPL.MergeDocument(SecondaryDoc); // Save the merged document to disk DPL.SaveToFile("merged_docs.pdf");
Lets get technical (optional)
It is important to note that merging two PDF files together is not the same as stapling two pieces of paper together. The only time you see a page in a PDF is when the PDF is rendered.
Internally each page in a PDF is represented by a page object. The page object is a dictionary which includes references to the page’s content and other attributes. The individual page objects are tied together in a structure called the page tree. However, the structure of the page tree is not necessarily related to the logical structure or flow of the document.
When you merge two PDF files together you need to take two separate PDFs and merge all of the objects and resources together. It is not simply a case of physically appending one document to another.
So instead of stapling two sheets together, a more accurate comparison for the PDF merging process would be the recycling of paper where multiple different sheets of paper are broken down (and fibres are mixed) to create a new ream of paper.
Note: there are a few different terms which can be used when describing the task of merging PDF files together. For example, you could say combining PDF files together or appending one PDF file to another PDF file or joining PDF files together . All of these terms are essentially the same.
This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.
Updated on May 16, 2022