Merge Documents

Wordize Merge for .NET combines your documents into one, saving time and reducing manual work. Merge documents, preserving styles and choosing a convenient output document format.

Go to Merge Documents Code Examples

Wordize provides the Wordize Merge for .NET module to combine multiple documents into one, saving time and reducing manual work. Programmatically merging documents ensures consistent formatting, supports various output formats (e.g. DOCX, PDF), and enhances document management. It is especially useful for creating reports, consolidating legal documents, and handling large-scale data efficiently.

How to Merge Documents

Merging documents appends the second document to the end of the first. Use one of the following Merge methods to programmatically merge two documents:

string inputDoc1 = "Document1.docx";
string inputDoc2 = "Document2.docx";

Merger.Merge("MergeDocument.1.docx", new[] { inputDoc1, inputDoc2 });

Find a full list of code examples for merging documents in the Merge Documents Code Examples section at the bottom of the page.

How to Specify Output Document Formatting

You can also specify how formatting is merged when combining multiple documents using the MergeFormatMode enumeration:

  • MergeFormatting — to combine document formatting.
  • KeepSourceFormatting — to retain source documents’ formatting
  • KeepSourceLayout — to preserve source documents’ layout

The following code example shows how to merge two documents into one, preserving the source documents’ layout and using the Merge method:

string inputDoc1 = "Document1.docx";
string inputDoc2 = "Document2.docx";

Merger.Merge("MergeDocument.2.pdf", new[] { inputDoc1, inputDoc2 }, SaveFormat.Pdf, MergeFormatMode.KeepSourceLayout);

Find a full list of code examples for merging documents with output formatting in the Merge Documents Code Examples section at the bottom of the page.

How to Merge To an Image

You can merge two documents and save the result as an image. In this case, each element of the returned array represents one page of output, rendered as an image.

The following code example shows how to merge two documents and save the result as a PNG:

Stream[] pages = Merger.MergeToImages(new string[] { "Document1.pdf", "Document2.docx" }, new ImageSaveOptions(SaveFormat.Png), MergeFormatMode.KeepSourceFormatting);

Find a full list of code examples for merging documents to an image in the following Merge Documents Code Examples section.

Merge Documents Code Examples

Wordize provides both a Standard API and a Fluent API, allowing developers to choose the most convenient approach for their needs.

Go to Merge Documents description

Wordize API


method Merge(string, string[])
string inputDoc1 = "Document1.docx";
string inputDoc2 = "Document2.docx";

Merger.Merge("MergeDocument.1.docx", new[] { inputDoc1, inputDoc2 });

method Merge(Stream, Stream[], SaveFormat)
using var firstStreamIn = File.OpenRead("Document1.docx");
using var secondStreamIn = File.OpenRead("Document1.docx");
using var streamOut = new File.Create("MergeStreamDocument.1.docx");

Merger.Merge(streamOut, new[] { firstStreamIn, secondStreamIn }, SaveFormat.Docx);

method Merge(string, string[], SaveFormat, MergeFormatMode)
string inputDoc1 = "Document1.docx";
string inputDoc2 = "Document2.docx";

Merger.Merge("MergeDocument.2.pdf", new[] { inputDoc1, inputDoc2 }, SaveFormat.Pdf, MergeFormatMode.KeepSourceLayout);

method MergeToImages(string[], ImageSaveOptions, MergeFormatMode)
Stream[] pages = Merger.MergeToImages(new string[] { "Document1.pdf", "Document2.docx" }, new ImageSaveOptions(SaveFormat.Png), MergeFormatMode.KeepSourceFormatting);

method MergeToImages(Stream[], ImageSaveOptions, MergeFormatMode)
using var firstStreamIn = File.OpenRead("Document1.pdf");
using var secondStreamIn = File.OpenRead("Document2.pdf");

Stream[] pages = Merger.MergeToImages(new Stream[] { firstStreamIn, secondStreamIn }, new ImageSaveOptions(SaveFormat.Png), MergeFormatMode.KeepSourceFormatting);

Wordize Fluent API

Within the Fluent API, there is a context. MergerContext allows you to customize the document merging process. You can merge documents with or without context.


method to Merge documents in different formats and save the result as files in DOCX and PDF formats
Merger.Create()
    .From("DocumentIn.docx")
    .From("DocumentIn.pdf")
    .From("DocumentIn.doc")
    .From("DocumentIn.rtf")
    .To("DocumentOut.docx")
    .To("DocumentOut.pdf")
    .Execute();

method to Merge documents from files and save the result as a stream
using var streamOut = new File.Create("MergeStreamDocument.1.docx");

Merger.Create()
    .From("DocumentIn.docx")
    .From("DocumentIn.pdf")
    .To(streamOut)
    .Execute();

method to Merge documents in different formats and save the result as files in DOCX and PDF formats
MergerContext mergerContext = new MergerContext();
mergerContext.MergeFormatMode = MergeFormatMode.KeepSourceLayout;

Merger.Create(mergerContext)
    .From("DocumentIn.docx")
    .From("DocumentIn.pdf")
    .From("DocumentIn.doc")
    .From("DocumentIn.rtf")
    .To("DocumentOut.docx")
    .Execute();

method to Merge documents from files and save the result to an image

The result can be saved not only to a file or stream, but also to a list of streams. The latter is useful when saving the result as images, where each page of the document is saved as a separate stream and added to the list. In this case, you can also specify multiple output types at the same time.

List<Stream> outputStreams = new List<Stream>();

Merger.Create()
    .From("Document1.pdf")
    .From("Document2.pdf")
    .To(outputStreams, new ImageSaveOptions(SaveFormat.Png)) // Note: a separate stream is created for each page. The streams disposing is the consumer responsibility.
    .Execute();

method to Merge documents from streams and save the result to an image
using var firstStreamIn = File.OpenRead("Document1.pdf");
using var secondStreamIn = File.OpenRead("Document2.pdf");

List<Stream> outputStreams = new List<Stream>();

Merger.Create()
    .From(firstStreamIn)
    .From(secondStreamIn)
    .To(outputStreams, new ImageSaveOptions(SaveFormat.Png)) // Note: a separate stream is created for each page. The streams disposing is the consumer responsibility.
    .Execute();