Merging Word documents
29 Nov 202410 minutes to read
You can merge multiple Word documents into single Word document by using DocIO’s capability of importing contents from one document to another. The imported contents are appended at the end of document.
Assemblies and NuGet packages required
Refer to the following links for assemblies and NuGet packages required based on platforms to merge Word documents using the .NET Word Library (DocIO).
Merge document in new page
The following code example illustrates how to import the contents from source document into destination document where the contents are appended.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
FileStream sourceStreamPath = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
FileStream destinationStreamPath = new FileStream(destinationFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Opens an source document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument(sourceStreamPath, FormatType.Automatic))
{
//Opens the destination document
WordDocument destinationDocument = new WordDocument(destinationStreamPath, FormatType.Docx);
//Imports the contents of source document at the end of destination document
destinationDocument.ImportContent(document, ImportOptions.UseDestinationStyles);
//Saves and closes the destination document to MemoryStream
MemoryStream stream = new MemoryStream();
destinationDocument.Save(stream, FormatType.Docx);
destinationDocument.Close();
document.Close();
}
//Opens the source document
WordDocument sourceDocument = new WordDocument(sourceFileName);
//Opens the destination document
WordDocument destinationDocument = new WordDocument(targetFileName);
//Imports the contents of source document at the end of destination document
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles);
//Saves the destination document
destinationDocument.Save(outputFileName, FormatType.Docx);
//closes the document instances
sourceDocument.Close();
destinationDocument.Close();
'Opens the source document
Dim sourceDocument As New WordDocument(sourceFileName)
'Opens the destination document
Dim destinationDocument As New WordDocument(targetFileName)
'Imports the contents of source document at the end of destination document
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles)
'Saves the destination document
destinationDocument.Save(outputFileName, FormatType.Docx)
'closes the document instances
sourceDocument.Close()
destinationDocument.Close()
You can download a complete working sample from GitHub.
In the resultant document, the imported contents start from a new page followed by existing contents in a destination document. This is the default behavior.
Merge document in same page
When your requirement is to append the contents from the same page instead of starting from a new page, you need to set the break code of first section of Source document as NoBreak. The following code example illustrates the importing contents from the same page.
FileStream sourceStreamPath = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
FileStream destinationStreamPath = new FileStream(destinationFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Opens an source document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument(sourceStreamPath, FormatType.Automatic))
{
//Opens the destination document
WordDocument destinationDocument = new WordDocument(destinationStreamPath, FormatType.Docx);
//Sets the break-code of First section of source document as NoBreak to avoid imported from a new page
document.Sections[0].BreakCode = SectionBreakCode.NoBreak;
//Imports the contents of source document at the end of destination document
destinationDocument.ImportContent(document, ImportOptions.UseDestinationStyles);
//Saves and closes the destination document to MemoryStream
MemoryStream stream = new MemoryStream();
destinationDocument.Save(stream, FormatType.Docx);
destinationDocument.Close();
document.Close();
}
//Opens the source document
WordDocument sourceDocument = new WordDocument(sourceFileName);
//Opens the destination document
WordDocument destinationDocument = new WordDocument(targetFileName);
//Sets the break-code of First section of source document as NoBreak to avoid imported from a new page
sourceDocument.Sections[0].BreakCode = SectionBreakCode.NoBreak;
//Imports the contents of source document at the end of destination document
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles);
//Saves the destination document
destinationDocument.Save(outputFileName, FormatType.Docx);
//Closes the document instances
sourceDocument.Close();
destinationDocument.Close();
'Opens the source document
Dim sourceDocument As New WordDocument(sourceFileName)
'Opens the destination document
Dim destinationDocument As New WordDocument(targetFileName)
'Sets the break-code of first section of source document as NoBreak to avoid imported from a new page
sourceDocument.Sections(0).BreakCode = SectionBreakCode.NoBreak
'Imports the contents of source document at the end of destination document
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles)
'Saves the destination document
destinationDocument.Save(outputFileName, FormatType.Docx)
'Closes the document instances
sourceDocument.Close()
destinationDocument.Close()
You can download a complete working sample from GitHub.
Maintain Imported List style information
The following code example shows how to maintain information about imported list styles in a Word document while cloning and merging multiple Word documents.
//Opens the source document
FileStream SourceFileStream = new FileStream("Source.docx", FileMode.Open);
WordDocument sourceDocument = new WordDocument(SourceFileStream, FormatType.Docx);
//Opens the destination document
FileStream DestinationFileStream = new FileStream("Destination.docx", FileMode.Open);
WordDocument destinationDocument = new WordDocument(DestinationFileStream, FormatType.Docx);
//Sets true value to maintain imported list style cache to destination document
destinationDocument.Settings.MaintainImportedListCache = true;
//Processes the body contents for each section in the Word document
foreach (WSection section in sourceDocument.Sections)
{
//Accesses the body of section where all the contents in document are apart
foreach (TextBodyItem bodyItem in section.Body.ChildEntities)
{
destinationDocument.LastSection.Body.ChildEntities.Add(bodyItem.Clone());
}
}
//Closes the source document
sourceDocument.Close();
//Sets false value to exclude imported list style cache to destination document
destinationDocument.Settings.MaintainImportedListCache = false;
//Saves the destination document
MemoryStream outputStream = new MemoryStream();
destinationDocument.Save(outputStream, FormatType.Docx);
//Closes the destination document
destinationDocument.Close();
//Opens the source document
WordDocument sourceDocument = new WordDocument(sourceFileName);
//Opens the destination document
WordDocument destinationDocument = new WordDocument(targetFileName);
//Sets true value to maintain imported list style cache to destination document
destinationDocument.Settings.MaintainImportedListCache = true;
//Processes the body contents for each section in the Word document
foreach (WSection section in sourceDocument.Sections)
{
//Accesses the body of section where all the contents in document are apart
foreach (TextBodyItem bodyItem in section.Body.ChildEntities)
{
destinationDocument.LastSection.Body.ChildEntities.Add(bodyItem.Clone());
}
}
//Closes the source document
sourceDocument.Close();
//Sets false value to exclude imported list style cache to destination document
destinationDocument.Settings.MaintainImportedListCache = false;
//Saves the destination document
destinationDocument.Save(outputFileName, FormatType.Docx);
//Closes the destination document
destinationDocument.Close();
'Opens the source document
Dim sourceDocument As New WordDocument(sourceFileName)
'Opens the destination document
Dim destinationDocument As New WordDocument(targetFileName)
'Sets true value to maintain imported list style cache to destination document
destinationDocument.Settings.MaintainImportedListCache = True
'Processes the body contents for each section in the Word document
For Each section As WSection In sourceDocument.Sections
'Accesses the body of section where all the contents in document are apart
For Each bodyItem As TextBodyItem In section.Body.ChildEntities
destinationDocument.LastSection.Body.ChildEntities.Add(bodyItem.Clone())
Next
Next
'Closes the source document
sourceDocument.Close()
'Sets false value to exclude imported list style cache to destination document
destinationDocument.Settings.MaintainImportedListCache = False
'Saves the destination document
destinationDocument.Save(outputFileName, FormatType.Docx)
'Closes the destination document
destinationDocument.Close()
See Also
- How to merge multiple Word documents in C#, VB.NET
- How to remove Section Break when merging documents using ImportContent API
- How to merge Word documents without changing the page numbers?
- How to merge multiple documents with header and footer of destination document?
- How to merge Word documents and generate TOC in C#?