-
C# PDF Page操作——设置页面切换按钮
概述
在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上页或者最后一页,另一种是设置按钮跳转到指定页面。两种方法适应不同的程序设计需要,可自行选择合适的添加方法。
说明
这里的代码示例需要使用类库Spire.PDF for .NET https://www.e-iceblue.cn/Introduce/Spire-PDF-NET.html,版本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页。
关于添加PDF页面跳转按钮的方法,本文介绍到这里,希望文中的代码能给各位开发者提供一定参考价值。
如需转载,请注明出处。
原文链接:https://www.cnblogs.com/Yesi/p/8979938.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数