PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Mar 29, 2024 3:51 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Adding Table of Contents
PostPosted: Wed Apr 27, 2016 9:33 am 
Offline

Joined: Tue Apr 05, 2016 11:23 am
Posts: 11
I would like to add a Table of Contents to a document I create with MigraDoc. My code looks like this:

Code:
        private void AddRefFields(PageViewModel page, Section sec)
        {
            foreach (var item in page.GetReportSections().Where(s => s != null).OrderBy(reportSection => reportSection.OrderIndex))
            {
                var par = sec.AddParagraph();
                par.AddText(item.SecondaryLangTitle + "\t\t\t\t");
                par.AddPageRefField(item.SecondaryLangTitle);
            }
            foreach (var child in page.Children)
            {
                AddRefFields(child, sec);
            }
        }


The problem is that the pages are not ordered, so I get something like this:

SectionA 123
Section B 89
Section C 249
Section D 152

It seems like it is not possible to access the page numbers in the code, so how can I create an ordered table of contents?


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 27, 2016 9:50 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

PageViewModel is a class of your own, not a MigraDoc class.

If your application adds the content from front to back, then just add each item you want to have in your TOC to a list and use that list to sort the items.

Or use the same loops for creating the TOC as you use for creating the contents. You show a "foreach" with an "OrderBy", but the code that creates the MigraDoc contents may use a different loop/order.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 27, 2016 10:01 am 
Offline

Joined: Tue Apr 05, 2016 11:23 am
Posts: 11
I have found a solution to the original problem but I have the problem now that sometimes my code adds bookmarks which do not exist. How can I check if a bookmark exists?


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 27, 2016 10:14 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Entwickler582 wrote:
How can I check if a bookmark exists?
A MigraDoc Document is a tree. You can walk this tree and search for a bookmark.
You can walk this tree once and compile a list of bookmarks.

Or, as I already suggested, compile the list of bookmarks while you add the contents.

In any case this will work only if you add the table of contents after adding the contents (which is a simple thing with MigraDoc).

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 170 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