问题
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.
回答1:
This appears to be a bug in MigraDoc up to version 1.50 beta 1.
The bug wasn't fixed yet, but it should require a small modification only and I hope it will be corrected with the next release.
I had to change my mind: it is not a bug, it is a feature.
To achieve what the OP wants you have to assign a PageSetup
to the first section that sets the StartingNumber and also assign a PageSetup
to the second section that does not set the StartingNumber.
If no PageSetup
is set for the second section then this section will inherit the PageSetup
from the first section and the StartingNumber will be applied to both sections. This is the desired behaviour if each section is e.g. a serial letter.
Here is my testcode (just a code snippet):
var sec1 = document.LastSection;
sec1.PageSetup = document.DefaultPageSetup.Clone();
sec1.PageSetup.StartingNumber = 17;
sec1.Footers.Primary = new HeaderFooter();
var para = sec1.Footers.Primary.AddParagraph();
para.AddPageField();
var sec2 = document.AddSection();
sec2.PageSetup = document.DefaultPageSetup.Clone();
The second section inherits the footer from the first section, but a new PageSetup
overrides the StartingNumber.
The Clone()
comes handy here.
来源:https://stackoverflow.com/questions/30203820/page-number-resets-when-new-section-starts-migradoc