How to find and replace a text with hyperlink in Word document?
Syncfusion® DocIO is a .NET Core Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can find and replace a text with hyperlink in Word document using C#.
Steps to find and replace a text with hyperlink in Word document
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your application from NuGet.org.
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.
- Include the following Namespaces in the Program.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
- Add the following code snippet in Program.cs file to find a text and replace with hyperlink in Word document.
using (FileStream fileStreamPath = new FileStream("Data/Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open an existing Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
string textToReplace = "Syncfusion";
//Initialize new text body part.
TextBodyPart textBodyPart = new TextBodyPart(document);
//Initialize new paragraph.
WParagraph paragraph = new WParagraph(document);
//Add the paragraph into the text body part.
textBodyPart.BodyItems.Add(paragraph);
//Append web hyperlink to the paragraph.
paragraph.AppendHyperlink("http://www.syncfusion.com", textToReplace, HyperlinkType.WebLink);
//Replace all entries of given string in the document with text body part.
document.Replace(textToReplace, textBodyPart, false, true);
//Clear the text body part.
textBodyPart.Clear();
//Create file stream.
using (FileStream outputFileStream = new FileStream("Data/Result.docx", FileMode.Create, FileAccess.ReadWrite))
{
//Save the Word document to file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
A complete working sample to find and replace a text with hyperlink in Word document using C# can be downloaded from GitHub.
By executing the program, you will get the 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 , split, and compare documents, find and replace text in the Word document, and most importantly PDF conversion with code examples.
See Also
- How to replace text in a Word document with HTML
- How to find and replace text inside table in Word document
- How to find and replace text in headers and footers of Word document
- How to find and replace placeholder with page break 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
Conclusion
I hope you enjoyed learning how to find and replace a text with Table of Contents (TOC) in Word document.
You can refer to ASP.NET Core control feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our
ASP.NET Core Examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-trac, or
feedback portal. We are always happy to assist you!