|
<< Click to Display Table of Contents >> Navigation: Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Creation > Create PDF with a header and footer |
In this section we present example code to create a new PDF document containing a header and footer. Note that pages in the PDF format does not have a notion of header or footer.
Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.
Given below is the sample code.
// Create a new PDF document
using (var pdfDoc = new PDF())
{
// Create a new PDF page with specified header and footer heights and margins
var pdfPage = new PDFPage(
pageSize: PaperKind.A4,
pageHeaderHeight: 2, pageFooterHeight: 2,
pageLeftMargin: 3, pageTopMargin: 2, pageRightMargin: 1, pageBottomMargin: 2,
measurementUnit: MeasurementUnit.Centimeters);
// Add an image to the header
pdfPage.AddHeaderImage(imgPath: @"header_logo.png", horizPosition: PDFHAlignment.Center, vertiPosition: PDFVAlignment.Center);
// Add formatted text to the header
pdfPage.AddHeaderText(text: "This is header Text",font: new PDFFont(StdType1Font.Helvetica, 12), horizPosition: PDFHAlignment.Left, vertiPosition: PDFVAlignment.Top);
// Add an image to the footer
pdfPage.AddFooterImage(imgPath: @"footer_logo.png", horizPosition: PDFHAlignment.Center, vertiPosition: PDFVAlignment.Center);
// Add formatted text to the footer
pdfPage.AddFooterText(text: "This is footer Text", font: new PDFFont(StdType1Font.Helvetica, 12), horizPosition: PDFHAlignment.Left, vertiPosition: PDFVAlignment.Top);
// Add the page to the document and save it
pdfDoc.AddPage(pdfPage);
pdfDoc.Save("Headers_Footers.pdf");
}