Guides
Add Headers or Footers in PDF
This sample shows how to add and remove headers and footers.
objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
CPDFDocument *myDocument = document;
[self addCommonHeaderFooter:myDocument];
[self addPageHeaderFooter:myDocument];
[self editHeaderFooter];
[self deleteHeaderFooter];
}
// Insert common header footer
- (void)addCommonHeaderFooter:(CPDFDocument *)oldDocument {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 1: Insert common header footer\n"];
// Get Sandbox path for saving the PDF File
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddCommonHeaderFooterTest"];
// Save the document in the test PDF file
NSURL *addCommonHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
[oldDocument writeToURL:addCommonHeaderFooterURL];
// Create a new document for test PDF file
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addCommonHeaderFooterURL];
// Create text header footer
CPDFHeaderFooter *headerFooter = document.headerFooter;
[headerFooter setText:@"ComPDFKit" atIndex:0];
[headerFooter setText:@"ComPDFKit" atIndex:1];
[headerFooter setText:@"ComPDFKit" atIndex:2];
[headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
[headerFooter setFontSize:14.0 atIndex:0];
[headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:1];
[headerFooter setFontSize:14.0 atIndex:1];
[headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:2];
[headerFooter setFontSize:14.0 atIndex:2];
headerFooter.pageString = @"0-4";
[headerFooter update];
// Print header footer object message
for (int i = 0; i < 3; i++) {
commandLineStr = [commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
}
[document writeToURL:addCommonHeaderFooterURL];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddCommonHeaderFooterTest.pdf\n"];
}
// Insert page header footer
- (void)addPageHeaderFooter:(CPDFDocument *)oldDocument {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 2: Insert page header footer\n"];
// Get Sandbox path for saving the PDF File
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddPageHeaderFooterTest"];
// Save the document in the test PDF file
NSURL *addPageHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
[oldDocument writeToURL:addPageHeaderFooterURL];
// Create a new document for test PDF file
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addPageHeaderFooterURL];
// Create page header footer
CPDFHeaderFooter *headerFooter = document.headerFooter;
[headerFooter setText:@"<<1,2>>" atIndex:0];
[headerFooter setText:@"<<1,2>>" atIndex:1];
[headerFooter setText:@"<<1,2>>" atIndex:2];
[headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
[headerFooter setFontSize:14.0 atIndex:0];
[headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
[headerFooter setFontSize:14.0 atIndex:1];
[headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
[headerFooter setFontSize:14.0 atIndex:2];
headerFooter.pageString = @"0-4";
[headerFooter update];
// Print page header footer message
for (int i = 0; i < 3; i++) {
commandLineStr = [commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
}
[document writeToURL:addPageHeaderFooterURL];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddPageHeaderFooterTest.pdf\n"];
}
// Edit header footer
- (void)editHeaderFooter {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 3: Edit header footer\n"];
// Get Sandbox path for saving the PDF File
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
// Copy file
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EditHeaderFooterTest"];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
// Edit text header footer
NSURL *editHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:editHeaderFooterURL];
CPDFHeaderFooter *headerFooter = document.headerFooter;
[headerFooter setText:@"ComPDFKit Samples" atIndex:0];
[headerFooter setText:@"ComPDFKit" atIndex:1];
[headerFooter setText:@"ComPDFKit" atIndex:2];
[headerFooter update];
for (int i = 0; i < 3; i++) {
commandLineStr = [commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
}
[document writeToURL:editHeaderFooterURL];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in EditHeaderFooterTest.pdf\n"];
}
// Delete header footer
- (void)deleteHeaderFooter {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 4: Delete header footer\n"];
// Save a document in Sandbox
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
// Copy file
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"DeleteHeaderFooterTest"];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
NSURL *deleteHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:deleteHeaderFooterURL];
// Delete header footer
CPDFHeaderFooter *headerFooter = document.headerFooter;
[headerFooter clear];
[document writeToURL:deleteHeaderFooterURL];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in DeleteHeaderFooterTest.pdf\n"];
}
- (NSString *)getStringFromEnumLocation:(NSInteger)enums {
switch (enums) {
case 0:
return @"Top Left";
break;
case 1:
return @"Top Middle";
break;
case 2:
return @"Top Right";
break;
case 3:
return @"Button Left";
break;
case 4:
return @"Button Middle";
break;
case 5:
return @"Button Right";
break;
default:
return @" ";
break;
}
}
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199