C# PDF Page操作设置页面切换按钮的方法

所属分类: 软件编程 / C#教程 阅读数: 124
收藏 0 赞 0 分享

概述

在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上页或者最后一页,另一种是设置按钮跳转到指定页面。两种方法适应不同的程序设计需要,可自行选择合适的添加方法。

说明

这里的代码示例需要使用类库Spire.PDF for .NET,版本4.0 。在使用该类库时,在项目程序中引用Spire.Pdf.dll即可(dll文件在安装路径下的Bin文件中获取)。

如:

代码操作示例(供参考)

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");
 }
 }
}

PS:这里的PdfNameAction类支持四种按钮跳转动作

添加效果(截图):

点击文中的按钮时,即可跳转至按钮指向的页面。

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");
 }
 }
}

添加效果(截图):

点击按钮,即可跳转至指定的文档第3页。

以上所述是小编给大家介绍的C# PDF Page操作设置页面切换按钮的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多