Using WordWriter to merge Word Documents

Several of our consulting projects have had a similar need: programatically merge multiple Word documents into a single Word file. Using SoftArtisans’ WordWriter, this task is fairly easy. In this article, I’ll show you how. If you’re not a current WordWriter user, you can download a free evaluation and follow along!

When you use WordWriter to merge documents, the page setup of the destination document you are merging into is used for all pages. (This follows the same behavior as Word.) If you create a new document to merge into, you’re resulting document will have default formatting.

If you want to preserve the formatting of each document that you are merging, insert a section break in your final output file before merging each document. You will then need to copy the formatting information from each document to the section you just created. Some types of document formatting that you may want to preserve include: margins; paper size or orientation; page borders; vertical alignment; and number of columns. (Text formatting, such as font, font size, text color are preserved, so you don’t need to manually set these.)

There is one tricky content type: tables. Tables will automatically take the formatting of the destination document as soon as you do the merge. To revert the formatting of the document, including tables, back to their original layout, follow these steps:

  1. Add a new section to the destination document.
  2. Merge a document into the new section of the destination document.
  3. Set the new section’s page orientation and margins from the source document’s first section.
  4. Iterate over all of the tables in the source document and copy each table’s Formatting property to the matching table in the new section.

Here are some code samples that show how to perform each of these steps. Code samples are in C#.

0. These code samples assume you have created a WordApplication object and have 2 Document objects: one for your source document and one for your final Word report (the file you are merging into).

using SoftArtisans.OfficeWriter.WordWriter;
.
.
.
WordApplication wApp = new WordApplication();
Document outDoc = wApp.Create(); // Our final document containing all of the merged pieces.
Document srcDoc = wApp.Open("FirstDocToMerge.doc") // The source document that will be merged into our final document.

1. Add a new section to the destination document.

// Insert a new section at the end of the document
// and save the section object for later use.
Section lastSection = outDoc.CreateSectionAfter();

2. Merge a document into the newly created section of the destination document

outDoc.InsertAfter(srcDoc);

3. Set the new section’s page orientation and margins from the original document’s first section

// Get the first section from the document we are merging.
Section sourceSection = srcDoc.Sections[0];
// Copy the page orientation (portrait vs. landscape) settings
// and the page margins into the newly created section.
lastSection.PageOrientation = sourceSection.PageOrientation;
lastSection.LeftMargin = sourceSection.LeftMargin;
lastSection.RightMargin = sourceSection.RightMargin;
lastSection.TopMargin = sourceSection.TopMargin;
lastSection.BottomMargin = sourceSection.BottomMargin;

4. If you have tables with formatting you want to preserve, iterate over all of the tables in the source document and copy each table’s Formatting property to the matching table in the new section.

// Performing the merge automatically reformatted the tables to
// match the destination document's format settings. Change the
// table formatting back to its original settings.
Element[] tables = srcDoc.get_Elements(Element.Type.Table);
int sourceTableCount = tables.Length;
for (int i = 0; i < sourceTableCount; i++)
{
Table destTable = (Table)lastSection.get_Elements(Element.Type.Table)[i];
destTable.Formatting = ((Table)tables[i]).Formatting;
}

That’s all there is to it.

Related posts: