概述
頁眉頁腳是一篇完整、精致的文檔的重要組成部分。在頁眉頁腳處,可以呈現的內容很多,如公司名稱、頁碼、工作表名、日期、圖片,如logo、標記等。在下面的文章中,將介紹如何在pdf中添加頁眉頁腳。通過代碼測試,添加頁眉頁腳可以分兩種情況來實現效果:
1.通過添加新的一頁,在新建的頁面上來添加頁眉頁腳
2.通過給現有文檔直接添加頁眉頁腳
下面將根據這兩種情況介紹具體的c#代碼操作
使用工具
free spire.pdf for .net 4.3(社區版)
示例代碼(供參考)
1.新建一頁來添加頁眉頁腳
1.1 添加頁眉
【c#】
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
|
using spire.pdf; using spire.pdf.graphics; using system.drawing; using system; namespace addheader_pdf { class program { static void main( string [] args) { //新建一個pdfdocument類對象,并添加一頁 pdfdocument pdf = new pdfdocument(); pdfpagebase page = pdf.pages.add(); //設置margin pdfunitconvertor unitcvtr = new pdfunitconvertor(); pdfmargins margin = new pdfmargins(); margin.top = unitcvtr.convertunits(2.54f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point); margin.bottom = margin.top; margin.left = unitcvtr.convertunits(4.17f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point); margin.right = margin.left; //調用addheader()方法添加頁眉 addheader(pdf, pdfpagesize.a4, margin); //保存并打開文檔 pdf.savetofile( "pdf頁眉.pdf" ); system.diagnostics.process.start( "pdf頁眉.pdf" ); } static void addheader(pdfdocument doc, sizef pagesize, pdfmargins margin) { //初始化一個pdfpagetemplateelement對象,用于創建頁眉 pdfpagetemplateelement headerspace = new pdfpagetemplateelement(pagesize.width, margin.top); headerspace.foreground = true ; doc.template.top = headerspace; //在頁眉部分繪入文字 pdftruetypefont font = new pdftruetypefont( new font( "arial unicode ms" , 10f), true ); pdfstringformat format = new pdfstringformat(pdftextalignment.right); string headertext = "world trade organization, wto \n the international organization that regulates international trade" ; float x = pdfpagesize.a4.width; float y = 0; headerspace.graphics.drawstring(headertext, font, pdfbrushes.black, x, y, format); //在頁眉部分繪入圖片 pdfimage headerimage = pdfimage.fromfile( @"c:\users\administrator\desktop\1.png" ); float width = headerimage.width / 2; float height = headerimage.height / 3; headerspace.graphics.drawimage(headerimage, 0, 0, width, height); } } } |
頁眉添加效果:
1.2添加頁腳
【c#】
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
|
using spire.pdf; using spire.pdf.graphics; using system.drawing; using system; using spire.pdf.automaticfields; namespace addfooter_pdf { class program { static void main( string [] args) { //新建一個pdfdocument類對象,添加一頁 pdfdocument doc = new pdfdocument(); pdfpagebase page = doc.pages.add(); //設置margin pdfunitconvertor unitcvtr = new pdfunitconvertor(); pdfmargins margin = new pdfmargins(); margin.top = unitcvtr.convertunits(2.54f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point); margin.bottom = margin.top; margin.left = unitcvtr.convertunits(4.17f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point); margin.right = margin.left; //調用addfooter()方法添加頁腳 addfooter(doc, pdfpagesize.a4, margin); //調用addpagenumber()方法添加頁碼 addpagenumber(doc, margin); //保存并打開文檔 doc.savetofile( "pdf頁腳.pdf" ); system.diagnostics.process.start( "pdf頁腳.pdf" ); } static void addfooter(pdfdocument doc, sizef pagesize, pdfmargins margin) { //初始化一個pdfpagetemplateelement對象,用于創建頁腳 pdfpagetemplateelement footerspace = new pdfpagetemplateelement(pagesize.width, margin.bottom); footerspace.foreground = true ; doc.template.bottom = footerspace; //在頁腳部分繪入文字 pdftruetypefont font = new pdftruetypefont( new font( "arial unicode ms" , 10f), true ); pdfstringformat format = new pdfstringformat(pdftextalignment.center); string headertext = "website : www.wto.org" ; float x = pdfpagesize.a4.width / 2; float y = 0; footerspace.graphics.drawstring(headertext, font, pdfbrushes.black, x, y, format); } static void addpagenumber(pdfdocument doc, pdfmargins margin) { //添加頁碼到頁腳部分 foreach (pdfpagebase page in doc.pages) { pdfstringformat format1 = new pdfstringformat(pdftextalignment.left); int x1 = convert.toint32(page.canvas.clientsize.width / 2); int y1 = convert.toint32(page.canvas.clientsize.height - margin.bottom + 20); rectangle bounds = new rectangle(x1, y1, 20, 20); pdfpagenumberfield field = new pdfpagenumberfield(); pdftruetypefont font = new pdftruetypefont( new font( "arial unicode ms" , 10f), true ); field.font = font; field.stringformat = format1; field.brush = pdfbrushes.black; field.bounds = bounds; field.draw(page.canvas); } } } } |
頁腳添加效果:
2.給現有pdf文檔添加頁眉頁腳
【c#】
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
|
using spire.pdf; using spire.pdf.automaticfields; using spire.pdf.graphics; using system; using system.drawing; namespace pdfheader { class program { static void main( string [] args) { //加載一個測試文檔 pdfdocument existingpdf = new pdfdocument(); existingpdf.loadfromfile( "test.pdf" ); //調用drawheader方法在現有文檔添加頁眉 drawheader(existingpdf); //調用drawfooter方法在現有文檔添加頁腳 drawfooter(existingpdf); //保存并打開文檔 existingpdf.savetofile( "output.pdf" ); system.diagnostics.process.start( "output.pdf" ); } //在頁面上方空白部位繪制頁眉 static void drawheader(pdfdocument doc) { //獲取頁面大小 sizef pagesize = doc.pages[0].size; //聲明x,y兩個float類型變量 float x = 90; float y = 20; for ( int i = 0; i < doc.pages.count; i++) { //在每一頁的指定位置繪制圖片 pdfimage headerimage = pdfimage.fromfile( "logo.png" ); float width = headerimage.width / 7; float height = headerimage.height / 7; doc.pages[i].canvas.drawimage(headerimage, x, y, width, height); //在每一頁的指定位置繪制橫線 pdfpen pen = new pdfpen(pdfbrushes.gray, 0.5f); doc.pages[i].canvas.drawline(pen, x, y + height + 2, pagesize.width - x, y + height + 2); } } //在頁面下方空白部位繪制頁腳 static void drawfooter(pdfdocument doc) { //獲取頁面大小 sizef pagesize = doc.pages[0].size; //聲明x,y兩個float類型變量 float x = 90; float y = pagesize.height - 72; for ( int i = 0; i < doc.pages.count; i++) { //在每一頁的指定位置繪制橫線 pdfpen pen = new pdfpen(pdfbrushes.gray, 0.5f); doc.pages[i].canvas.drawline(pen, x, y, pagesize.width - x, y); //在每一頁的指定位置繪制文字 y = y + 5; pdftruetypefont font = new pdftruetypefont( new font( "黑體" , 10f, fontstyle.bold), true ); pdfstringformat format = new pdfstringformat(pdftextalignment.left); string footertext = " website\n https://g20.org/" ; doc.pages[i].canvas.drawstring(footertext, font, pdfbrushes.black, x, y, format); //在每一頁的指定位置當前頁碼和總頁碼 pdfpagenumberfield number = new pdfpagenumberfield(); pdfpagecountfield count = new pdfpagecountfield(); pdfcompositefield compositefield = new pdfcompositefield(font, pdfbrushes.black, "{0}/{1}" , number, count); compositefield.stringformat = new pdfstringformat(pdftextalignment.right, pdfverticalalignment.top); sizef size = font.measurestring(compositefield.text); compositefield.bounds = new rectanglef(pagesize.width - x - size.width, y, size.width, size.height); compositefield.draw(doc.pages[i].canvas); } } } } |
測試效果:
注意事項
安裝之后,添加引用spire.pdf.dll文件到項目程序即可,dll文件可在安裝路徑下的bin文件夾中獲取。
以上是本次關于c#添加pdf頁眉頁腳的全部內容,兩種情況中的示例方法,可以選擇參考使用。
原文鏈接:https://segmentfault.com/a/1190000016209249