Guides
Add Headers and Footers in PDF
This sample shows how to add and remove headers and footers.
C#
using ComPDFKit.PDFDocument;
using System;
using System.Collections.Generic;
using System.IO;
namespace HeaderFooterTest
{
internal class HeaderFooterTest
{
private static string outputPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))) ?? string.Empty, "Output", "CS");
private static Dictionary<int, string> IntToLocationDic = new Dictionary<int, string>()
{
{0, "Top Left" },
{1, "Top Middle" },
{2, "Top Right" },
{3, "Bottom Left" },
{4, "Bottom Middle" },
{5, "Bottom Right" }
};
static void Main(string[] args)
{
Console.WriteLine("Running header and footer test sample…\r\n");
SDKLicenseHelper.LicenseVerify();
CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
#region Add common header and footer
if (AddCommonHeaderFooter(document))
{
Console.WriteLine("Add common header and footer done.");
}
else
{
Console.WriteLine("Add common header and footer failed.");
}
Console.WriteLine("--------------------");
#endregion
#region Add page header and footer
if (AddPageHeaderFooter(document))
{
Console.WriteLine("Add page header and footer done.");
}
else
{
Console.WriteLine("Add page header and footer failed.");
}
#endregion
Console.WriteLine("--------------------");
#region Edit header and footer
if (EditHeaderFooter(document))
{
Console.WriteLine("Edit header and footer done.");
}
else
{
Console.WriteLine("Edit header and footer failed.");
}
#endregion
Console.WriteLine("--------------------");
#region Delete header and footer
if (ClearHeaderFooter(document))
{
Console.WriteLine("Delete header and footer done.\n");
}
else
{
Console.WriteLine("delete header and footer failed\n");
}
#endregion
Console.WriteLine("--------------------");
Console.WriteLine("Done");
Console.WriteLine("--------------------");
Console.ReadLine();
}
/// <summary>
/// Follow these steps to add a header and footer in a blank pages file.
/// </summary>
/// <param name="document">Regular document</param>
static private bool AddCommonHeaderFooter(CPDFDocument document)
{
CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
byte[] color = { 255, 0, 0 };
headerFooter.SetPages("0-" + (document.PageCount - 1));
for (int i = 0; i <= 2; i++)
{
headerFooter.SetText(i, "ComPDFKit");
headerFooter.SetTextColor(i, color);
headerFooter.SetFontSize(i, 14);
Console.WriteLine("Text: {0}", headerFooter.GetText(i));
Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
}
headerFooter.Update();
string addHeaderFooterPath = Path.Combine(outputPath, "AddCommonHeaderFooterTest.pdf");
if (!document.WriteToFilePath(addHeaderFooterPath))
{
return false;
}
Console.WriteLine("Browse the changed file in " + addHeaderFooterPath);
return true;
}
/// <summary>
/// Add headers and footers that automatically display page numbers
/// </summary>
/// <param name="document">Regular document</param>
static private bool AddPageHeaderFooter(CPDFDocument document)
{
CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
byte[] color = { 255, 0, 0 };
headerFooter.SetPages("0-" + (document.PageCount - 1));
for (int i = 3; i <= 5; i++)
{
headerFooter.SetText(i, "<<1,2>>");
headerFooter.SetTextColor(i, color);
headerFooter.SetFontSize(i, 14);
Console.WriteLine("Text: {0}", headerFooter.GetText(i));
Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
}
headerFooter.Update();
string addHeaderFooterPath = Path.Combine(outputPath, "AddPageHeaderFooterTest.pdf");
if (document.WriteToFilePath(addHeaderFooterPath))
{
Console.WriteLine("Browse the changed file in " + addHeaderFooterPath);
return true;
}
else
{
return false;
}
}
/// <summary>
/// Follow these steps to delete a header and footer in a blank pages file.
/// </summary>
/// <param name="document">Documents that require manipulation</param>
static private bool EditHeaderFooter(CPDFDocument document)
{
CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
if (headerFooter.GetText(0) != string.Empty)
{
Console.WriteLine("Get old head and footer 0 succeeded, text is {0}", headerFooter.GetText(0));
}
else
{
Console.WriteLine("Get head and footer 0 failed, or it does not exist");
return false;
}
headerFooter.SetText(0, "ComPDFKit Samples");
headerFooter.Update();
Console.WriteLine("Change head and footer 0 succeeded, new text is {0}", headerFooter.GetText(0));
string editHeaderFooterPath = Path.Combine(outputPath, "EditHeaderFooterTest.pdf");
if (document.WriteToFilePath(editHeaderFooterPath))
{
Console.WriteLine("Browse the changed file in " + editHeaderFooterPath);
return true;
}
else
{
return false;
}
}
static private bool ClearHeaderFooter(CPDFDocument document)
{
CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
headerFooter.Clear();
string clearHeaderFooterPath = Path.Combine(outputPath, "ClearHeaderFooterTest.pdf");
if (document.WriteToFilePath(clearHeaderFooterPath))
{
Console.WriteLine("Browse the changed file in " + clearHeaderFooterPath);
return true;
}
else
{
return false;
}
}
}
}
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
200
201
202
203
204
205
206
207
208
209
210
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
200
201
202
203
204
205
206
207
208
209
210