C# PDF Page操作——設置頁面切換按鈕

概述

在以下示例中,將介紹在PDF文檔頁面設置頁面切換按鈕的方法。示例中將頁面切換按鈕的添加分爲了兩種情況,一種是設置按鈕跳轉到首頁、下頁、上頁或者最後一頁,另一種是設置按鈕跳轉到指定頁面。兩種方法適應不同的程序設計需要,可自行選擇合適的添加方法。

說明

這裏的代碼示例需要使用類庫Spire.PDF for .NET,版本4.0 。在使用該類庫時,在項目程序中引用Spire.Pdf.dll即可(dll文件在安裝路徑下的Bin文件中獲取)。
如:
C# PDF Page操作——設置頁面切換按鈕

代碼操作示例(供參考)

1.跳轉至特定頁(首頁、下一頁、上一頁、最後一頁)

【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)
        {
            //實例化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);

            //設置按鈕邊框顏色
            button.BorderStyle = PdfBorderStyle.Solid;
            button.BorderColor = new PdfRGBColor(Color.White);

            //設置按鈕背景色
            button.BackColor = Color.Azure;

            //設置按鈕提示語       
            button.ToolTip = "To the first page";

            //設置按鈕文字字體和顏色      
            PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("Avant Garde", 9f), true);
            button.Font = trueTypeFont;
            button.ForeColor = Color.Black;

            //創建PdfNamedAction實例,在傳入的參數中選擇上一頁、下一頁、首頁或最後一頁
            PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);

            //應用動作
            button.Actions.MouseDown = namedAction;

            //添加按鈕到文檔
            doc.Form.Fields.Add(button);

            //保存並打開PDF文檔
            doc.SaveToFile("result.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("result.pdf");

        }
    }
}

【VB.NET】

Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.Fields
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace ButtonToAppointedPage_PDF

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim doc As PdfDocument = New PdfDocument()
            doc.LoadFromFile("sample.pdf")
            doc.AllowCreateForm = True
            Dim lastPage As PdfPageBase = doc.Pages(doc.Pages.Count - 1)
            Dim button As PdfButtonField = New PdfButtonField(lastPage, "Click To Back ")
            button.Bounds = New RectangleF(lastPage.ActualSize.Width - 150, lastPage.ActualSize.Height - 400, 60, 20)
            button.BorderStyle = PdfBorderStyle.Solid
            button.BorderColor = New PdfRGBColor(Color.White)
            button.BackColor = Color.Azure
            button.ToolTip = "To the first page"
            Dim trueTypeFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Avant Garde", 9F), True)
            button.Font = trueTypeFont
            button.ForeColor = Color.Black
            Dim namedAction As PdfNamedAction = New PdfNamedAction(PdfActionDestination.FirstPage)
            button.Actions.MouseDown = namedAction
            doc.Form.Fields.Add(button)
            doc.SaveToFile("result.pdf", FileFormat.PDF)
            System.Diagnostics.Process.Start("result.pdf")
        End Sub
    End Class
End Namespace

PS:這裏的PdfNameAction類支持四種按鈕跳轉動作
C# PDF Page操作——設置頁面切換按鈕
添加效果(截圖):
C# PDF Page操作——設置頁面切換按鈕
點擊文中的按鈕時,即可跳轉至按鈕指向的頁面。

2.跳轉至指定頁面

【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)
        {
            //實例化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);

            //設置按鈕邊框顏色
            button.BorderStyle = PdfBorderStyle.Solid;
            button.BorderColor = new PdfRGBColor(Color.Transparent);

            //設置按鈕背景色
            button.BackColor = Color.WhiteSmoke;

            //設置按鈕提示語       
            button.ToolTip = "Click and back to the third page";

            //設置按鈕文字字體和顏色      
            PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("Avant Garde", 9f), true);
            button.Font = trueTypeFont;
            button.ForeColor = Color.Black;

            //實例化PdfDestination對象,傳入指定頁碼到第3頁
            PdfDestination destination = new PdfDestination(doc.Pages[2]);

            //創建go to動作
            PdfGoToAction goToAction = new PdfGoToAction(destination);

            //應用動作
            button.Actions.MouseDown = goToAction;

            //添加按鈕到文檔
            doc.Form.Fields.Add(button);

            //保存並打開PDF文檔
            doc.SaveToFile("result.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

【VB.NET】

Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.Fields
Imports Spire.Pdf.General
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace Buttom2

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim doc As PdfDocument = New PdfDocument()
            doc.LoadFromFile("sample.pdf")
            doc.AllowCreateForm = True
            Dim lastPage As PdfPageBase = doc.Pages(doc.Pages.Count - 1)
            Dim button As PdfButtonField = New PdfButtonField(lastPage, "Back")
            button.Bounds = New RectangleF(lastPage.ActualSize.Width - 150, lastPage.ActualSize.Height - 700, 50, 20)
            button.BorderStyle = PdfBorderStyle.Solid
            button.BorderColor = New PdfRGBColor(Color.Transparent)
            button.BackColor = Color.WhiteSmoke
            button.ToolTip = "Click and back to the third page"
            Dim trueTypeFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Avant Garde", 9F), True)
            button.Font = trueTypeFont
            button.ForeColor = Color.Black
            Dim destination As PdfDestination = New PdfDestination(doc.Pages(2))
            Dim goToAction As PdfGoToAction = New PdfGoToAction(destination)
            button.Actions.MouseDown = goToAction
            doc.Form.Fields.Add(button)
            doc.SaveToFile("result.pdf", FileFormat.PDF)
            System.Diagnostics.Process.Start("result.pdf")
        End Sub
    End Class
End Namespace

添加效果(截圖):
C# PDF Page操作——設置頁面切換按鈕
點擊按鈕,即可跳轉至指定的文檔第3頁。

關於添加PDF頁面跳轉按鈕的方法,本文到這裏就介紹完畢,希望文中的代碼能給各位開發者提供一定參考價值。
如需轉載,請註明出處。
感謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章