Click or drag to resize
Pdf Library for .NET

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:

// load pdf file from disk
PdfDocument doc = new PdfDocument(fileName);

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:

// append doc1 to doc2
doc2.Append(doc1);
Sample Code

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();
Merging inputs in different shapes

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:

// merge a file and a stream into a new document
using (FileStream fs = File.OpenRead("second.pdf"))
{
    PdfDocument merged = new PdfDocument();

    merged.Append("first.pdf");
    merged.Append(fs);

    merged.Save("combined.pdf");
    merged.Close();
}
Merging tagged (accessible) documents

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.

Merging into PDF/A documents

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.

Appending to an existing file

Pages can also be appended to a document loaded from a file or a stream, and the result saved back to the same file.

PdfDocument doc = new PdfDocument("report.pdf");
doc.Append("appendix.pdf");
doc.Save("report.pdf");
doc.Close();
See Also