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 to pages containing an existing 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:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath, password);
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
int index = 0;
headerFooter.setText(index, "<<1,2>> page");
headerFooter.setTextColor(index, Color.RED);
headerFooter.setFontSize(index, 14);
headerFooter.setPages("0,1,2");
headerFooter.setRules(1);
headerFooter.update();
2
3
4
5
6
7
8
9
10
11
// Open document from file path.
val document = CPDFDocument(context)
document.open(pdfPath, password)
val index = 0
document.headerFooter.apply {
setText(index, "<<1,2>> page")
setTextColor(index, Color.RED)
setFontSize(index, 14F)
pages = "0,1,2"
rules = 1
update()
}
2
3
4
5
6
7
8
9
10
11
12
Header/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:
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
headerFooter.clear();
2
document.headerFooter.clear()