void CreatePDF()
{
PdfDocument document = new PdfDocument();
document.Pages.Add();
document.Pages.Add();
AddFooter(document);
document.Save("Sample.pdf");
document.Close(true);
}
void AddFooter(PdfDocument doc)
{
RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
PdfPageTemplateElement footer = new PdfPageTemplateElement(rect);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);
PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);
PdfPageCountField count = new PdfPageCountField(font, brush);
string pageNumberFormat = "Page {0} of {1}";
PointF pageNumberLocation = new PointF(100, 20);
SizeF dimensionOfPageNumber = font.MeasureString(pageNumberFormat);
footer.Graphics.DrawRectangle(PdfBrushes.Aquamarine,
new RectangleF(new PointF(pageNumberLocation.X - 5F, pageNumberLocation.Y), dimensionOfPageNumber));
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, pageNumberFormat, pageNumber, count);
compositeField.Draw(footer.Graphics, pageNumberLocation);
doc.Template.Bottom = footer;
} |
// Creating source document to add footer
PdfDocument doc = new PdfDocument();
for (int pageIndex = 0; pageIndex < 100; pageIndex++)
{
doc.Pages.Add();
}
MemoryStream ms = new MemoryStream();
doc.Save(ms);
ms.Position = 0;
// Add footer
PdfLoadedDocument ldDoc = new PdfLoadedDocument(ms);
for (int pageIndex = 0; pageIndex < ldDoc.PageCount; pageIndex++)
{
// Footer location in the page
PointF footerLocation = new PointF(200, 600);
PdfFont footerFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 35);
string pageNumberText = "Page " + pageIndex.ToString() + " of " + ldDoc.PageCount.ToString();
SizeF footerDimension = footerFont.MeasureString(pageNumberText);
// Footer content goes here
ldDoc.Pages[pageIndex].Graphics
.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(footerLocation, footerDimension));
ldDoc.Pages[pageIndex].Graphics
.DrawString(pageNumberText, footerFont, PdfBrushes.Black, footerLocation);
}
using (var docStream = File.Create("Output.pdf"))
ldDoc.Save(docStream); |