XDoc.PDF
Features
Tech Specs
How-to C#
How-to VB.NET
Pricing
C# PDF: How to PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
Page: Add/Remove Header/Footer/Page Number to PDF file
  |  
Home ›› XDoc.PDF ›› C# PDF: Add/Remove Header/Footer/Page Number

C#.NET PDF Library - Add/Remove Header/Footer to PDF Page


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



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


Header: The area surrounded by Left, Right and Top Margin.


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 color through PDFPageTextField object.


The region and location of header/footer are as follows:



The following demo codes will show you how set margins to create Header/Footer regions.



//Create a Header/Footer object. 
PDFPageHeaderFooter hdrftr = new  PDFPageHeaderFooter();
//Define Margins to create a Header/Footer region, Unit: inch.
hdrftr.LeftMargin = 0.5F;
hdrftr.RightMargin = 0.5F;
hdrftr.TopMargin = 1.0F;
hdrftr.BottomMargin = 1.0F;

//Set the properties of Text Fields
//Set LeftHeader Field
hdrftr.LeftHeaderField.TextFont = new Font("Arial", 12F, FontStyle.Regular);
hdrftr.LeftHeaderField.TextColor = Color.Black;
hdrftr.LeftHeaderField.SetContent("This is a Header field");

//Set other fields
//hdrftr.CenterHeaderField...
//hdrftr.RightHeaderField...
//hdrftr.LeftFooterField...
//hdrftr.CenterFooterField...
//hdrftr.RightFooterField...



If you want to add header/footer to some pages not all pages, you can complete the task through the PageRangeOptions object.


All Pages: 
PageRangeOptions ops = new  PageRangeOptions();
ops.AllPages = true;

From Third to Sixth Pages: 
PageRangeOptions ops = new  PageRangeOptions();
ops.SetPageNumberRange(3, 6);

All the Even pages from Third to Sixth pages(That is 4th, 6th page): 
PageRangeOptions ops = new  PageRangeOptions();
ops.SetPageNumberRange(3, 6);
ops.Subset = PageRangeSubset.Even;

Get page index array or page number array: 
PageRangeOptions ops = new  PageRangeOptions();
// ...;

int totalPageCount = 20;
int[] pageIndexes = PageRangeOptions.CalculatePageIndexes(ops, totalPageCount);
int[] pageNumbers = PageRangeOptions.CalculatePageNumbers(ops, totalPageCount);



Note:

       a. When adding Header/Footer to page, it will cover current header/footer settings, the document will record the value set last time.

       

       b. When deleting Header/Footer, it will delete total header/footer, you can't delete header/footer partially.

       

       c. When adding Header/Footer to page, it will be better to delete the recorded settings, otherwise the document will apply new and old settings at the same time.




C#.NET Sample Code: Add Header & Footer to PDF File



Add necessary references:


  RasterEdge.Imaging.Basic.dll


  RasterEdge.Imaging.Basic.Codec.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XImage.Raster.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XDoc.PDF.dll


Use corresponding namespaces;


  using RasterEdge.Imaging.Basic;


  using RasterEdge.XDoc.PDF;


Note: When you get the error "Could not load file or assembly 'RasterEdge.Imaging.Basic' or any other assembly or one of its dependencies. An attempt to load a program with an incorrect format", please check your configure as follows:

       

       If you are using x64 libraries/dlls, Right click the project -> Properties -> Build -> Platform target: x64.

       

       If using x86, the platform target should be x86.


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




//Load PDF document 
PDFDocument doc = new PDFDocument(@"C:\input.pdf");
//Define a header/footer setting 
PDFPageHeaderFooter headerFooter = new PDFPageHeaderFooter();
//Set center header field 
headerFooter.CenterHeaderField.Set(@"Title:*****", new Font(Arial, 12F, FontStyle.Regular), Color.Black);
//Set left footer field 
headerFooter.LeftFooterField.Set("Page<<1/n>>", new Font(Arial, 9F, FontStyle.Regular), Color.DarkGray);

//Define page range: all odd papges
PageRangeOptions pageRange = new  PageRangeOptions();
pageRange.AllPages = true;
pageRange.Subset = PageRangeSubset.Odd;

//Apply header/footer settings to all odd pages
PDFPageFieldHandler.ApplyHeaderFooter(doc, headerFooter, pageRange);

//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 papges
PageRangeOptions pageRange222 = new  PageRangeOptions();
pageRange2.AllPages = true;
pageRange2.Subset = PageRangeSubset.Even;

//Apply header/footer settings to all even pages
PDFPageFieldHandler.ApplyHeaderFooter(doc, hdrftr2, pageRange2);
doc.Save(@"C:\output.pdf");



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




//Load PDF document 
PDFDocument doc = new PDFDocument(@"C:\input.pdf");
//Remove header/footer from pdf file
PDFPageFieldHandler.RemoveHeaderFooters(doc);
doc.Save(@"C:\output.pdf");





public static int ApplyHeaderFooter(PDFDocument doc, PDFPageHeaderFooter hdrftr, PDFRangeOptions ops)

Description:
     Add header/footer to PDF file with specified settings.

Parameters:

Return:
     Error code, 0 if success.



public static int RemoveHeaderFooters(PDFDocument doc)

Description:
     Remove header/footer from pdf document.

Parameters:

Return:
     Error code, 0 if success.



public static int RemoveHeaderFooters(PDFDocument doc, int[] pageIndexs)

Description:
     Remove page header/footer from specified pdf pages.

Parameters:

Return:
     Error code, 0 if success.