Add a header and footer image to all pages

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Enhancement >

Add a header and footer image to all pages

In this section we present example code to add a header and footer image to all pages of an existing PDF document. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

 

Note that pages in the PDF format does not have a notion of headers or footers.

 

Given below is the sample code.

 

string inputPdfFile = @"input.pdf";
string headerImage = @"header_logo.jpg";
string footerImage = @"footer_logo.jpg";
using (Gnostice.Documents.PDF.PDF pdfDoc = new Gnostice.Documents.PDF.PDF())
{
   // Register callback function to supply the header/footer size
   pdfDoc.OnPageLoad += PdDoc_OnPageLoad;
   pdfDoc.OpenAfterCreate = true;
   pdfDoc.LoadDocument(inputPdfFile);
   
   // Add header and footer to all the pages in the PDF
   string pageRange = "1-" + pdfDoc.GetPageCount().ToString();
 
   // Add header image centered in the allocated header space
   pdfDoc.AddHeaderImage(headerImage, Gnostice.Documents.PDF.PDFHAlignment.Center, Gnostice.Documents.PDF.PDFVAlignment.Center, pageRange);
 
   // Add footer image centered in the allocated header space
   pdfDoc.AddFooterImage(footerImage, Gnostice.Documents.PDF.PDFHAlignment.Center, Gnostice.Documents.PDF.PDFVAlignment.Center, pageRange);
 
   pdfDoc.Save(@"HeaderFooterImageAdded.pdf");
   pdfDoc.CloseDocument();
}
 
// Implementation of the callback function
private static void PdDoc_OnPageLoad(object sender, int pageNumber, float pageWidthInPoints, float pageHeightInPoints, ref Gnostice.Documents.PDF.PageMargins margins)
{
   // Set the height in points of header/footer to allocate
   margins.headerHeight = 1;
   margins.footerHeight = 1;
}