|
C# PDF Page Header/Footer Library
How to add, edit header and footer in PDF file using c# .net with example source code
An Excellent PDF Control Allows C# Developers to add or delete header/footer/page number to PDF File in C#.NET, without itextsharp, itext
In this tutorial, you will learn how to create Acrobat compatible PDF page header and footer section contents using C# in your .NET applications.
- Add static text message to page header/footer
- Add dynamic fields (Page Number, Date) to page header/footer
- Add image content to page header/footer
- Update existing page header/footer
- Remove existing page header/footer
How to add, remove PDF page header and footer programmatically using C#
- 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 Core and Framework for PDF page header, footer
- Support .NET WinForms, ASP.NET Core, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, SharePoint
- Besides page header, footer edit, PDF C# library also includes
page count,
resize pdf page,
rotate pdf pages orientation,
add new pages,
remove, delete pages from PDF,
extract PDF pages
- 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:
- 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 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>> : Page number only.
- <<1/n>> : Page number and total page count, with separator '/'.
For Date
- <<m/d>> : Month and day, with separator '/'.
- <<m/d/yy>> : Month, day and year, with separator '/'.
- <<m/d/yyyy>> : Month, day and year, with separator '/'.
- <<mm/dd/yy>> : Month, day and year, with separator '/'.
- <<mm/dd/yyyy>> : Month, day and year, with separator '/'.
- <<d/m/yy>> : Day, month and year, with separator '/'.
- <<d/m/yyyy>> : Day, month and year, with separator '/'.
- <<dd/mm/yy>> : Day, month and year, with separator '/'.
- <<dd/mm/yyyy>> : Day, month and year, with separator '/'.
- <<mm/yy>> : Month and year, with separator '/'.
- <<mm/yyyy>> : Month and year, with separator '/'.
- <<m.d.yy>> : Month, day and year, with separator '.'.
- <<m.d.yyyy>> : Month, day and year, with separator '.'.
- <<mm.dd.yy>> : Month, day and year, with separator '.'.
- <<mm.dd.yyyy>> : Month, day and year, with separator '.'.
- <<mm.yy>> : Month and year, with separator '.'.
- <<mm.yyyy>> : Month and year, with separator '.'.
- <<d.m.yy>> : Day, month and year, with separator '.'.
- <<d.m.yyyy>> : Day, month and year, with separator '.'.
- <<dd.mm.yy>> : Day, month and year, with separator '.'.
- <<dd.mm.yyyy>> : Day, month and year, with separator '.'.
- <<yy-mm-dd>> : Year, month and day, with separator '-'.
- <<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);
Add image in PDF header and footer using C#
Like Acrobat, XDoc.PDF C# library does not support adding image in PDF header and footer through PDFPageHeaderFooter object. However you can do it through PDF watermark.
The text and C# source code below explains how to create, add image to PDF page header and footer using C# code.
- Create a PDFDocument object from an existing PDF file path
- Create a PDFWatermarkImageRes image watermark object from an existing raster image file
- Customize PDFWatermarkImageRes object with options, such as below the page content, opacity.
- Apply the image position with horizontal and vertical alignment and margin spaces. Make sure the image location is inside the page header and footer area.
- Create a PageRangeOptions object with image inserted page range specified
- Call PDFPageFieldHandler.ApplyWatermark() to add image to specified pages
- Save modified PDF document to a new PDF file
String inputFilePath = Path.Combine(inputFolder, "Input.pdf");
String outputFilePath = Path.Combine(inputFolder, "output.pdf");
Bitmap resImage = new Bitmap(Path.Combine(inputFolder, "RE_logo.jpg"));
// Open file
PDFDocument doc = new PDFDocument(inputFilePath);
// Load image resource for the watermark
PDFWatermarkImageRes resWatermark = new PDFWatermarkImageRes(resImage);
// Define a watermark setting
PDFPageWatermark watermark = new PDFPageWatermark(resWatermark);
// Set watermark behind the page content.
watermark.IsAbovePage = false;
// Set transparancy.
watermark.Opacity = 0.9F;
// Set Alignment (Top left)
watermark.HoriAlignment = WatermarkHoriAlignment.Left;
watermark.VertAlignment = WatermarkVertAlignemnt.Top;
// Shift image 0.05 inches in horizontally and 0.1 inches in vertically.
watermark.HoriMargin = 0.05F;
watermark.VertMargin = 0.1F;
// Define page range: all pages
PageRangeOptions pageRange = new PageRangeOptions();
pageRange.AllPages = true;
pageRange.Subset = PageRangeSubset.All;
// Apply watermark settings to all odd pages
PDFPageFieldHandler.ApplyWatermark(doc, watermark, pageRange);
// Save file
doc.Save(outputFilePath);
You can view more watermark functions using C# PDF library in C# code at
How to add, remove text, image watermark to pdf using C# code?
Remove all Page Header/Footer data from a PDF document object using C#
The text and C# source code below shows how to remove page header and footer from PDF document using C# code.
- Create a PDFDocument object from a PDF file path
- Call PDFPageFieldHandler.RemoveHeaderFooters() to remove page header and footer from PDF document
- Save modified PDF document to a new PDF file
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();
|