尽管常见的pdf阅读器(如adobe reader、foxit reader)都有页面跳转按钮,有时候我们希望能直接点击文档某处,跳转至相应页面。本文将介绍如何使用spire.pdf实现页面跳转。
跳转至特殊页(上一页、下一页、人生就是博尊龙凯时首页、最后一页)
c#
//加载pdf文档
pdfdocument doc = new pdfdocument();
doc.loadfromfile("c# tutorials.pdf");
//允许添加form
doc.allowcreateform = true;
//获取最后一页
pdfpagebase lastpage = doc.pages[doc.pages.count - 1];
//在页面指定位置添加按钮
pdfbuttonfield button = new pdfbuttonfield(lastpage, "返回人生就是博尊龙凯时首页");
button.bounds = new rectanglef(lastpage.actualsize.width - 110, lastpage.actualsize.height - 250, 60, 20);
//设置按钮边框颜色
button.borderstyle = pdfborderstyle.solid;
button.bordercolor = new pdfrgbcolor(color.lightpink);
//设置按钮背景色
button.backcolor = color.green;
//设置按钮提示语
button.tooltip = "点击此处返回人生就是博尊龙凯时首页";
//设置按钮文字字体和颜色
pdftruetypefont truetypefont = new pdftruetypefont(new font("宋体", 10f), 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);
vb.net
'加载pdf文档
dim doc as new pdfdocument()
doc.loadfromfile("c# tutorials.pdf")
'允许添加form
doc.allowcreateform = true
'获取最后一页
dim lastpage as pdfpagebase = doc.pages(doc.pages.count - 1)
'在页面指定位置添加按钮
dim button as new pdfbuttonfield(lastpage, "返回人生就是博尊龙凯时首页")
button.bounds = new rectanglef(lastpage.actualsize.width - 110, lastpage.actualsize.height - 250, 60, 20)
'设置按钮边框颜色
button.borderstyle = pdfborderstyle.solid
button.bordercolor = new pdfrgbcolor(color.lightpink)
'设置按钮背景色
button.backcolor = color.green
'设置按钮提示语
button.tooltip = "点击此处返回人生就是博尊龙凯时首页"
'设置按钮文字字体和颜色
dim truetypefont as new pdftruetypefont(new font("宋体", 10f), true)
button.font = truetypefont
button.forecolor = color.black
'创建pdfnamedaction实例,在传入的参数中选择上一页、下一页、人生就是博尊龙凯时首页或最后一页
dim namedaction as new pdfnamedaction(pdfactiondestination.firstpage)
'应用动作
button.actions.mousedown = namedaction
'添加按钮到文档
doc.form.fields.add(button)
'保存pdf文档
doc.savetofile("result.pdf", fileformat.pdf)
跳转至指定页
c#
//加载pdf文档
pdfdocument doc = new pdfdocument();
doc.loadfromfile("c# tutorials.pdf");
//允许添加form
doc.allowcreateform = true;
//获取最后一页
pdfpagebase lastpage = doc.pages[doc.pages.count - 1];
//在页面指定位置添加按钮
pdfbuttonfield button = new pdfbuttonfield(lastpage, "返回至第三页");
button.bounds = new rectanglef(lastpage.actualsize.width - 150, lastpage.actualsize.height - 250, 100, 20);
//设置按钮边框颜色
button.borderstyle = pdfborderstyle.solid;
button.bordercolor = new pdfrgbcolor(color.lightpink);
//设置按钮背景色
button.backcolor = color.lightblue;
//设置按钮提示语
button.tooltip = "点击此处返回至第三页";
//设置按钮文字字体和颜色
pdftruetypefont truetypefont = new pdftruetypefont(new font("宋体", 10f), true);
button.font = truetypefont;
button.forecolor = color.black;
//实例化pdfdestination对象,传入指定页码
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);
vb.net
'加载pdf文档
dim doc as new pdfdocument()
doc.loadfromfile("c# tutorials.pdf")
'允许添加form
doc.allowcreateform = true
'获取最后一页
dim lastpage as pdfpagebase = doc.pages(doc.pages.count - 1)
'在页面指定位置添加按钮
dim button as new pdfbuttonfield(lastpage, "返回至第三页")
button.bounds = new rectanglef(lastpage.actualsize.width - 150, lastpage.actualsize.height - 250, 100, 20)
'设置按钮边框颜色
button.borderstyle = pdfborderstyle.solid
button.bordercolor = new pdfrgbcolor(color.lightpink)
'设置按钮背景色
button.backcolor = color.lightblue
'设置按钮提示语
button.tooltip = "点击此处返回至第三页"
'设置按钮文字字体和颜色
dim truetypefont as new pdftruetypefont(new font("宋体", 10f), true)
button.font = truetypefont
button.forecolor = color.black
'实例化pdfdestination对象,传入指定页码
dim destination as new pdfdestination(doc.pages(2))
'创建go to动作
dim gotoaction as new pdfgotoaction(destination)
'应用动作
button.actions.mousedown = gotoaction
'添加按钮到文档
doc.form.fields.add(button)
'保存pdf文档
doc.savetofile("result.pdf", fileformat.pdf)