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 { Merger } from '@groupdocs/groupdocs.total';
import { resolve } from 'path';

const 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(resolve('merger-join-documents.docx'));

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 { Merger } from '@groupdocs/groupdocs.total';
import java from 'java';

const SplitOptions = java.import('com.groupdocs.merger.domain.options.SplitOptions');
const SplitMode = java.import('com.groupdocs.merger.domain.options.SplitMode');

const merger = new Merger('contract.docx');

// {0} is replaced with the page number, so page 1 becomes page_1.docx
const pageNumbers = java.newArray('int', [1, 2, 3]);
const 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 { Merger } from '@groupdocs/groupdocs.total';

const merger = new Merger('contract.docx');
const info = merger.getDocumentInfo();

console.log('File type: ' + info.getType().getFileFormat());
console.log('Pages: ' + info.getPageCount());
console.log('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.