PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Mar 27, 2024 5:05 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Feb 28, 2023 10:25 am 
Offline

Joined: Mon Feb 27, 2023 2:07 pm
Posts: 6
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!


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 28, 2023 10:42 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 28, 2023 11:03 am 
Offline

Joined: Mon Feb 27, 2023 2:07 pm
Posts: 6
Removed


Last edited by jorgz on Sat Mar 04, 2023 7:14 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 28, 2023 2:00 pm 
Offline

Joined: Mon Feb 27, 2023 2:07 pm
Posts: 6
I removed the post, the first post is enough to expose what the doubt was.


Last edited by jorgz on Sat Mar 04, 2023 7:14 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 28, 2023 4:04 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Maybe it helps you to look at the MDDDL file saved before rendering the document.
See also:
http://pdfsharp.net/wiki/MigraDocDDL.ashx

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 01, 2023 9:38 am 
Offline

Joined: Mon Feb 27, 2023 2:07 pm
Posts: 6
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group