Merge PDF documents
Debenu 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 two PDF documents together
If you want to merge two PDF files together then the MergeDocument funtion 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.
// Setup the files for merging
string firstDoc = "sample1.pdf";
string secondDoc = "sample2.pdf";
string DestFileName = "merged_samples.pdf";
// Load the first PDF into memory
QP.LoadFromFile(firstDoc);
int MainDocID = QP.SelectedDocument();
// Load the second PDF into memory
QP.LoadFromFile(secondDoc);
int AppendID = QP.SelectedDocument();
// Select the PDF to which the other PDF
// should be appended to and then merge.
// After merging AppendID will be removed
// from memory and MainDocID will be selected
QP.SelectDocument(MainDocID);
QP.MergeDocument(AppendID);
// Remove document from memory
QP.SaveToFile(DestFileName);
Merge a list of PDF files together
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.
// Add a range of files to a named list
QP.AddToFileList("FilesToMerge", "sample1.pdf");
QP.AddToFileList("FilesToMerge", "sample2.pdf");
QP.AddToFileList("FilesToMerge", "sample3.pdf");
// Merge the list of files together
QP.MergeFileList("FilesToMerge", "merged_file_list.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.