This step-by-step tutorial guides you on how to insert hyperlink in Word using C#. It provides complete steps to insert normal text as well as a hyperlink pointing to a URL with the help of a few lines of code only. It assists to add hyperlink in Word using C# such that complete control is available for customizing the hyperlink text and saving the output file as a DOC or DOCX file.
Steps to Insert Link in Word using C#
- Establish the development environment to use Aspose.Words for .NET to add a hyperlink
- Create a new file using the Document class and put some text using the DocumentBuilder object
- Set the formatting for inserting the hyperlink text
- Insert a hyperlink, the target URL, and a flag for the URL
- Clear the formatting and add some normal text again
- Save the Word file having a hyperlink in it
These steps summarize the process of how to add link in Word using C#. It contains detailed steps to create a Word file, insert normal text in it using the DocumentBuilder class, and then set the formatting to insert some text as a hyperlink along with linking it to a URL. Once the hyperlink is added, the formatting is cleared, and then again normal text is added to the Word file before saving it on the disk.
Code to Insert Hyperlink in Word using C#
This code demonstrates to add hyperlink to Word document using C# where the DocumentBuilder class object is used to insert normal text as well as a formatted text in the form of a hyperlink. Once you change the formatting like font color and other properties, all the text added to the document after it will be formatted with the same formatting. In the end, after adding the hyperlink using the InsertHyperlink() method the ClearFormatting() method in the Font class is called to insert normal text again in the document if required.
This article has taught us how to add hyperlink in Word using C#. If you want to learn the process to add a bookmark, refer to the article on how to add bookmark in Word using C#.