Articles in this section
Category / Section

How to merge two Word documents without header and footer from the source document

2 mins read

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:

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. 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.Input Documents

By executing the program, you will get the Output document as follows.

Output document

Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail mergemerge 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 Essential DocIO?

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied