In this example, two documents are combined by copying (importing) all sections of the source document and appending to the end of the destination document.
This approach is not limited to just combining documents. It is a common approach that you should use when you need to copy nodes from one document into another. There are three simple steps to copy any node from one document to another:
- Obtain the node in the source document that you want to copy.
- Import the node into the destination document. Importing creates a new node that is a copy of the original node, but suitable for insertion into the destination document.
- Insert the imported node into the destination document.
Since the time this article was written a new method Document.AppendDocument was added to allow appending one document to another in one simple step. However, this article was kept because the technique discussed here is useful in other situations when you want to copy paragraphs, tables, sections and so on.
Note how PageSetup.SectionStart of the first section of the document that is being added allows to control whether the appended document starts on the same or a new page. It is a property of a section in Microsoft Word documents that specifies how a section starts.
Example CombineDocuments
Combines two documents into one.
[C#]
public void CombineDocuments()
{
// Open the destination document.
Document dstDoc = new Document(MyDir + "Section.CombineDocuments1.doc");
// Open the source document.
Document srcDoc = new Document(MyDir + "Section.CombineDocuments2.doc");
// For a twist, let's say I want the second document to start on the same page where
// the first document ends. This is controlled by a property of the first section.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
// Combine the documents, see the function below.
AppendDoc(dstDoc, srcDoc);
// Save the finished document.
dstDoc.Save(MyDir + "Section.CombineDocuments Out.doc");
}
/// <summary>
/// A useful function that you can use to easily append one document to another.
/// </summary>
/// <param name="dstDoc">The destination document where to append to.</param>
/// <param name="srcDoc">The source document.</param>
public void AppendDoc(Document dstDoc, Document srcDoc)
{
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
foreach (Section srcSection in srcDoc)
{
// Because we are copying a section from one document to another,
// it is required to import the Section node into the destination document.
// This adjusts any document-specific references to styles, lists, etc.
//
// Importing a node creates a copy of the original node, but the copy
// is ready to be inserted into the destination document.
Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
// Now the new section node can be appended to the destination document.
dstDoc.AppendChild(dstSection);
}
}
[Visual Basic]
Public Sub CombineDocuments()
' Open the destination document.
Dim dstDoc As Document = New Document(MyDir & "Section.CombineDocuments1.doc")
' Open the source document.
Dim srcDoc As Document = New Document(MyDir & "Section.CombineDocuments2.doc")
' For a twist, let's say I want the second document to start on the same page where
' the first document ends. This is controlled by a property of the first section.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous
' Combine the documents, see the function below.
AppendDoc(dstDoc, srcDoc)
' Save the finished document.
dstDoc.Save(MyDir & "Section.CombineDocuments Out.doc")
End Sub
''' <summary>
''' A useful function that you can use to easily append one document to another.
''' </summary>
''' <param name="dstDoc">The destination document where to append to.</param>
''' <param name="srcDoc">The source document.</param>
Public Sub AppendDoc(ByVal dstDoc As Document, ByVal srcDoc As Document)
' Loop through all sections in the source document.
' Section nodes are immediate children of the Document node so we can just enumerate the Document.
For Each srcSection As Section In srcDoc
' Because we are copying a section from one document to another,
' it is required to import the Section node into the destination document.
' This adjusts any document-specific references to styles, lists, etc.
'
' Importing a node creates a copy of the original node, but the copy
' is ready to be inserted into the destination document.
Dim dstSection As Node = dstDoc.ImportNode(srcSection, True, ImportFormatMode.KeepSourceFormatting)
' Now the new section node can be appended to the destination document.
dstDoc.AppendChild(dstSection)
Next srcSection
End Sub
[Java]
public void CombineDocuments() throws Exception
{
// Open the destination document.
Document dstDoc = new Document(getMyDir() + "Section.CombineDocuments1.doc");
// Open the source document.
Document srcDoc = new Document(getMyDir() + "Section.CombineDocuments2.doc");
// For a twist, let's say I want the second document to start on the same page where
// the first document ends. This is controlled by a property of the first section.
srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
// Combine the documents, see the function below.
AppendDoc(dstDoc, srcDoc);
// Save the finished document.
dstDoc.save(getMyDir() + "Section.CombineDocuments Out.doc");
}
/// <summary>
/// A useful function that you can use to easily append one document to another.
/// </summary>
/// <param name="dstDoc">The destination document where to append to.</param>
/// <param name="srcDoc">The source document.</param>
public void AppendDoc(Document dstDoc, Document srcDoc) throws Exception
{
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
for (int i = 0; i < srcDoc.getSections().getCount(); i++)
{
Section srcSection = srcDoc.getSections().get(i);
// Because we are copying a section from one document to another,
// it is required to import the Section node into the destination document.
// This adjusts any document-specific references to styles, lists, etc.
//
// Importing a node creates a copy of the original node, but the copy
// is ready to be inserted into the destination document.
Node dstSection = dstDoc.importNode(srcSection, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Now the new section node can be appended to the destination document.
dstDoc.appendChild(dstSection);
}
}