How to merge PDF and DOCX and convert DOCX to PDF in Java
In this tutorial you’ll discover how to merge PDF and DOCX files and also convert DOCX to PDF using GroupDocs.Merger for Java. You’ll see how to load documents from streams, handle multi‑gigabyte payloads, and produce a single output that can be saved to disk, sent to a client, or stored in cloud storage. By the end, you’ll have a ready‑to‑use snippet that fits into any web service, batch job, or micro‑service architecture.
Quick answers
- What is the primary way to save a merged document in Java? Call
Merger.save(OutputStream)after adding all source files. - Can GroupDocs.Merger merge different file formats? Yes – it supports 30+ input and output formats, including DOCX, PDF, PPTX, XLSX, and many image types.
- How do I load a document from an InputStream? Instantiate
Mergerwith the stream:new Merger(stream). - What should I do with large documents? Use buffered streams, process files in batches, and close each stream as soon as possible to keep memory usage low.
- Is a license required for production use? Yes – a valid GroupDocs license unlocks full functionality and removes evaluation watermarks.
What is merge PDF and DOCX?
Merging PDF and DOCX means combining one or more PDF and Word documents into a single file while preserving each source’s page order, metadata, and visual fidelity. With GroupDocs.Merger you can also treat the merge operation as a fast way to convert DOCX to PDF by appending the DOCX content to a PDF container and then saving the result as PDF.
Why use GroupDocs.Merger to merge different file formats?
GroupDocs.Merger abstracts the low‑level details of each format, letting you focus on business rules. The library can process 30+ formats, handle files up to 500 MB without loading the entire document into memory, and guarantees that page numbering, headers, and footers remain intact across the merged output.
Prerequisites
- GroupDocs.Merger for Java library
- Java 8+ (JDK 8 or higher)
- Maven or Gradle for dependency management
- An IDE such as IntelliJ IDEA or Eclipse
- A valid GroupDocs license for production (a free trial is available)
Setting up GroupDocs.Merger for Java
Maven
Add the following dependency to your pom.xml file:
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-merger</artifactId>
<version>latest-version</version>
</dependency>
Gradle
In your build.gradle, include:
implementation 'com.groupdocs:groupdocs-merger:latest-version'
Direct download
Alternatively, download the latest version from GroupDocs.Merger for Java releases and manually add it to your project’s classpath.
License acquisition steps
- Free trial – explore core features without a credit card.
- Temporary license – request a short‑term key on the temporary license page (temporary license page).
- Full purchase – obtain an unlimited production license for commercial deployments. You can purchase a full license directly from GroupDocs (GroupDocs).
Basic initialization
The Merger class is the entry point for all operations; it represents a single merging session. After adding the library, create a Merger instance:
import com.groupdocs.merger.Merger;
// Initialize GroupDocs Merger
erMerger = new Merger("YOUR_DOCUMENT_DIRECTORY/SAMPLE_DOCX");
How to load document from stream?
Loading a document from an InputStream lets you work with files that come from HTTP uploads, cloud buckets, or in‑memory generation. An InputStream is a Java abstraction that reads bytes from a source such as a file, network socket, or memory buffer, allowing the library to process data without creating temporary files on disk. This approach reduces I/O overhead and improves performance in high‑throughput services.
Step 1 – create an input stream
import java.io.FileInputStream;
import java.io.InputStream;
InputStream stream = new FileInputStream("YOUR_DOCUMENT_DIRECTORY/SAMPLE_DOCX");
Why? The stream converts the physical file into a byte sequence that the Merger can read without persisting it on disk.
Step 2 – initialize Merger with the stream
Merger merger = new Merger(stream);
Why? Supplying the stream enables in‑memory processing, which is ideal for serverless functions or high‑throughput APIs.
How to save merged document java?
After merging, you must persist the combined content to a destination of your choice. An OutputStream is a Java abstraction that writes bytes to a target such as a file, network socket, or in‑memory buffer, giving you flexibility in how the merged document is delivered. Providing an OutputStream to Merger.save ensures the final PDF or DOCX is written efficiently and safely.
Step 1 – define an output stream
import java.io.FileOutputStream;
import java.io.OutputStream;
OutputStream outputStream = new FileOutputStream("YOUR_OUTPUT_DIRECTORY/merged_output.docx");
Why? The OutputStream tells Java where the merged file should be written, whether to disk, a response body, or cloud storage.
Step 2 – save the document
merger.save(outputStream);
Why? save() finalizes the merge, flushes buffers, and ensures the output adheres to the selected format’s specifications.
Step 3 – close the stream
outputStream.close();
Why? Closing releases OS handles and guarantees that all buffered bytes are persisted, preventing file corruption.
How to handle large documents java?
Large PDFs or multi‑gigabyte Word files can exhaust the JVM heap if loaded naively. To keep memory usage under control, wrap raw streams in BufferedInputStream and BufferedOutputStream, process files in manageable batches, and invoke Merger.close() after each operation to free native resources. Monitoring heap usage and setting an appropriate -Xmx flag (aim for ≤70 % of available memory) further protects against out‑of‑memory errors.
Practical applications
GroupDocs.Merger is ideal for real‑world tasks such as:
- Batch report generation – combine daily Excel exports (XLSX) and Word summaries (DOCX) into a single PDF for archiving.
- Dynamic invoice creation – merge a PDF invoice template with a DOCX terms‑and‑conditions document, then deliver the final PDF to the client.
- Cross‑platform services – expose a REST endpoint that accepts mixed‑format uploads, merges them, and streams the result back without storing temporary files.
Performance considerations
- Memory management – always close streams; the library uses native buffers that must be released.
- Batch operations – grouping files reduces the number of disk seeks and improves throughput by up to 30 %.
- Efficient I/O – for files larger than 10 MB, buffered I/O can cut processing time roughly in half.
Common issues and solutions
| Issue | Reason | Fix |
|---|---|---|
FileNotFoundException | Wrong path or insufficient permissions | Verify absolute/relative paths and ensure the app has read/write rights |
IOException during save | Stream not closed or disk full | Use try‑with‑resources, close streams, and check available disk space |
| Memory spikes with large PDFs | Whole file loaded into memory | Switch to buffered streams and process in smaller batches |
Frequently asked questions
Q: Can I merge different file formats using GroupDocs.Merger?
A: Yes, the library supports 30+ formats—including DOCX, PDF, PPTX, XLSX, and common image types—allowing seamless cross‑format merges.
Q: How do I handle large documents efficiently?
A: Wrap your file streams in BufferedInputStream/BufferedOutputStream, merge files in batches, and close each stream immediately after use.
Q: Is there support for password‑protected files?
A: Absolutely. Provide the password when constructing the Merger instance to open encrypted PDFs or DOCX files.
Q: Can I use this library in a commercial product?
A: Yes, a valid GroupDocs license unlocks full production capabilities without evaluation watermarks.
Q: What should I do if I encounter an IOException?
A: Double‑check file paths, ensure the process has sufficient permissions, verify disk space, and wrap I/O calls in try‑catch blocks or use try‑with‑resources.
Resources
- Documentation: GroupDocs Documentation
- API reference: API Reference Guide
- Download library: GroupDocs Downloads
- Purchase license: Buy GroupDocs License
- Free trial & temporary license: Try Out GroupDocs and Request a Temporary License
- Support: GroupDocs Support Forum
Last Updated: 2026-10-11
Tested With: GroupDocs.Merger latest version (as of 2026)
Author: GroupDocs