Skip to main content
Version: Version 1.5

Merge PDF documents

The Pdftools SDK lets you assemble pages from multiple input PDF documents into a single output PDF document. During this process, duplicated resources such as fonts are identified and merged, significantly reducing the size of the output PDF document. The DocumentAssembler class automatically resolves conflicts between named elements like form fields.

Quick start

Download the full sample now in C#, Java, C, and Python.

Interested in other language samples? Let us know on the contact page and we'll add it to our samples backlog.

Depending on the requirements, you can adjust the characteristics of the output document by setting the PageCopyOptions Class used in the assembly process.

You can also add images to the output document by converting an image to a PDF document and then merging the converted image into the output PDF document.

Steps to merge PDF documents:

  1. Creating a DocumentAssembler object
  2. Reading the input documents
  3. Appending to the output document
  4. Running the Assemble method
  5. Full example

Before you begin

Creating a DocumentAssembler object

Create the DocumentAssembler object. This object will generate the output PDF document. Instantiate the DocumentAssembler and pass it an output Stream (for example, a file or memory stream) that will contain the output data.

// Open an output stream and pass it to the DocumentAssembler
using var outStream = File.Create(outPath);
using var docAssembler = new PdfTools.DocumentAssembly.DocumentAssembler(outStream);

Reading the input documents

Iterate through the input document set, loading each file as a Document.

// Iterate through the set of input PDF documents
foreach (var inPath in inPaths)
{
using var inStream = File.OpenRead(inPath);
using var inDoc = PdfTools.Pdf.Document.Open(inStream);

Appending to the output document

Select a page range from the input Document by passing optional firstPage and lastPage parameters to the Append method of the DocumentAssembler object. If these parameters are not passed, the entire input Document is appended to the output PDF document.

This example uses the default parameters, so the entire input Document is appended.

// Append the entire content of each input document to the output document
// Optionally, firstPage and lastPage parameters can be passed to append only a range of pages
docAssembler.Append(inDoc);

Running the Assemble method

After using the Append method to merge the input documents into the output PDF document, the final step is to call the Assemble method. This method creates the structure of the output PDF document and writes the document to the output Stream of the DocumentAssember object.

// Create the final structure of the output PDF document and write it to the output stream
docAssembler.Assemble();
tip

Don't forget that some objects (like the Document object) must be explicitly closed. For these objects, we recommend using the mechanism for automatically closing objects.

Full example

// Create output stream
using var outStream = File.Create(outPath);
using var docAssembler = new PdfTools.DocumentAssembly.DocumentAssembler(outStream);

// Iterate through the set of input PDF documents
foreach (var inPath in inPaths)
{
using var inStream = File.OpenRead(inPath);
using var inDoc = PdfTools.Pdf.Document.Open(inStream);

// Append the entire content of each input document to the output document
docAssembler.Append(inDoc);
}

// Create the final structure of the output PDF document and write it to the output stream
docAssembler.Assemble();