Articles in this section
Category / Section

How to copy the HeaderFooter content from one Word document and insert them into first page header footer of another Word document

2 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can copy the HeaderFooter content from one Word document and insert them into first page header footer of another Word document.

Steps to copy the Header Footer content from one Word document and insert them into first page header footer of another Word document programmatically:

  1. Create a new C# console application project.

    Graphical user interface, application

Description automatically generated

  2. Install the Syncfusion.DocIO.Winforms NuGet package as a reference to your .NET Framework applications from NuGet.org.

    Graphical user interface, text, application, email

Description automatically generated

  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

 

VB.NET

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS

 

  1. Use the following code example to copy the Header Footer content from one Word document and insert them into first page header footer of another Word document.
    //Opens the source document 
    WordDocument sourceDocument = new WordDocument(@"Template.docx");
    //Opens the destination document
    WordDocument destinationDocument = new WordDocument(@"Test.docx");
     
    //Merge Header Footer from one document to another document
    MergeHeaderFooter(ref sourceDocument, ref destinationDocument);
     
    //Saves the destination document
    destinationDocument.Save("Output.docx", FormatType.Docx);
    //Closes the document instances
    sourceDocument.Close();
    destinationDocument.Close();
    

 

VB.NET

'Opens the source document 
Dim sourceDocument As WordDocument = New WordDocument("Template.docx")
'Opens the destination document 
Dim destinationDocument As WordDocument = New WordDocument("Test.docx")
 
'Merge Header Footer from one document to another document
MergeHeaderFooter(sourceDocument, destinationDocument)
 
'Saves the destination document
destinationDocument.Save("Output.docx", FormatType.Docx)
'Closes the document instances
sourceDocument.Close()
destinationDocument.Close()

 

  1. Helper method.

C#

public static void MergeHeaderFooter(ref WordDocument sourceDocument, ref WordDocument destinationDocument)
{
      WPageSetup pageSetup = sourceDocument.LastSection.PageSetup;
      //Get the child entities of header and footers in first document.
      EntityCollection firstDocumentHeader = sourceDocument.LastSection.HeadersFooters.Header.ChildEntities;
      EntityCollection firstDocumentFooter = sourceDocument.LastSection.HeadersFooters.Footer.ChildEntities;
 
 
      foreach (WSection section in destinationDocument.Sections)
      {
          section.PageSetup.HeaderDistance = pageSetup.HeaderDistance;
          section.PageSetup.FooterDistance = pageSetup.FooterDistance;
          section.PageSetup.Margins = pageSetup.Margins;
          section.HeadersFooters.Header.ChildEntities.Clear();
          section.HeadersFooters.Footer.ChildEntities.Clear();
          foreach (Entity entity in firstDocumentHeader)
          {
              section.HeadersFooters.Header.ChildEntities.Add(entity.Clone());
          }
          foreach (Entity entity in firstDocumentFooter)
          {
              section.HeadersFooters.Footer.ChildEntities.Add(entity.Clone());
          }
     }
}

 

VB.NET

Public Shared Sub MergeHeaderFooter(ByRef sourceDocument As WordDocument, ByRef destinationDocument As WordDocument)
Dim pageSetup As WPageSetup = sourceDocument.LastSection.PageSetup
'Get the child entities of header and footers in first document.
Dim firstDocumentHeader As EntityCollection = sourceDocument.LastSection.HeadersFooters.Header.ChildEntities
Dim firstDocumentFooter As EntityCollection = sourceDocument.LastSection.HeadersFooters.Footer.ChildEntities
 
     For Each section As WSection In destinationDocument.Sections
         section.PageSetup.HeaderDistance = pageSetup.HeaderDistance
         section.PageSetup.FooterDistance = pageSetup.FooterDistance
         section.PageSetup.Margins = pageSetup.Margins
         section.HeadersFooters.Header.ChildEntities.Clear()
         section.HeadersFooters.Footer.ChildEntities.Clear()
 
         For Each entity As Entity In firstDocumentHeader
             section.HeadersFooters.Header.ChildEntities.Add(entity.Clone())
         Next
 
         For Each entity As Entity In firstDocumentFooter
             section.HeadersFooters.Footer.ChildEntities.Add(entity.Clone())
         Next
    Next
End Sub

 

A complete working sample to find the list of fonts used in the Word document using C# can be downloaded from here

Take a moment to peruse the document 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.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.

 

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