PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

AddPageRefField and AddHyperLink Question
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4423
Page 1 of 1

Author:  jorgz [ Tue Feb 28, 2023 10:25 am ]
Post subject:  AddPageRefField and AddHyperLink Question

Hello,

I want to solve an issue with a table of contents that is generated with migradoc and then rendered to pdf. In short this is how the table of content is generated:
Code:
            Section section = document.LastSection;

            // add table of contents and add it
            var paragraph = section.AddParagraph(ToCBookmark, "Heading1");
            paragraph.AddBookmark(ToCBookmark);

            paragraph = section.AddParagraph();
            paragraph.Style = "TOC";
            Hyperlink hyperlink = paragraph.AddHyperlink(CoverBookmark);
            hyperlink.AddText(CoverBookmark + "\t");
            hyperlink.AddPageRefField(CoverBookmark);

            paragraph = section.AddParagraph();
            paragraph.Style = "TOC";
            hyperlink = paragraph.AddHyperlink(SummaryBookmark);
            hyperlink.AddText(SummaryBookmark + "\t");
            hyperlink.AddPageRefField(SummaryBookmark);

            paragraph = section.AddParagraph();
            paragraph.Style = "TOC";
            hyperlink = paragraph.AddHyperlink(ToCBookmark);
            hyperlink.AddText(ToCBookmark + "\t");
            hyperlink.AddPageRefField(ToCBookmark);

            paragraph = section.AddParagraph();
            paragraph.Style = "TOC";
            hyperlink = paragraph.AddHyperlink(DetailedReportBookmark);
            hyperlink.AddText(DetailedReportBookmark + "\t");
            hyperlink.AddPageRefField(DetailedReportBookmark);

            if (report.Root != null)
                ReportTableOfContentsR(report.Root, document, 0);
        }


The recurse funtion the bookmark and page number are added:
Code:
                // create a empty paragraph and add a hyperlink
                var paragraph = section.AddParagraph();
                paragraph.Style = "TOC";
                paragraph.Format.LeftIndent = Unit.FromCentimeter((level + 1) * ToCIndent);
                var hyperlink = paragraph.AddHyperlink(bookmarkStr);

                // add text with name of bookmark
                hyperlink.AddText(String.Format("{0} [", bookmarkStr));

                // add a colored status tag to each element
                var text = hyperlink.AddFormattedText(statusStr);
                text.Font.Bold = true;
                text.Font.Color = statusColor;

                // finish the ToC element with a tab to create dots
                hyperlink.AddText("]\t");

                // add the reference for the bookmark
                hyperlink.AddPageRefField(bookmarkStr);
 

and continue recursing on ReportTableOfContentsR. The issue is that on the last line:
Code:
hyperlink.AddPageRefField(bookmarkStr);

The page number is always the page number of page:
Code:
hyperlink.AddPageRefField(DetailedReportBookmark);

Also the links also point to that page bookmark.

The correct bookmarks are added like this:
Code:
       var bookmark = row.Cells[0].AddParagraph(bookmarkStr);
                bookmark.Style = "HiddenBookmark";
                bookmark.Format.OutlineLevel = outLevel;
                bookmark.AddBookmark(bookmarkStr);

On the pdf reader I can see the bookmarks but on the TOC links an page numbers are of DetailedReportBookmark
for hints where the bug is, thanks!

Author:  TH-Soft [ Tue Feb 28, 2023 10:42 am ]
Post subject:  Re: AddPageRefField and AddHyperLink Question

I only see code fragments. I do not see where variable "bookmarkStr" is set, I do not see where the variable is used to create a bookmark.

http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

Author:  jorgz [ Tue Feb 28, 2023 11:03 am ]
Post subject:  Re: AddPageRefField and AddHyperLink Question

Removed

Author:  jorgz [ Tue Feb 28, 2023 2:00 pm ]
Post subject:  Re: AddPageRefField and AddHyperLink Question

I removed the post, the first post is enough to expose what the doubt was.

Author:  TH-Soft [ Tue Feb 28, 2023 4:04 pm ]
Post subject:  Re: AddPageRefField and AddHyperLink Question

Maybe it helps you to look at the MDDDL file saved before rendering the document.
See also:
http://pdfsharp.net/wiki/MigraDocDDL.ashx

Author:  jorgz [ Wed Mar 01, 2023 9:38 am ]
Post subject:  Re: AddPageRefField and AddHyperLink Question

So the solution was to add the bookmarks outside the cell table, only then the index bookmark took the correct page number on the TOC and also did the jump to the place the bookmark was added.

I will no post the solution code but as I said, do section.AddParagraph (in the place of the table where the jump should be made) then to that paragraph add the bookmark and make it hidden, then start a new table and since the bookmark is hidden the table looks continuous and the jumps from the index work.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/