PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Page number resets when new section starts - MigraDoc
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3110
Page 1 of 1

Author:  ankitagarg1019 [ Wed May 13, 2015 12:45 am ]
Post subject:  Page number resets when new section starts - MigraDoc

I am using MigraDoc to create a PDF. As per my requirement, I have to add different headers according to the contents of the page\section. I am able to achieve it by adding new section and defining new header for every section. Till here, it works fine.
In footer, I am showing page numbers using "AddPageField". But I realized that whenever a new section starts, page field reset to 1. Is there any way that footer continues the page count across all sections ?
If I use only one section throughout the document, then page number continues. But in this case, I can not use different headers.

Author:  Thomas Hoevel [ Wed May 13, 2015 7:35 am ]
Post subject:  Re: Page number resets when new section starts - MigraDoc

Hi!
ankitagarg1019 wrote:
Is there any way that footer continues the page count across all sections?
If you do not reset the page count for the new section, the counting just continues. What you want is the default behavior.

So I presume there is an error in your code (which you are not showing).

Author:  ankitagarg1019 [ Thu May 14, 2015 12:49 am ]
Post subject:  Re: Page number resets when new section starts - MigraDoc

Hello Thomas,
Thanks for the reply. No, I am not changing\resetting the page count in new section. But in first section, I added this line.
section.PageSetup.StartingNumber = 2;

Somehow, this line was effecting all the headers. Now, I removed this line and yes, page number continues between sections.
Thanks again. :D

Author:  () => true [ Thu May 14, 2015 8:36 am ]
Post subject:  Re: Page number resets when new section starts - MigraDoc

Hi!

Sections inherit the properties from the previous section.

Remove the line "section.PageSetup.StartingNumber = 2;" and page numbering will continue automatically (but section 1 starts with the wrong page number).

I don't know what you have to specify for section 2 to get continuing page numbers if you set a starting value for section 1.
Next week I'll ask Stefan what he thinks about this.

Author:  Thomas Hoevel [ Thu Jun 25, 2015 1:09 pm ]
Post subject:  Re: Page number resets when new section starts - MigraDoc

Hi!

The mystery is solved:
For section one you set the starting number, maybe like this:
Code:
var sec1 = document.LastSection;
sec1.PageSetup = document.DefaultPageSetup.Clone();
sec1.PageSetup.StartingNumber = 17;


For section two you have to assign the PageSetup, but do not set the starting number. Maybe like this:
Code:
var sec2 = document.AddSection();
sec2.PageSetup = document.DefaultPageSetup.Clone();


If you do not set a PageSetup for the second section, then it will inherit the PageSetup from the first section - and it will have the same starting number.
If you set a PageSetup without a starting number for the second section, then everything will be fine and page numbering for section two continues as expected.

A third section would inherit the page setup from the second section, so there is no need to set the page setup.
And with my test, the second section inherited the footer from the first section. So there is no need to duplicate the code that sets header and footer, just set a clean page setup for the second section.

I am sorry that it took me so long to come up with this rather simple solution.
So it is no bug, it's a feature.

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