当前位置: 移动技术网 > IT编程>开发语言>c# > C# PDF Page操作设置页面切换按钮的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
概述 在以下示例中,将介绍在pdf文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上页或者最后一页,另一种是设置

概述

在以下示例中,将介绍在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操作设置页面切换按钮的方法,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网