How to Start Convert PDF Read PDF Build PDF 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 Editor Library
How to add, edit PDF file page header, footer using c# .net


An Excellent PDF Control Allows C# Developers to add or delete header/footer/page number to PDF File in C#.NET









  • Professional PDF SDK for Visual Studio .NET, which able to add header, footer, page number in C#.NET class
  • Advanced PDF edit control and component for modify header, footer, page number in both C#.NET WinForms
  • Free online sample code for quick evaluation in Visual C#.NET framework for PDF page header, footer
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • Easy to add/remove header, footer to/from PDF page online in browser in ASP.NET web project
  • Support to copy a PDF page header, footer to another PDF file page in .NET framework
  • Support to add different headers/footers to PDF file.




C#.NET:Define Header and Footer regions



Using XDoc.PDF library, the region of header and footer is defined through Page Margins as follows:

  1. Header : The area surrounded by Left, Right and Top Margin.
  2. Footer : The area surrounded by Left, Right and Bottom Margin.


The header/footer region can be devided into 3 PDFPageTextField (Left/Center/Right).
You can define text font and text color through PDFPageTextField object.
The region and location of header/footer are as follows:





Add Header & Footer to PDF File using C#


You can add, insert fixed text message (such as document title, author name etc.) to page header & footer fields , you can also add message with one or more dynamic field tokens. It supports two dynamic field tokens: automatic page number, and date.

A dynamic field token must be in form "<<***>>".

For Automatic Page Number

  1. <<1>> : Page number only.
  2. <<1/n>> : Page number and total page count, with separator '/'.


For Date

  1. <<m/d>> : Month and day, with separator '/'.
  2. <<m/d/yy>> : Month, day and year, with separator '/'.
  3. <<m/d/yyyy>> : Month, day and year, with separator '/'.
  4. <<mm/dd/yy>> : Month, day and year, with separator '/'.
  5. <<mm/dd/yyyy>> : Month, day and year, with separator '/'.
  6. <<d/m/yy>> : Day, month and year, with separator '/'.
  7. <<d/m/yyyy>> : Day, month and year, with separator '/'.
  8. <<dd/mm/yy>> : Day, month and year, with separator '/'.
  9. <<dd/mm/yyyy>> : Day, month and year, with separator '/'.
  10. <<mm/yy>> : Month and year, with separator '/'.
  11. <<mm/yyyy>> : Month and year, with separator '/'.
  12. <<m.d.yy>> : Month, day and year, with separator '.'.
  13. <<m.d.yyyy>> : Month, day and year, with separator '.'.
  14. <<mm.dd.yy>> : Month, day and year, with separator '.'.
  15. <<mm.dd.yyyy>> : Month, day and year, with separator '.'.
  16. <<mm.yy>> : Month and year, with separator '.'.
  17. <<mm.yyyy>> : Month and year, with separator '.'.
  18. <<d.m.yy>> : Day, month and year, with separator '.'.
  19. <<d.m.yyyy>> : Day, month and year, with separator '.'.
  20. <<dd.mm.yy>> : Day, month and year, with separator '.'.
  21. <<dd.mm.yyyy>> : Day, month and year, with separator '.'.
  22. <<yy-mm-dd>> : Year, month and day, with separator '-'.
  23. <<yyyy-mm-dd>> : Year, month and day, with separator '-'.


The following demo code will explain how to add header or footer to PDF file using C#.



String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_hdrftr.pdf";

//  open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);

{
    //  define a header/footer setting
    PDFPageHeaderFooter hdrftr1 = new PDFPageHeaderFooter();
    //  set center header field
    hdrftr1.CenterHeaderField.Set(@"Title: *****", new Font("Arial", 12F, FontStyle.Regular), Color.Black);
    //  set left footer field
    hdrftr1.LeftFooterField.Set("Page <<1/n>>", new Font("Arial", 9F, FontStyle.Regular), Color.DarkGray);

    //  define page range: all odd pages
    PageRangeOptions pageRange1 = new PageRangeOptions();
    pageRange1.AllPages = true;
    pageRange1.Subset = PageRangeSubset.Odd;

    //  apply header/footer settings to all odd pages
    PDFPageFieldHandler.ApplyHeaderFooter(doc, hdrftr1, pageRange1);
}
{
    //  define a header/footer setting
    PDFPageHeaderFooter hdrftr2 = new PDFPageHeaderFooter();
    //  set center header field
    hdrftr2.CenterHeaderField.Set(@"Title: *****", new Font("Arial", 12F, FontStyle.Regular), Color.Black);
    //  set right footer field
    hdrftr2.RightFooterField.Set("Page <<1/n>>", new Font("Arial", 9F, FontStyle.Regular), Color.DarkGray);

    //  define page range: all even pages
    PageRangeOptions pageRange2 = new PageRangeOptions();
    pageRange2.AllPages = true;
    pageRange2.Subset = PageRangeSubset.Even;

    //  apply header/footer settings to all even pages
    PDFPageFieldHandler.ApplyHeaderFooter(doc, hdrftr2, pageRange2);
}

doc.Save(outputFilePath);




Remove all Page Header/Footer settings in a PDF document object using C#


String inputFilePath = Program.RootPath + "\\" + "1_hdrftr.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
PDFPageFieldHandler.RemoveHeaderFooters(doc);
doc.Save(outputFilePath);




Retrieve all Page Header/Footer settings from a PDF file using C#


String inputFilePath = Program.RootPath + "\\" + "1_hdrftr.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);

//  get all Header/Footer settings in the document
PDFPageHeaderFooterInfo info = PDFPageFieldHandler.RetreiveHeaderFooters(doc);

//  get default setting for Header/Footer and Page Range
PDFPageHeaderFooter defaultSetting = info.GetDefaultSetting();
PageRangeOptions defaultPageRange = info.GetDefaultPageRangeSettings();