Merge and split documents with GroupDocs.Merger

GroupDocs.Merger combines documents and rearranges their pages — join, split, extract, remove, reorder, rotate — and manages document passwords. It works on Word, PDF, spreadsheets, presentations, images and archives.

Every example below opens contract.docx and is ready to copy into a project once you have installed the package.

Join two documents

join appends another document to the one you opened. Call it repeatedly to combine several, then save the result.

import com.groupdocs.merger.Merger;

Merger merger = new Merger("contract.docx");

merger.join("statement-of-work.docx");

// Merger.save resolves a bare relative name oddly, so pass an absolute path
merger.save(new java.io.File("merger-join-documents.docx").getAbsolutePath());

contract.docx and statement-of-work.docx are the sample files used in this example. Download contract.docx and statement-of-work.docx.

Binary file (DOCX, 15 KB)

Download full output

Split a document into pages

split breaks one document into several. With SplitMode.Pages and a list of page numbers, each listed page is written out as its own file — here every page of the three-page contract lands separately in the merger-split-pages folder.

import com.groupdocs.merger.Merger;
import com.groupdocs.merger.domain.options.SplitMode;
import com.groupdocs.merger.domain.options.SplitOptions;

Merger merger = new Merger("contract.docx");

// {0} is replaced with the page number, so page 1 becomes page_1.docx
int[] pageNumbers = new int[] { 1, 2, 3 };
SplitOptions splitOptions = new SplitOptions(
    "merger-split-pages/page_{0}.docx", pageNumbers, SplitMode.Pages);

merger.split(splitOptions);

contract.docx is the sample file used in this example. Click here to download it.

merger-split-pages/page_1.docx (14 KB)
merger-split-pages/page_2.docx (10 KB)
merger-split-pages/page_3.docx (8 KB)

Download full output

Read document information

getDocumentInfo reports the format, page count and size — worth checking before you split a document into pages that may not exist.

import com.groupdocs.merger.Merger;
import com.groupdocs.merger.domain.result.IDocumentInfo;

Merger merger = new Merger("contract.docx");

IDocumentInfo info = merger.getDocumentInfo();

System.out.println("File type: " + info.getType().getFileFormat());
System.out.println("Pages: " + info.getPageCount());
System.out.println("Size: " + info.getSize() + " bytes");

contract.docx is the sample file used in this example. Click here to download it.

File type: Microsoft Word Open XML Document
Pages: 3
Size: 15096 bytes

Download full output

Learn more

GroupDocs.Merger also removes, moves, swaps and rotates pages, changes page orientation, adds, updates and removes document passwords, generates page previews, and offers a page builder for chaining operations.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.