Guides
Add Headers and Footers in PDF
This sample shows how to add and remove headers and footers
Java
/**
* Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
* <p>
* THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
* AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
* UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
* This notice may not be removed from this file.
*/
package com.compdfkit.samples.samples;
import android.graphics.Color;
import com.compdfkit.core.document.CPDFDocument;
import com.compdfkit.core.document.CPDFHeaderFooter;
import com.compdfkit.samples.PDFSamples;
import com.compdfkit.samples.R;
import com.compdfkit.samples.util.FileUtils;
import com.compdfkit.samples.util.OutputListener;
import java.io.File;
public class HeaderFooterTest extends PDFSamples {
public HeaderFooterTest() {
setTitle(R.string.header_footer_test_title);
setDescription(R.string.header_footer_test_desc);
}
@Override
protected void run(OutputListener outputListener) {
super.run(outputListener);
printHead();
addCommonHeaderFooter();
addPageHeaderFooter();
editHeaderFooter();
clearHeaderFooterTest();
printFooter();
}
/**
* samples 1 : add header
*/
private void addCommonHeaderFooter() {
printDividingLine();
outputListener.println("Samples 1 : Insert common header footer");
CPDFDocument document = new CPDFDocument(context);
document.open(FileUtils.getAssetsTempFile(context, "CommonFivePage.pdf"));
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
String headerStr = "ComPDFKit";
// index 0 : top left
// index 1 : top middle
// index 2 : top right
int index = 3;
for (int i = 0; i < index; i++) {
outputListener.println("Text: " + headerStr);
headerFooter.setText(i, headerStr);
headerFooter.setTextColor(i, Color.RED);
headerFooter.setFontSize(i, 14);
if (i == 0) {
outputListener.println("Location: Top Left");
} else if (i == 1) {
outputListener.println("Location: Top Middle");
} else {
outputListener.println("Location: Top Right");
}
outputListener.println();
}
headerFooter.setPages("0,1,2,3,4");
headerFooter.update();
File file = new File(outputDir(), "HeaderFooterTest/AddCommonHeaderFooter.pdf");
saveSamplePDF(document, file, false);
outputListener.println("Done. Results saved in AddCommonHeaderFooter.pdf");
printDividingLine();
}
private void addPageHeaderFooter() {
outputListener.println("Samples 2 : Insert page header footer");
CPDFDocument document = new CPDFDocument(context);
document.open(FileUtils.getAssetsTempFile(context, "CommonFivePage.pdf"));
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
String headerStr = "ComPDFKit";
for (int i = 0; i < document.getPageCount(); i++) {
int pageNumber = i + 1;
int index = 6;
// index 0 : top left
// index 1 : top middle
// index 2 : top right
// index 3 : bottom left
// index 4 : bottom middle
// index 5 : bottom right
for (int i1 = 0; i1 < index; i1++) {
if (i1 < 3) {
outputListener.println("Text: " + headerStr);
headerFooter.setText(i1, headerStr);
headerFooter.setTextColor(i1, Color.BLACK);
} else {
outputListener.println("Text: 0" + pageNumber);
headerFooter.setText(i1, "0" + pageNumber);
headerFooter.setTextColor(i1, Color.RED);
}
if (i1 == 0) {
outputListener.println("Location: Top Left");
} else if (i1 == 1) {
outputListener.println("Location: Top Middle");
} else if (i1 == 2) {
outputListener.println("Location: Top Right");
} else if (i1 == 3) {
outputListener.println("Location: Bottom Left");
} else if (i1 == 4) {
outputListener.println("Location: Bottom Middle");
} else {
outputListener.println("Location: Bottom Right");
}
headerFooter.setFontSize(i, 14);
}
headerFooter.setPages("" + i);
headerFooter.update();
}
File file = new File(outputDir(), "HeaderFooterTest/AddPageHeaderFooter.pdf");
saveSamplePDF(document, file, false);
outputListener.println("Done. Results saved in AddPageHeaderFooter.pdf");
printDividingLine();
}
/**
* samples 3 : edit top left header
*/
private void editHeaderFooter() {
outputListener.println("Samples 3 : Edit top left header");
CPDFDocument document = new CPDFDocument(context);
File file = new File(outputDir(), "HeaderFooterTest/AddCommonHeaderFooter.pdf");
document.open(file.getAbsolutePath());
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
outputListener.println("Get old head and footer 0 succeeded, text is " + headerFooter.getText(0));
outputListener.println("Change head and footer 0 succeeded, new text is ComPDFKit Samples");
// change top left text
headerFooter.setText(0, "ComPDFKit Samples");
headerFooter.update();
File resultsFile = new File(outputDir(), "HeaderFooterTest/EditHeaderFooterTest.pdf");
saveSamplePDF(document, resultsFile, false);
outputListener.println("Done. Results saved in EditHeaderFooterTest.pdf");
printDividingLine();
}
private void clearHeaderFooterTest(){
outputListener.println("Samples 4 : Clean all header and footer");
CPDFDocument document = new CPDFDocument(context);
File file = new File(outputDir(), "HeaderFooterTest/AddCommonHeaderFooter.pdf");
document.open(file.getAbsolutePath());
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
outputListener.println("");
headerFooter.clear();
File resultsFile = new File(outputDir(), "HeaderFooterTest/ClearHeaderFooterTest.pdf");
saveSamplePDF(document, resultsFile, false);
outputListener.println("Done. Results saved in ClearHeaderFooterTest.pdf");
printDividingLine();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159