Articles in this section
Category / Section

How to find and replace text in headers and footers of Word document?

3 mins read

Syncfusion® Essential® DocIO is a .NET Word library used to create, read, and edit Word  documents programmatically without Microsoft or interop dependencies. Using this library, you can find and replace text in the headers and footers of a Word document in C# and VB.NET.

Find and replace text in headers and footers of a Word document

  1. Create a new C# console application project.

Create .NET Framework console application in Visual Studio

  1. Install Syncfusion.DocIO.WinForms NuGet packages as a reference to your .NET Framework applications from  NuGet.org.

Add DocIO NuGet packages of WinForms

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

C#

using Syncfusion.DocIO.DLS; 

VB

Imports Syncfusion.DocIO.DLS
  1. Use the following code snippet to find and replace text in the headers and footers of a Word document.
// Load the template document.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Input.docx"), FormatType.Docx))
{
    // Create a paragraph for the header.
    WParagraph headerParagraph = new WParagraph(document);
    // Align the paragraph horizontally to the right.
    headerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right;
    // Append a picture in the paragraph.
    WPicture picture = headerParagraph.AppendPicture(Image.FromFile(@"../../AdventureCycle.jpg")) as WPicture;
    // Set width and height for the picture.
    picture.Height = 65;
    picture.Width = 200;
    // Create a text body part.
    TextBodyPart headerBodyPart = new TextBodyPart(document);
    headerBodyPart.BodyItems.Add(headerParagraph);
    // Replace all entries of a given regular expression with the text body part along with its formatting in the header.
    document.Replace(new Regex("^<<(.*)>>"), headerBodyPart, false);

 
    // Create a paragraph for the footer.
    WParagraph footerParagraph = new WParagraph(document);
    // Align the paragraph horizontally to the right.
    footerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right;
    // Add the text.
    footerParagraph.AppendText(" Page ");
    // Add a page number field.
    footerParagraph.AppendField("CurrentPageNumber", FieldType.FieldPage);
    // Add the text.
    footerParagraph.AppendText(" of ");
    // Add a number of pages field.
    footerParagraph.AppendField("TotalNumberOfPages", FieldType.FieldNumPages);
    // Create a text body part.
    TextBodyPart footerBodyPart = new TextBodyPart(document);
    footerBodyPart.BodyItems.Add(footerParagraph);
    // Replace all entries of a given regular expression with the text body part along with its formatting in the footer.
    document.Replace(new Regex("^//(.*)"), footerBodyPart, false);
    // Save the document.
    document.Save(Path.GetFullPath("Sample.docx"), FormatType.Docx);
}

VB.NET

' Load the template document.
Using Dim document As New WordDocument("../../Input.docx", FormatType.Docx)
' Create a paragraph for the header.
Dim headerParagraph As New WParagraph(document)
' Align the paragraph horizontally to the right.
headerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right
Dim picture As WPicture = CType(headerParagraph.AppendPicture(Image.FromFile("AdventureCycle.jpg")), WPicture)
' Set width and height for the picture.
picture.Height = 65.0!
picture.Width = 200.0!
' Create a text body part.
Dim headerBodyPart As New TextBodyPart(document)
headerBodyPart.BodyItems.Add(headerParagraph)
' Replace all entries of a given regular expression with the text body part along with its formatting in the header.
document.Replace(New Regex("^<<(.*)>>"), headerBodyPart, False)

' Create a paragraph for the footer.
Dim footerParagraph As New WParagraph(document)
' Align the paragraph horizontally to the right.
footerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right
' Add the text.
footerParagraph.AppendText(" Page ")
' Add a page number field.
footerParagraph.AppendField("CurrentPageNumber", FieldType.FieldPage)
' Add the text.
footerParagraph.AppendText(" of ")
' Add a number of pages field.
footerParagraph.AppendField("TotalNumberOfPages", FieldType.FieldNumPages)
' Create a text body part.
Dim footerBodyPart As New TextBodyPart(document)
footerBodyPart.BodyItems.Add(footerParagraph)
' Replace all entries of a given regular expression with the text body part along with its formatting in the footer.
document.Replace(New Regex("^//(.*)"), footerBodyPart, False)
' Save the document.
document.Save("Sample.docx", FormatType.Docx)
End Using

 

A complete working example to find and replace text in headers and footers of Word document using C# can be downloaded from GitHub.

By executing the application, you will get the output Word document as follows. 

Replaced Word document in C# using DocIO

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion® Word Framework features.

An online example to find and replace in a Word document.

See Also:

How to replace particular text with a hyperlink in a Word document

How to find and replace text inside a table in a Word document

How to find and replace text in headers and footers of a Word document

How to find and replace a placeholder with a page break in a Word document

How to find and replace a line break in a Word document as a paragraph mark?

How to find and replace text with a content control in a Word document?

How to find and replace text in a Word document with a text from a database?

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a 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 a trial message.

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