Blog Post

How to Merge Two or More PDFs into a Single PDF Using Java (or Kotlin)

Illustration: How to Merge Two or More PDFs into a Single PDF Using Java (or Kotlin)

Many companies have reams of documents about customers, employees, inventory, etc. And in some cases, it can be useful to collate these documents to redistribute them. But how do you do that? In today’s post, I’ll show how to merge multiple PDF documents into a single PDF using Java and Kotlin, thereby solving the redistribution problem!

Java and Kotlin

Java has a long history and is used in many different environments. Today, it’s most likely known for being used with Android or on backend server work. Because it’s been around for such a long time and is still popular, we can be sure it won’t go away anytime soon.

Compared to Java, Kotlin is the new kid on the block. The great thing is that it works in most places Java does, so if you want the benefits of a new and interesting language but with the environment of Java, Kotlin is the way to go. If you’re looking for an expressive language with the possibility of native JavaScript and Java interoperability, it could be worth checking out the Kotlin guides.

Merging Example

In our hypothetical situation, we’ll have two PDF documents, each consisting of one page. We’ll merge these two pages together to create a PDF document with one page.

Image showing two individual pages merged into one document

// Get two documents and merge them together. 0 denotes the page index of where to insert the document.
DocumentEditor documentEditor = new DocumentEditor();

// Import the two documents.
File dogDocument = new File("Assets/dog.pdf");
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider(dogDocument));
File catDocument = new File("Assets/cat.pdf");
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider(catDocument));

// Write out to a new file.
File outputFile = File.createTempFile("dogCatPair", ".pdf");
documentEditor.saveDocument(new FileDataProvider(outputFile));
// Get two documents and merge them together. 0 denotes the page index of where to insert the document.
val documentEditor = DocumentEditor()

// Import the two documents.
val dogDocument = File("Assets/dog.pdf")
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, FileDataProvider(dogDocument))
val catDocument = File("Assets/cat.pdf")
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, FileDataProvider(catDocument))

// Write out to a new file.
val outputFile = File.createTempFile("dogCatPair", ".pdf")
documentEditor.saveDocument(FileDataProvider(outputFile))

With just a few lines of code and the PSPDFKit Document Editor, we managed to merge our documents into one.

Each document is added by calling the importDocument method, with 0 denoting index 0 and DocumentEditor.IndexPosition.BeforeIndex instructing the editor to place the pages before the given index (this is necessary for the first document, as there are no other indices to reference yet). Then we instruct the Document Editor where to find the document to import. In the code example above, we take a simple file path, but it’s also possible to pass a custom data provider that extends DataProvider to provide data from any source required, such as memory, network data, or even a cryptographic solution.

We can keep repeating these steps to merge as many documents as we desire. One factor to be aware of is the size of the PDFs in memory. Opening and appending PDFs can be a memory-intensive process, so if you’re importing large documents, keep this in mind.

Extra Operations

We completed our task and merged two documents together. So now, let’s look into the many features of the Document Editor.

With the two documents combined into one, we now have a front page that doesn’t reflect the data it holds.

So, how about we remove that page?

...
File projectDocument = new File("Assets/projectPlan.pdf");
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider(projectDocument));
File tpsDocument = new File("Assets/tpsReport.pdf");
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider(tpsDocument));

// Now remove the first page.
Set<Integer> pages = new HashSet<>();
pages.add(0);
documentEditor.removePages(pages);
...
...
val projectDocument = File("Assets/projectPlan.pdf")
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, FileDataProvider(projectDocument))
val tpsDocument = File("Assets/tpsReport.pdf")
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, FileDataProvider(tpsDocument))

// Now remove the first page.
documentEditor.removePages(setOf(0))
...

Again we added the two documents, and for the final operation, we instructed the Document Editor to remove the first page. We can continue staging instructions like this to our heart’s desire — including but not limited to addPage, removePages, rotatepages, and setPageLabel — to create the final PDF needed.

Conclusion

This blog post provided a quick overview of the document editing possibilities of the PSPDFKit Library for Java, and there’s much more to explore. You can do more with our PDF library by leveraging other components. For example, combine:

  • Document Editor and Forms to merge multiple PDF forms into one file or split one PDF into multiple PDFs.

  • OCR and Redaction to find and remove sensitive information from scanned PDF documents.

Try our PDF library using the free trial, and check out our Java PDF library guides and Java API documentation for more examples and other important features of the SDK.

Related Products
Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
TUTORIALS  |  Java • Kotlin • PDF • How To

Converting a PDF to an Image in Java

DEVELOPMENT  |  Java • Kotlin • PDF • How To

Java PDF Editor: How to Programmatically Edit PDFs Using Java

TUTORIALS  |  API • Java • How To

How to Convert DOCX to PDF Using Java