I am attempting to add a right-aligned section page number to the footer of a pdf document I am creating from scratch. From the sample center and online tutorials, I was able to add a page number, and it looks as desired on pages 1-9, eg. “Page 1 of 10”, but on page ten, the page number appears as “Page 10 of 1”. When I modify the location of the field to elsewhere in the footer, the text appears as expected.
Of note, when I set the x value of the field’s location to 0, it appears as left aligned in the footer, even with the left side of the table generated, despite the StringFormat property specifying that the text should be right-aligned. I tried changing the property to left alignment and it had no effect on the resulting pdf.
Elsewhere, I am writing to the header and footer with Graphics.DrawString() instead of PdfCompositeField.Draw(), and it is functioning as expected with both left and center aligned text; it is only PdfCompisiteField.Draw() that I can’t get working correctly.
Also, in the sample code below, the section template widths do not match the width of the templates in the sample center or online tutorials. Setting the template.Bottom width to the full page width caused the page number to be written to the right of the printable space and not to appear.
I am using version 6.7.8 of the dlls. The Sample Center lists version 6.1.4, but all the relevant code is the same as in the online tutorials.
Relevant code follows:
public MemoryStream GetReport(Dictionary<string, DataTable> RptTables)
{
PdfDocument doc = new PdfDocument();
PdfUnitConvertor unitConvert = new PdfUnitConvertor();
PdfBrush FooterBrush = PdfBrushes.Gray;
PdfPen FooterPen = new PdfPen(FooterBrush, 0.75f);
PdfTrueTypeFont FooterFont = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);
PdfStringFormat FooterFmtRight = new PdfStringFormat(PdfTextAlignment.Right);
PdfStringFormat FooterFmtLeft = new PdfStringFormat(PdfTextAlignment.Left);
PdfTrueTypeFont HeaderFont = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
PdfStringFormat HeaderFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Top);
PdfBrush HeaderBrush = PdfBrushes.Black;
float FooterBuffer = FooterFont.Height * 0.75f;
float HeaderBuffer = HeaderFont.Height * 0.75f;
float FooterOffset = FooterBuffer + FooterFont.Height;
float HeaderOffset = HeaderBuffer + HeaderFont.Height;
PdfMargins margins = new PdfMargins()
{
Top = unitConvert.ConvertUnits(.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point) + HeaderOffset,
Bottom = unitConvert.ConvertUnits(.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point) + FooterOffset,
Left = unitConvert.ConvertUnits(.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point),
Right = unitConvert.ConvertUnits(.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
};
PdfPageSettings pageSettings = new PdfPageSettings();
pageSettings.Margins = margins;
pageSettings.Size = PdfPageSize.Letter;
pageSettings.Orientation = PdfPageOrientation.Portrait;
doc.PageSettings = pageSettings;
foreach (var kvp in RptTables)
{
PdfSection section = doc.Sections.Add(pageSettings);
section.Template.Top = new PdfPageTemplateElement(PdfPageSize.A4.Width - margins.Left - margins.Right, HeaderOffset);
section.Template.Bottom = new PdfPageTemplateElement(PdfPageSize.A4.Width - margins.Left - margins.Right, FooterOffset);
PdfSectionPageNumberField pn = new PdfSectionPageNumberField();
PdfSectionPageCountField pc = new PdfSectionPageCountField();
PdfCompositeField cf = new PdfCompositeField();
cf.AutomaticFields = new PdfAutomaticField[] { pn, pc };
cf.Text = "Page {0} of {1}";
cf.Font = FooterFont;
cf.Brush = FooterBrush;
cf.Location = new PointF(section.Template.Bottom.Width - margins.Right, FooterBuffer);
cf.StringFormat = FooterFmtRight;
cf.Draw(section.Template.Bottom.Graphics);
string Title = _config.Title;
if ((kvp.Key ?? "") != "")
{
section.Template.Bottom.Graphics.DrawString(kvp.Key, FooterFont, FooterBrush, 0, FooterBuffer, FooterFmtLeft);
Title += " - " + kvp.Key;
}
section.Template.Top.Graphics.DrawString(Title, HeaderFont, HeaderBrush, section.Template.Top.Width / 2, 0, HeaderFormat);
DrawPDFTable(section.Pages.Add(), kvp.Value);
}
#if DEBUG
int pgMax = 10;
while (doc.Pages.Count > pgMax)
doc.Pages.RemoveAt(doc.Pages.Count - 1);
#endif
MemoryStream Ret = new MemoryStream();
doc.SaveToStream(Ret, FileFormat.PDF);
return Ret;
}