Description

In this blog you will see how to create Anchor Text Link in word processing document using Open XML SDK 2.0

Assembly

DocumentFormat.OpenXml.dll

Namesapces

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

Code:

C#
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO.Packaging;
 
namespace OpenXMLConsole
{
    class Program
    {
        static void Main(string[] args)
        {         
            string path = @"E:\OpenXMLTable.docx";           
            using (WordprocessingDocument doc = WordprocessingDocument.Open(path,true))
            {
                Body body = doc.MainDocumentPart.Document.Body;
                Paragraph para = body.AppendChild(new Paragraph());
                //// Create an hyperlink
                Hyperlink hyperlink = new Hyperlink();
                //// The anchor attribute specifies that the target of the current hyperlink must be the text contained within the bookmark Description within the document
                //// If no bookmark exists in the current document with the given bookmark name, then the default behavior shall be to navigate to the start of the document
                hyperlink.Anchor = "Description";
                hyperlink.DocLocation = "";
                //// Append the hyperlink to the Paragraph
                para.AppendChild(hyperlink);
                //// Append the run to the hyperlink
                Run run = hyperlink.AppendChild(new Run());
                //// Append the text to the run
                run.AppendChild(new Text("Click on description for more details"));
                //// Save the document
                doc.MainDocumentPart.Document.Save();               
            }
        }
    }

XML:

HTML / XML
 <w:hyperlink w:anchor="Description">
  <w:r>
    <w:t>Click on description for more details</w:t>
  </w:r>
</w:hyperlink>