How to Start Convert PDF Read PDF Edit PDF PDF Report Builder Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Report Generator Library
How to add page header and footer to PDF file in C# ASP.NET MVC app


C# Demo Code to add page header and footer to adobe pdf file





In this C# tutorial, you will learn how to add PDF page header and footer contents to a new PDF report file using C# in ASP.NET MVC Web, Windows applications.

  • Create new PDF page header and footer content
  • Apply header and footer to all PDF pages or specified pages





Add a simple header and footer


In the C# source code below, you will know how to add page header and footer quickly to PDF document in C# code

  • Create new PDF file with document opened for editing
  • Create a new Header object with text paragraph content, alignment set
  • Create a new Footer object with text paragraph content, alignment set
  • Create a HeaderFooterOption object to add header and footer to all pages of the PDF document
  • Apply the changes and save the PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample71.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

document.Open();

//  create a header
Header header = new Header();
//  set header content
Paragraph headerContent = new Paragraph("This is a header");
//  set header alignment
headerContent.Alignment = Alignment.ALIGN_CENTER;
header.AddParagraph(0, headerContent);

//  create a footer
Footer footer = new Footer();
//  set footer content
Paragraph footerContent = new Paragraph("This is a footer");
//  set footer alignment
footerContent.Alignment = Alignment.ALIGN_RIGHT;
footer.AddParagraph(0, footerContent);

//  set header and footer to the document
HeaderFooterOption hdrftrOps = new HeaderFooterOption();
hdrftrOps.PageRange = HeaderFooterOption.PageRangeMode.All;
document.SetHeaderFooter(header, footer, hdrftrOps);

document.Add(new Paragraph("This is body field."));

//  add a new page (the 2nd page)
document.NewPage();
//  add a new page (the 3rd page)
document.NewPage();

document.Close();