We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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


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 headers and footers of Word document in C# and VB.NET.

Find and replace text in headers and footers of 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 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 headers and footers of Word document.

C#

//Load the template document.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Input.docx"), FormatType.Docx))
{
    //Create paragraph for header.
    WParagraph headerParagraph = new WParagraph(document);
    //Create paragraph horizontally to the right.
    headerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right;
    //Append 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 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 header.
    document.Replace(new Regex("^<<(.*)>>"), headerBodyPart, false);
 
 
    //Create paragraph for 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 page number field.
    footerParagraph.AppendField(" CurrentPageNumber", FieldType.FieldPage);
    //Add the text.
    footerParagraph.AppendText(" of ");
    //Add number of page field.
    footerParagraph.AppendField(" TotalNumberOfPages ", FieldType.FieldNumPages);
    //Create 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 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 paragraph for 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 paragraph.
picture.Height = 65.0!
picture.Width = 200.0!
'Create 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 header.
document.Replace(New Regex("^<<(.*)>>"), headerBodyPart, False)
 
' Create paragraph for 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 page number field.
footerParagraph.AppendField(" CurrentPageNumber", FieldType.FieldPage)
'Add the text.
footerParagraph.AppendText(" of ")
'Add number of page field.
footerParagraph.AppendField(" TotalNumberOfPages ", FieldType.FieldNumPages)
'Create 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 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 Word document.

See Also:

How to replace the particular text with hyperlink in Word document

How to find and replace text inside table in Word document

How to replace text in a Word document with HTML

How to find and replace text with page break in Word document?

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

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

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

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 link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile