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.

Warning

Save needs an absolute output path. Given a relative one it does nothing at all — no file, no exception, no error code:

merger.Save("merged.docx");                    // returns cleanly, writes nothing
merger.Save(Path.GetFullPath("merged.docx"));  // writes the file

Input paths passed to the constructor and to Join may be relative. Only the output path must be absolute, which is why every example below wraps it in Path.GetFullPath.

Join two documents

Join appends another document to the one you opened. Call it repeatedly to combine several, then Save.

using System.IO;
using GroupDocs.Merger;

using (Merger merger = new Merger("contract.docx"))
{
    merger.Join("statement-of-work.docx");

    merger.Save(Path.GetFullPath("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, 16 KB)

Download full output

Extract a page range

ExtractPages keeps only the pages you name and discards the rest — useful for pulling a signed section or an appendix out of a long document.

using System.IO;
using GroupDocs.Merger;
using GroupDocs.Merger.Domain.Options;

using (Merger merger = new Merger("contract.pdf"))
{
    // Keep pages 1 to 3
    ExtractOptions extractOptions = new ExtractOptions(1, 3);

    merger.ExtractPages(extractOptions);
    merger.Save(Path.GetFullPath("merger-extract-pages.pdf"));
}

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

Binary file (PDF, 127 KB)

Download full output

Read document information

GetDocumentInfo reports the format, page count and page dimensions — worth checking before you extract a range that may not exist.

using System;
using GroupDocs.Merger;
using GroupDocs.Merger.Domain.Result;

using (Merger merger = new Merger("contract.pdf"))
{
    IDocumentInfo info = merger.GetDocumentInfo();

    Console.WriteLine("File type: " + info.Type.FileFormat);
    Console.WriteLine("Pages: " + info.PageCount);
    Console.WriteLine("Size: " + info.Size + " bytes");
}

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

File type: Portable Document Format File
Pages: 3
Size: 130412 bytes

Download full output

Learn more

GroupDocs.Merger also splits one document into several, removes, moves, swaps and rotates pages, changes page orientation, adds, updates and removes document passwords, generates page previews, and offers a fluent API 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.