Use document merging to combine multiple files into one PDF.

Common use cases include:

  • Consolidating reports into one deliverable
  • Combining scanned and digital documents
  • Building document packages for distribution
  • Merging mixed formats into a single PDF output
Download sample

How Nutrient helps

Nutrient Java SDK handles document appending and export.

The SDK enables you to:

  • Open documents from file paths or streams
  • Append one document into another
  • Export the final merged document as PDF

Preparing the project

Start by specifying a package name and importing the required classes:

package io.nutrient.Sample;
import io.nutrient.sdk.*;
import io.nutrient.sdk.exceptions.NutrientException;
import io.nutrient.sdk.editors.*;
import io.nutrient.sdk.exporters.*;

Create the main method and declare NutrientException for this sample:

public class PDFMerging {
public static void main(String[] args) throws NutrientException {

Merging multiple documents

Open the base document with try-with-resources.

In this sample:

  • The base input is input.docx.
  • File paths are used.
  • Streams are also supported.
try (Document document = Document.open("input.docx")) {

Create a PdfEditor instance for merge operations:

PdfEditor editor = PdfEditor.edit(document);

Appending additional documents

Open another document and append it to the base document.

In this sample, a PDF (input.pdf) is appended to the Word-based document. The SDK handles format conversion automatically, so appendDocument() can merge PDF with other supported input formats, including Word (.docx, .doc), Excel (.xlsx, .xls), PowerPoint (.pptx, .ppt), and additional supported document types (for example, .rtf, .odt, .txt, .md, .html, .mhtml).

If the appended document isn’t already a PDF, the SDK converts it to PDF before appending (with implicit conversion enabled by default).

try (Document document2 = Document.open("input.pdf")) {
editor.appendDocument(document2);
}

Saving the merged result

Save editor changes and export the merged result as PDF:

editor.save();
editor.close();
document.export("output_merged_documents.pdf", new PdfExporter());
}
}
}

Error handling

The sample throws NutrientException for SDK and processing issues.

In production code:

  • Catch NutrientException.
  • Return a clear error message.
  • Log failure details for debugging.

Conclusion

Use this workflow to merge documents into one PDF:

  1. Open the base document.
  2. Create a PdfEditor.
  3. Open each additional document and append it.
  4. Save editor changes.
  5. Export the merged output with PdfExporter.

Available input formats can depend on your license/features (for example, Office, HTML, or image conversion capabilities).

For related document workflows, refer to the Java SDK guides.

Download this ready-to-use sample package.