概述
在以下示例中,將介紹在pdf文檔頁(yè)面設(shè)置頁(yè)面切換按鈕的方法。示例中將頁(yè)面切換按鈕的添加分為了兩種情況,一種是設(shè)置按鈕跳轉(zhuǎn)到首頁(yè)、下頁(yè)、上頁(yè)或者最后一頁(yè),另一種是設(shè)置按鈕跳轉(zhuǎn)到指定頁(yè)面。兩種方法適應(yīng)不同的程序設(shè)計(jì)需要,可自行選擇合適的添加方法。
說(shuō)明
這里的代碼示例需要使用類庫(kù)spire.pdf for .net,版本4.0 。在使用該類庫(kù)時(shí),在項(xiàng)目程序中引用spire.pdf.dll即可(dll文件在安裝路徑下的bin文件中獲取)。
如:
代碼操作示例(供參考)
1.跳轉(zhuǎn)至特定頁(yè)(首頁(yè)、下一頁(yè)、上一頁(yè)、最后一頁(yè))
【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
|
using spire.pdf; using spire.pdf.actions; using spire.pdf.fields; using spire.pdf.graphics; using system.drawing; namespace buttontoappointedpage_pdf { class program { static void main( string [] args) { //實(shí)例化pdfdocument類,加載pdf測(cè)試f文檔 pdfdocument doc = new pdfdocument(); doc.loadfromfile( "sample.pdf" ); //允許添加form doc.allowcreateform = true ; //獲取文檔最后一頁(yè) pdfpagebase lastpage = doc.pages[doc.pages.count - 1]; //在頁(yè)面指定位置添加指定大小的按鈕 pdfbuttonfield button = new pdfbuttonfield(lastpage, "click to back " ); button.bounds = new rectanglef(lastpage.actualsize.width - 150, lastpage.actualsize.height - 400, 60, 20); //設(shè)置按鈕邊框顏色 button.borderstyle = pdfborderstyle.solid; button.bordercolor = new pdfrgbcolor(color.white); //設(shè)置按鈕背景色 button.backcolor = color.azure; //設(shè)置按鈕提示語(yǔ) button.tooltip = "to the first page" ; //設(shè)置按鈕文字字體和顏色 pdftruetypefont truetypefont = new pdftruetypefont( new font( "avant garde" , 9f), true ); button.font = truetypefont; button.forecolor = color.black; //創(chuàng)建pdfnamedaction實(shí)例,在傳入的參數(shù)中選擇上一頁(yè)、下一頁(yè)、首頁(yè)或最后一頁(yè) pdfnamedaction namedaction = new pdfnamedaction(pdfactiondestination.firstpage); //應(yīng)用動(dòng)作 button.actions.mousedown = namedaction; //添加按鈕到文檔 doc.form.fields.add(button); //保存并打開(kāi)pdf文檔 doc.savetofile( "result.pdf" , fileformat.pdf); system.diagnostics.process.start( "result.pdf" ); } } } |
ps:這里的pdfnameaction類支持四種按鈕跳轉(zhuǎn)動(dòng)作
添加效果(截圖):
點(diǎn)擊文中的按鈕時(shí),即可跳轉(zhuǎn)至按鈕指向的頁(yè)面。
2.跳轉(zhuǎn)至指定頁(yè)面
【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
|
using spire.pdf; using spire.pdf.actions; using spire.pdf.fields; using spire.pdf.general; using spire.pdf.graphics; using system.drawing; namespace buttom2 { class program { static void main( string [] args) { //實(shí)例化pdfdocument類,加載pdf文檔 pdfdocument doc = new pdfdocument(); doc.loadfromfile( "sample.pdf" ); //允許添加form doc.allowcreateform = true ; //獲取最后一頁(yè) pdfpagebase lastpage = doc.pages[doc.pages.count - 1]; //在頁(yè)面指定位置添加按鈕 pdfbuttonfield button = new pdfbuttonfield(lastpage, "back" ); button.bounds = new rectanglef(lastpage.actualsize.width - 150, lastpage.actualsize.height - 700, 50, 20); //設(shè)置按鈕邊框顏色 button.borderstyle = pdfborderstyle.solid; button.bordercolor = new pdfrgbcolor(color.transparent); //設(shè)置按鈕背景色 button.backcolor = color.whitesmoke; //設(shè)置按鈕提示語(yǔ) button.tooltip = "click and back to the third page" ; //設(shè)置按鈕文字字體和顏色 pdftruetypefont truetypefont = new pdftruetypefont( new font( "avant garde" , 9f), true ); button.font = truetypefont; button.forecolor = color.black; //實(shí)例化pdfdestination對(duì)象,傳入指定頁(yè)碼到第3頁(yè) pdfdestination destination = new pdfdestination(doc.pages[2]); //創(chuàng)建go to動(dòng)作 pdfgotoaction gotoaction = new pdfgotoaction(destination); //應(yīng)用動(dòng)作 button.actions.mousedown = gotoaction; //添加按鈕到文檔 doc.form.fields.add(button); //保存并打開(kāi)pdf文檔 doc.savetofile( "result.pdf" , fileformat.pdf); system.diagnostics.process.start( "result.pdf" ); } } } |
添加效果(截圖):
點(diǎn)擊按鈕,即可跳轉(zhuǎn)至指定的文檔第3頁(yè)。
以上所述是小編給大家介紹的c# pdf page操作設(shè)置頁(yè)面切換按鈕的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://www.cnblogs.com/Yesi/p/8979938.html