快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C#PDFPage操作——设置页面切换按钮

概述

在以下示例中,将介绍在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页面跳转按钮的方法,本文到这里就介绍完毕,希望文中的代码能给各位开发者提供一定参考价值。
如需转载,请注明出处。
感谢阅读!


分享文章:C#PDFPage操作——设置页面切换按钮
文章地址:http://6mz.cn/article/jeeosj.html

其他资讯