Search code examples
c#pdfpdfsharpmigradoc

Adding document link in pdf by Migradoc


I use PdfSharp/Migradoc to generate PDF file in c#, now i tried to add document link inside the pdf file, basically it's to create a table of items, and click the item name to navigate to another detail page. I tried to use the Migradoc Paragraph.AddHyperlink() method, below it's the code used

Paragraph p = cell.AddParagraph();

Hyperlink link = p.AddHyperlink(tmp_value, HyperlinkType.Bookmark);
link.AddText(tmp_value);
link.AddPageRefField(some_bookmark);

My issue is this navigation page is created before the bookmarks are generated later on, after execution, the link is not appeared and only error message like "[item name] the bookmark [some_bookmark] is not defined", anyone can highlight me what's the proper way to acheve this? thanks.


Solution

  • You need something like paragraph.AddBookmark(tmp_value); as the target of the jump. That's what the error message is telling you: target bookmark not defined.

    MigraDoc creates the PDF in two passes and it does not matter where in the document the target is.

    link.AddPageRefField(some_bookmark); will add the page number of the page with the bookmark "some_bookmark". What's the purpose of that?

    See also:
    http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

    Hyperlink hyperlink = paragraph.AddHyperlink("Paragraphs"); 
    hyperlink.AddText("Paragraphs\t"); 
    hyperlink.AddPageRefField("Paragraphs"); 
    

    The first line defines the target - the string defined with AddBookmark elsewhere in the document. The second line gives text that is shown in the link. The third line adds a page number to the link.