In electrical files like PDFs, headers and footers are practical at work. Except for page numbers, the headers and footers are also powerful tools for us to add date and other text information. For writers, they could add their name or the title of the works. For companies, they may need to add the company’s name, slogan, etc.
As for developers, we would think more about the code methods to add headers & footers. That’s the theme of this article. I’m going to show how to add headers & footers to PDFs with ComPDFKit PDF SDK.
Headers & Footers
There are six fields in a header and footer. Each field has a number. You can add text into the field you want with the field’s number and atIndex:
.
With ComPDFKit PDF SDK, the fields are defined through page margins. You can have a look at the following picture for the details.
Add Headers & Footers with ComPDFKit
Text
You can add text content to headers and footers with ComPDFKit. The text will be repeated across all pages of a PDF. According to your preference or needs, you can also set the text like font size, color, style, margin, and the range of pages. We have a code example in Objective-C here. You can follow the code example to set the center footer text as “1” with font size 16.
NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
CPDFHeaderFooter *headerFooter = [document headerFooter];
[headerFooter setText:@"<<1>>" atIndex:4];
[headerFooter setFontSize:16 atIndex:4];
[headerFooter update];
For the method in other programming languages, please refer to our developer guides.
Page Numbers & Date
Page numbers and date are dynamic field tokens and a little different from other text. For example, every page has a number, and the page number will change according to the pages. That’s dynamic. The date will also fill dynamically after setting the mode of the date. Here are some examples of setting the page number.
- <<1>> : Page number only.
- <<1/n>> : Page number and total page count, with separator '/'.
Remove Headers & Footers
When you don’t need the header and footer, you can also remove them by the following method. We also take Objective-C as an example here. For the method in other programming languages, please refer to our developer guides.
[headerFooter clear];
Other Functions
Bates numbers might not be known by most people except the people in law, medicine, and the business. Bates numbers make it more simple to search and index documents. Here is an example below for you to see how to set it.
- front<<#3#1#ab#cd>>back
When you set the bates code like above, it will display like “frontab001cdback”.
Conclusion
The professional ComPDFKit PDF SDK not only supports headers and footers, but also other practical functions. For more information about ComPDFKit, click here or contact us.