Merging different documents is often required when you intend to gather the scattered data of different documents into one single file. In this article, you will learn to automate the documents merging process. This will show how to programmatically merge multiple documents of either the same or different file types into one file using Java. In another post, we discussed merging multiple files of different formats using C#.
The following topics are covered below:
- Java API - Merge Multiple Files
- Merge PDF, Word, Excel files into one PDF
- Merge Selective Pages of Multiple files into One File
Java API for Merging Multiple Document Types
I will be using GroupDocs.Merger for Java to combine documents of different file formats into one file. The Java API allows joining various documents of the same or different formats into one file. Furthermore, it allows documents to split, trim, swap, move, remove, rotate, or arrange pages accordingly. Additionally, it supports passwords and their removal to manage the security of the supported document formats.
Some of the document types the API supports include; word-processing documents, spreadsheets, presentations, HTML, PDF, eBooks, Visio drawings, CSV, and TSV.
Download and Configure
Get the document merger library from the downloads section. For Maven-based Java applications, add the following configuration within pom.xml. Afterward, you can try document merging java examples of this article as well as many more from GitHub. For the details, you may also visit the API Reference.
<repository>
<id>GroupDocsJavaAPI</id>
<name>GroupDocs Java API</name>
<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-merger</artifactId>
<version>21.3</version>
</dependency>
Merge PDF, Word, Excel files into one PDF in Java
PDF documents can be combined with your Word documents, Excel spreadsheets, PowerPoint presentations, and other PDF documents with just a few lines of code. The following are the steps of how to merge documents of multiple file types into one file.
- Load the initial document using the Merger class.
- Combine the second document using the join method.
- Keep merging the other documents (if required) using the same or similar join method.
- Save the final combined document on path or stream using the relevant save method.
The following source code shows how to merge PDF, Word, and Excel documents into one PDF file in Java.
Similarly, documents with the same file types can be combined. The below-mentioned is the output obtained by joining a word document, a PDF document. and a spreadsheet using the above-mentioned Java code.
Merge Selective Pages of Multiple PDF, Word, Excel files into One PDF in Java
If you want to pick few pages from one document and some other selective pages from the next document, and so on. The API allows you to merge selective pages of multiple file types into one file in different ways.
- Load the initial document using the Merger class.
- Prepare the merging options with JoinOptions class.
- Start merging the document using the join method.
- Keep joining the documents by setting appropriate joining options for each document.
- Save the final merged document using the save method.
The following source code shows how to merge the first page of a Word document and the even sheets of Excel spreadsheet in the provided range in Java with a PDF document. The output will be a single PDF file.
Get a Free API License
You can get a free temporary license in order to use the API without the evaluation limitations.
Conclusion
To conclude, you learned how to merge two or more documents of similar or different file types into one file using Java with your application. Additionally, you learned how to combine selective pages of multiple file types into one file.
You can learn more about GroupDocs.Merger using the documentation. In case you have queries, contact us via forum.