Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add hyperlinks into Word docx using open XML?

I am having a trouble adding hyperlinks to my word document. I don't know how to do it. I would like to make a link in a word document from my C# code using open xml. Is ther a different solution using only href or sth similar? There is a HyperLink class on the net from Open XML but how to use it?

like image 335
Antun Tun Avatar asked Apr 18 '13 14:04

Antun Tun


People also ask

How do I add a hyperlink to a DOCX File?

Click the Insert tab, and then click Hyperlink. Click the Web Page or File tab, and then enter an address in the Address box. You can also click Select and browse to the address you want. Click OK.

Why won't my hyperlinks work in Word?

Try selecting just the HYPERLINK field and pressing Shift+F9. On some keyboards, you may have to use FN together with the function keys: Alt+FN+F9 (or Shift+FN+F9).


1 Answers

Try this

using (WordprocessingDocument doc = WordprocessingDocument.Open("", true))
{
    doc.MainDocumentPart.Document.Body.AppendChild(
        new Paragraph(
            new Hyperlink(new Run(new Text("Click here")))
            {
                 Anchor = "Description",
                 DocLocation = "location",
             }
        )
    );

    doc.MainDocumentPart.Document.Save();

}
like image 72
Khurshid Avatar answered Sep 30 '22 19:09

Khurshid