PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 10:29 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
PostPosted: Fri Nov 30, 2018 6:00 am 
Offline

Joined: Wed Nov 21, 2018 5:48 pm
Posts: 13
I'm using MigraDoc to build PDF documents. I want to mark certain areas of the document to add to the outline later. The MigraDoc API requires a PdfPage for the outline, but the pages aren't there until the document is rendered. After the document is rendered, I need to find the pages that have the bookmarks and add just those pages to the outline.

One of the responses on StackOverflow uses BookMarks, but I don't always have something I can add a bookmark too. For example, pages with only images. Another way mentioned was to add some tiny invisible text. Is there a generic way I can place identifiers in the document and find the pages later? I suppose I could add bookmarks to empty paragraphs, then later on find what page those bookmarks are on and add an Outline entries for those pages.

This is the idea, but I couldn't get it to work:
https://stackoverflow.com/questions/294 ... 3#29497223

Thanks,
Brian


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 30, 2018 7:42 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Each MigraDoc document object has a Tag member that you can use to tag the element.

To have MigraDoc create the outlines, you can use "invisible" paragraphs - no tagging needed.

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 30, 2018 3:56 pm 
Offline

Joined: Wed Nov 21, 2018 5:48 pm
Posts: 13
If I tag an element, how do I go back later and find what page it is on?

If I go the invisible text route, how would you recommend making paragraphs "invisible" so they don't affect the layout? Also, even though the text is not visible in the document, it will still be visible in the outline?

I believe I have somethign that works. I did this and the text is not shown and the layout seems to be the same as if the text wasn't even there. I am adding the code here to complete this post so if someone finds it later than can see the whole thing:

Code:
Style style = pdfDoc.Styles["Heading1"];  // will be used for the root entry of the outline
style.Font.Size = .001;
style.Font.Color = Colors.White;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 0;
style.ParagraphFormat.SpaceAfter = 0;

style = pdfDoc.Styles["Heading2"];    // will be used for the second level of the outline
style.Font.Size = .001;
style.Font.Color = Colors.White;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 0;
style.ParagraphFormat.SpaceAfter = 0;


So to add the root to the outline, I do:
section.AddParagraph("Outline Root", "Heading1");

and to add second level outline entries I do:
section.AddParagraph("Specifications", "Heading2");


Top
 Profile  
Reply with quote  
PostPosted: Sat Dec 01, 2018 8:25 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
That is more or less the trick I used.

You can invoke "Clone()" to create copies of "Heading1" under a different name, so you can still have visible headings on other pages.

To find which objects are on a specific page, use code like "docRenderer.GetDocumentObjectsFromPage(idx + 1)".
See also:
http://forum.pdfsharp.net/viewtopic.php?p=1904#p1904
Note: with PDFsharp 1.50 you no longer need the Internalsvisibleto trick, all you need is public.

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


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 159 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