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
- Create a new C# console application project.
- Install Syncfusion.DocIO.WinForms NuGet packages as reference to your .NET Framework applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO.DLS;
VB
Imports Syncfusion.DocIO.DLS
- 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.
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?
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.