PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Sep 17, 2021 1:35 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Mar 19, 2021 8:45 am 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 45
Hello,

I need to add pagination to a pdf only if it has more than one page,

I know that MigraDoc does not have a concept of pages until the render of the document, but, is there a way to do what I need to do?

I already add pages to the header, I just need to know if theres a way to add pagination only if the document has more than one page.

Thanks in advance.


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 19, 2021 1:32 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 659
Location: CCAA
You can have pagination that starts on the second page. Maybe that is enough for your case.

Otherwise, start with the most likely case (e.g. with pagination) and prepare the document. If it has at least two pages then render it.
If it only has a single page, remove the pagination and prepare again, then render it.
If you print e.g. invoices, then maybe you make a good guess based on the number of records used for the document.

Or render the document without pagination and add the pagination later as needed with PDFsharp. You can render in a for loop and add the pagination before saving the document. This way there is no overhead with respect to CPU load.
Rendering in a loop is shown here:
viewtopic.php?f=8&t=3172

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 22, 2021 9:54 am 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 45
Hello,

What I ended up doing was exactly that,

I prepare the documento check the current number of pages, and remove the elements on the cell that has the pagination (looping through the objects in the primary footer).
I find the cell by using the Tag property.

So what I did, step by step, in case someone needs it is:
1- Start generating the pdf;

2- When I need to make the footer (where I have my pagination), I add a Tag with a string "draws_pagination_if_one_page" to the cell that contains pagination (Since I use tables in the footer).

3- Generate the rest of the pdf.

4- Before rendering and saving it, I use the PrepareDocument() method like this:
Code:
// mDocument is my MigraDoc document
DocumentRenderer _renderer = new DocumentRenderer(mDocument);
_renderer.PrepareDocument();


this way I can get the page count like this:
Code:
int pageCount = _renderer.FormattedDocument.PageCount;


5- since I have the page count and the document is not rendered I can still make changes to it.
So I loop through the ((Section)mDocument.Sections.First).Footers.Primary.Elements.
until I find the Cell where
Code:
Tag?.ToString() == "draws_pagination_if_one_page"
if exists

6- After and if I find the cell I just do
Code:
// Note that here I'm doing it on the first section's Footer, since thats the only footer configuration I use,
// if you use other types of footers you'll need to adjust the solution for your needs
((Table)((Section)mDocument.Sections.First).Footers.Primary.Elements[i]).Rows[j].Cells[l].Elements.Clear();



7- Then I just render and save the document.

The way that Thomas adviced, this is, adding the pagination during the render on the for loop will be more performant, but this, at least for my needs, is enough.


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

All times are UTC


Who is online

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