C# PDF Page操作设置页面切换按钮的方法
作者:E-iceblue 时间:2021-11-18 04:33:43
概述
在以下示例中,将介绍在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操作设置页面切换按钮的方法网站的支持!
来源:https://www.cnblogs.com/Yesi/p/8979938.html
标签:C#,PDF,Page
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java音乐播放器编写源码
2022-08-02 21:06:44
![](https://img.aspxhome.com/file/2023/1/65761_0s.jpg)
如何在IDEA Maven项目中导入本地jar包的步骤
2023-03-25 06:43:48
![](https://img.aspxhome.com/file/2023/5/90015_0s.png)
Java实现Flappy Bird游戏源码
2022-11-02 16:55:29
![](https://img.aspxhome.com/file/2023/9/82999_0s.png)
Mybatis分页插件使用方法详解
2021-10-12 06:59:30
WinForm中的几个实用技巧汇总
2022-08-12 13:17:02
Java值传递之swap()方法不能交换的解决
2023-11-12 20:54:50
![](https://img.aspxhome.com/file/2023/4/59594_0s.jpg)
idea中同一SpringBoot项目多端口启动
2023-05-09 06:39:27
![](https://img.aspxhome.com/file/2023/0/88060_0s.png)
java高并发ScheduledThreadPoolExecutor与Timer区别
2023-08-11 03:08:29
Java解析json报文实例解析
2023-07-18 21:33:58
基于C#实现的木马程序实例详解
2023-02-25 20:05:03
Mybatis实现分表插件
2023-07-23 12:41:54
![](https://img.aspxhome.com/file/2023/2/80292_0s.gif)
java反射获取包下所有类的操作
2022-02-12 16:06:15
java常用工具类 Random随机数、MD5加密工具类
2023-02-14 17:55:08
详解Java8中Optional的常见用法
2022-04-30 14:32:21
详解Spring Bean的配置方式与实例化
2022-01-13 05:47:51
![](https://img.aspxhome.com/file/2023/9/89559_0s.jpg)
Java中ArrayList和LinkedList之间的区别_动力节点Java学院整理
2023-03-30 18:16:29
基于idea Maven中的redis配置使用详解
2023-11-29 11:57:28
探讨:android项目开发 统筹兼顾 需要考虑的因素
2023-08-05 11:21:32
maven profile动态选择配置文件详解
2023-11-05 22:05:20
![](https://img.aspxhome.com/file/2023/4/64514_0s.png)
Intellij IDEA中启动多个微服务(开启Run Dashboard管理)
2022-01-11 02:25:00
![](https://img.aspxhome.com/file/2023/8/62708_0s.png)