C# DocX : add internal link

769 Views Asked by At

In the DocX library available at : https://github.com/WordDocX/DocX

It is possible to add some hyperlink but i haven't found a way to add internal link.

Does anyone know how to add a link to specific paragraph or to a bookmark ?

2

There are 2 best solutions below

1
REDMAN On BEST ANSWER

The way I've found is:

synthesisDocument.AddHyperlink("Link",new Uri("file:///path/to/doc/file.doc#MY_BOOKMARK")); synthesisDocument.Paragraphs[0].InsertHyperl‌​ink(h)

That way resolve your problem, but only in doc format, when you export to PDF it doesn't work. I hope it helps

0
Nick Smart On

As mentioned #BookMark does work if the Uri is created with the UriKind.Relative flag:

 var uri = new Uri("#" + BookMark,UriKind.Relative);
 var hyperLink =  doc.AddHyperlink(textToDisplay, uri  );

Now to use the hyperlink on a paragraph, p:

 p.InsertHyperlink(hyperLink,indexToInsertAt);

I achieved exactly the result I wanted using this in both .docx and .pdf

Hope it works for you,

Nick