Several of our consulting projects have had a similar need: programatically merge multiple Word documents into a single Word file. Using WordWriter, this task is fairly easy.
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, your 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#. 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).
s.OfficeWriter.WordWriter;
.
.
.
WordApplication wApp =
new
WordApplication();
Document outDoc = wApp.Create();
Document srcDoc = wApp.Open(
"FirstDocToMerge.doc"
)
1. Add a new section to the destination document.
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.
Section sourceSection = srcDoc.Sections[
0
];
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.
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.