How to merge two Word documents without header and footer from the source document
Syncfusion® Essential® DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can merge two Word documents without header and footer from the source document in C#.
Steps to merge two Word documents without header and footer from the source document:
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
- Use the following code example to merge two Word documents without header and footer from the source document.
C#
//Load the destination Word document as a stream.
using (FileStream destinationStreamPath = new FileStream(Path.GetFullPath(@"../../../DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open the destination Word document.
using (WordDocument destinationDocument = new WordDocument(destinationStreamPath, FormatType.Automatic))
{
//Load the source Word document as a stream.
using (FileStream sourceDocumentPathStream = new FileStream(Path.GetFullPath(@"../../../SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open the source Word document.
using (WordDocument sourceDocument = new WordDocument(sourceDocumentPathStream, FormatType.Docx))
{
//Iterate source Word document.
foreach (WSection sourceDocumentSection in sourceDocument.Sections)
{
//Remove the first page header.
sourceDocumentSection.HeadersFooters.FirstPageHeader.ChildEntities.Clear();
//Remove the first page footer.
sourceDocumentSection.HeadersFooters.FirstPageFooter.ChildEntities.Clear();
//Remove the even header.
sourceDocumentSection.HeadersFooters.EvenHeader.ChildEntities.Clear();
//Remove the even footer.
sourceDocumentSection.HeadersFooters.EvenFooter.ChildEntities.Clear();
//Remove the odd header.
sourceDocumentSection.HeadersFooters.OddHeader.ChildEntities.Clear();
//Remove the odd footer.
sourceDocumentSection.HeadersFooters.OddFooter.ChildEntities.Clear();
}
//Merge source Word document content to destination Word document.
destinationDocument.ImportContent(sourceDocument);
}
}
//Create a file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the Word document to the file stream.
destinationDocument.Save(outputFileStream, FormatType.Docx);
}
}
}
A complete working sample merge Word document without header and footer from the source document in C# can be downloaded from GitHub.
Input Destination and Source Word documents as follows.
By executing the program, you will get the Output document as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
See Also:
Is it possible to update Include Text field in Word document using DocIO?