C# PDF Page操作設(shè)置頁面切換按鈕的方法
概述
在以下示例中,將介紹在PDF文檔頁面設(shè)置頁面切換按鈕的方法。示例中將頁面切換按鈕的添加分為了兩種情況,一種是設(shè)置按鈕跳轉(zhuǎn)到首頁、下頁、上頁或者最后一頁,另一種是設(shè)置按鈕跳轉(zhuǎn)到指定頁面。兩種方法適應(yīng)不同的程序設(shè)計(jì)需要,可自行選擇合適的添加方法。
說明
這里的代碼示例需要使用類庫Spire.PDF for .NET,版本4.0 。在使用該類庫時(shí),在項(xiàng)目程序中引用Spire.Pdf.dll即可(dll文件在安裝路徑下的Bin文件中獲取)。
如:
代碼操作示例(供參考)
1.跳轉(zhuǎn)至特定頁(首頁、下一頁、上一頁、最后一頁)
【C#】
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測試F文檔 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //允許添加Form doc.AllowCreateForm = true; //獲取文檔最后一頁 PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1]; //在頁面指定位置添加指定大小的按鈕 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è)置按鈕提示語 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ù)中選擇上一頁、下一頁、首頁或最后一頁 PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage); //應(yīng)用動(dòng)作 button.Actions.MouseDown = namedAction; //添加按鈕到文檔 doc.Form.Fields.Add(button); //保存并打開PDF文檔 doc.SaveToFile("result.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("result.pdf"); } } }
PS:這里的PdfNameAction類支持四種按鈕跳轉(zhuǎn)動(dòng)作
添加效果(截圖):
點(diǎn)擊文中的按鈕時(shí),即可跳轉(zhuǎn)至按鈕指向的頁面。
2.跳轉(zhuǎn)至指定頁面
【C#】
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; //獲取最后一頁 PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1]; //在頁面指定位置添加按鈕 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è)置按鈕提示語 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對象,傳入指定頁碼到第3頁 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); //保存并打開PDF文檔 doc.SaveToFile("result.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("result.pdf"); } } }
添加效果(截圖):
點(diǎn)擊按鈕,即可跳轉(zhuǎn)至指定的文檔第3頁。
以上所述是小編給大家介紹的C# PDF Page操作設(shè)置頁面切換按鈕的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- C#實(shí)現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件
- C# WinForm打開PDF文件并在窗體中顯示
- C#代碼實(shí)現(xiàn)PDF文檔操作類
- C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- C#使用iTextSharp設(shè)置PDF所有頁面背景圖功能實(shí)例
- 用C#來解析PDF文件
- C#實(shí)現(xiàn)TIF圖像轉(zhuǎn)PDF文件的方法
- C# Page用于各頁面繼承功能實(shí)例
- 關(guān)于C# TabPage如何隱藏的問題
- C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法
- c#重寫TabControl控件實(shí)現(xiàn)關(guān)閉按鈕的方法
相關(guān)文章
Unity3D使用GL實(shí)現(xiàn)圖案解鎖功能
這篇文章主要為大家詳細(xì)介紹了Unity3D使用GL實(shí)現(xiàn)圖案解鎖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03C#實(shí)現(xiàn)優(yōu)先隊(duì)列和堆排序
本文詳細(xì)講解了C#實(shí)現(xiàn)優(yōu)先隊(duì)列和堆排序的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04詳解如何通過wireshark實(shí)現(xiàn)捕獲C#上傳的圖片
這篇文章主要為大家詳細(xì)介紹了如何通過wireshark實(shí)現(xiàn)捕獲C#上傳的圖片,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-11-11unity中點(diǎn)擊某一個(gè)按鈕播放某一個(gè)動(dòng)作的操作
這篇文章主要介紹了unity中點(diǎn)擊某一個(gè)按鈕播放某一個(gè)動(dòng)作的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04