Pdf Merge | |
Select.Pdf can be used to merge several existing pdf documents into a single pdf document.
To achive this task, individual pdf files need to be loaded into PdfDocument objects. The following sample code shows how to load an existing file from the disk:
Another important method that will be used by the pdf merge feature is Append(PdfDocument) of the PdfDocument object. Using this method, a pdf document can be appended to another pdf document:
This sample code shows how to use SelectPdf Library for .NET to merge 2 different pdf documents into a new pdf document.
// the 2 initial files string file1 = @"C:\Path\to\doc1.pdf"; string file2 = @"C:\Path\to\doc2.pdf"; // load the 2 pdf documents PdfDocument doc1 = new PdfDocument(file1); PdfDocument doc2 = new PdfDocument(file2); // create a new pdf document PdfDocument doc = new PdfDocument(); // add the pages of those 2 documents to the new document doc.Append(doc1); doc.Append(doc2); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close(); // close the original pdf documents doc1.Close(); doc2.Close();
The Append method of the PdfDocument class accepts inputs in different shapes - a file path, a Stream, or another PdfDocument. Calling Append repeatedly on a target document combines all the inputs into it:
Appending into a tagged document carries the appended document's logical structure across, so the merged document keeps a reading order that covers all of its pages and stays conformant. Appending into a document that is not tagged leaves the structure behind - the destination makes no accessibility claim, and the appended structure would be the only structure in it.
See Accessible PDF and PDF/A for tagged and PDF/A output.
A document created with a PDF/A standard (PdfDocument(PdfStandard)) takes pages only from documents that conform to its level: the same PDF/A part with at least the same conformance (for example a PDF/A-3a or PDF/A-3u source into a PDF/A-3b document). Appending a document that claims no PDF/A conformance, or another part, throws an exception rather than producing a file whose PDF/A claim is false. A document with no PDF/A standard takes any source.
Pages can also be appended to a document loaded from a file or a stream, and the result saved back to the same file.