Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
The PDF format does not explicitly specify any header or footer for a page. However, PDFOne .NET provides you the ability to specify your own headers and footers when you create or modify PDF documents. Using this feature, you can use images and formatted text as headers and footers.
When creating a new page using PDFOne .NET, you can specify header and footer sizes using one of the overloaded PDFPage
constructors.
public PDFPage( PaperKind pageSize, float pageHeaderHeight, float pageFooterHeight, float pageLeftMargin, float pageTopMargin, float pageRightMargin, float pageBottomMargin, PDFMeasurementUnit measurementUnit );
In the following code snippet, an A4-size PDF page has been created with header and footer sizes set at 2 centimeters.
// Create a page with specified header and footer sizes PDFPage page1 = new PDFPage( PaperKind.A4, // A4-size page 2, // 2-cm header 2, // 2-cm footer 3, // 3-cm left margin 2, // 2-cm top margin 1, // 1-cm right margin 2, // 2-cm bottom margin PDFMeasurementUnit.Centimeters // measurment units in centimeters );
After specifying the sizes of the header and footer regions, you can use the following methods of the PDFDocument
class to add text and images.
AddHeaderImage()
AddHeaderText()
AddFooterImage()
AddFooterText()
In the code snippet below, some text is written to the designated footer region of a page.
// Create a font PDFFont font1 = new PDFFont( StdType1Font.Times_Bold, // Times font PDFFontStyle.Fill, // filled (shaded) glyphs 15); // 15-point size // Add footer text page1.AddFooterText( "P.T.O", // footer text font1, // font used to render footer text PDFHAlignment.Right, // horizontal text alignment PDFVAlignment.Bottom // vertical text alignment);
If you want to add a header or footer to a page in an existing PDF document, it is not as straightforward as when you do the same with a new PDF document that you create.
As mentioned earlier, there is no concept of headers and footers in PDF. So, when you need to add a header or footer to an existing page, you need a mechanism to specify where the header or footer needs to be on that page. This would then help the above-mentioned methods to place text or images on the header/footer region that you have designated.
For that purpose, PDFOne .NET provides a CreatePage()
event for which you need to register an event handler that will then be used to specify the footer and header regions.
The CreatePage()
event fires when a page from the existing document is created in the output file. If the header and footer regions are defined by the event handler, then PDFOne .NET knows exactly where the header or footer needs to be placed on the page when an "add footer" or an "add header" method is subsequently called.
// event public event PageCreateEventHandler CreatePage(); // delegate public delegate void PageCreateEventHandler( object sender, ref PageMargins margins, int pageNumber, float pageWidthInPoints, float pageHeightInPoints);
In the PageCreateEventHandler
event handler, you specify the header and footer size with the PageMargins
structure parameter. The members of this structure contain values for the header margin, footer margin, top margin, bottom margin, right margin, left margin, and the measurement unit with which these margins are specified.
public struct PageMargins { public float leftMargin; public float rightMargin; public float topMargin; public float bottomMargin; public PDFMeasurementUnit measurementUnit; public float headerHeight; public float footerHeight; }
Now, here is the code snippet that brings it all together.
// A button-click event on a form - create a // form, add a button, double-click the button, // paste this code and run it. private void button1_Click( object sender, System.EventArgs e) { // Create a document object PDFDocument doc = new PDFDocument(); // Register the event handler doc.CreatePage += new PageCreateEventHandler(doc_CreatePage); // Load an existing PDF document doc.Load("Test.pdf"); // Process each page in the document for (int i = 1; i <= doc.GetPageCount(); i++) { // Get specified page from the document PDFPage page = doc.GetPage(i); // Add an image to the header page.AddHeaderImage( "image1.bmp", PDFHAlignment.Center, PDFVAlignment.Center); // Add formatted text to the header page.AddHeaderText( "This is Header Text", new PDFFont(StdType1Font.Helvetica, 12), PDFHAlignment.Left, PDFVAlignment.Top); // Add an image to the footer page.AddFooterImage( "image2.bmp", PDFHAlignment.Center, PDFVAlignment.Center); // Add formatted text to the header page.AddFooterText( "This is Footer Text", new PDFFont(StdType1Font.Helvetica, 12), PDFHAlignment.Left, PDFVAlignment.Top); } doc.OpenAfterCreate = true; doc.Save("Headers_Footers.pdf"); doc.Close(); } // Event handler private void doc_CreatePage( object sender, ref PageMargins margins, int pageNumber, float pageWidthInPoints, float pageHeightInPoints) { // Specify page margins margins.topMargin = 0.5f; margins.leftMargin = 2; margins.bottomMargin = 1; margins.rightMargin = 2; // Specify header and footer sizes margins.headerHeight = 0.5f; margins.footerHeight = 1; // Specify measurement unit for margins margins.measurementUnit = PDFMeasurementUnit.Centimeters; }
---o0O0o---
Our .NET Developer Tools | |
---|---|
![]() Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
![]() PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
Our Delphi/C++Builder developer tools | |
---|---|
![]() Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
![]() eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
![]() PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
Our Java developer tools | |
---|---|
![]() Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
![]() PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
Our Platform-Agnostic Cloud and On-Premises APIs | |
---|---|
![]() StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2025 Gnostice Information Technologies Private Limited. All rights reserved. |