Header and Footer
Header and Footer refer to annotations added at the top and bottom of a document, typically containing information such as titles, page numbers, and brand identification. By including headers and footers in a document, it becomes easier to navigate and identify, thereby enhancing the document's readability and professionalism.
In a PDF document, only one header and footer can exist, and adding a new header and footer will overwrite the old header and footer.
Add Header and Footer
The steps to add headers and footers are as follows:
Retrieve the header and footer objects within the document.
Specify the pages where headers and footers should be added.
Set attributes such as color and font size for the headers and footers.
Update the headers and footers on the pages.
This example shows how to add header and footer:
let url = URL(fileURLWithPath: "File Path")
let document = CPDFDocument(url: url)
let headerFooter = document?.headerFooter()
headerFooter?.setText("<<1,2>> page", at: 0)
headerFooter?.setTextColor(UIColor.red, at: 0)
headerFooter?.setFontSize(14.0, at: 0)
headerFooter?.update()
2
3
4
5
6
7
8
NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
CPDFHeaderFooter *headerFooter = document.headerFooter;
[headerFooter setText:@"<<1,2>> page" atIndex:0];
[headerFooter setTextColor:[UIColor redColor] atIndex:0];
[headerFooter setFontSize:14.0 atIndex:0];
[headerFooter update];
2
3
4
5
6
7
8
Header and Footer Regular Expression Explanation
Headers and footers support format-specific regular expressions that take effect when headerFooter.setRules(1)
is set, in the format: <<\d+,\d+>>|<<\d+>>|<\d+,>>
- <<i>> :
i
is the starting value of the page number. - <<i,f>>:
i
is the starting value of the page number, andf
is the number of digits in the page number, if the actual page number is not enough, it will be automatically filled with 0 in front.
eg: When text is set to "<<1,2>> page", the text displayed on the first page is "01 page".
Remove Header and Footer
Steps to remove headers and footers:
Retrieve the header and footer objects from the document.
Remove the headers and footers.
This example shows how to remove the header and footer:
let headerFooter = document?.headerFooter()
headerFooter?.clear()
2
CPDFHeaderFooter *headerFooter = document.headerFooter;
[headerFooter clear];
2