spire.pdf 是一款功能强大的组件,支持给pdf文档添加动作。如打开文档执行的动作,前一个动作完成后执行的动作,关闭文档执行的动作。以下示例将介绍如何给pdf文档添加动作。
添加在打开pdf 文档后执行的动作
c#
//初始化一个pdfdocument实例
pdfdocument document = new pdfdocument();
//设置文本字体
font font=new font("微软雅黑",20f);
pdftruetypefont truetypefont = new pdftruetypefont(font, true);
//添加两张页面并添加文本
pdfpagebase page2 = document.pages.add();
page1.canvas.drawstring("第一张页面",truetypefont, new pdfsolidbrush(color.black), 10, 10);
pdfpagebase page2 = document.pages.add();
page2.canvas.drawstring("第二张页面", truetypefont, new pdfsolidbrush(color.black), 10, 10);
//javascript代码
string script
= "app.alert({"
" cmsg: \"您好,我是第一个动作!\","
" nicon: 3,"
" ctitle: \"javascript action\""
"});";
// 创建pdfjavascriptaction实例 action1
pdfjavascriptaction action1 = new pdfjavascriptaction(script);
//设置action1在打开pdf文档后执行
document.afteropenaction = action1;
vb.net
'初始化一个pdfdocument实例
dim document as new pdfdocument()
'设置文本字体
dim font as new font("微软雅黑", 20f)
dim truetypefont as new pdftruetypefont(font, true)
'添加两张页面并添加文本
dim page2 as pdfpagebase = document.pages.add()
page1.canvas.drawstring("第一张页面", truetypefont, new pdfsolidbrush(color.black), 10, 10)
dim page2 as pdfpagebase = document.pages.add()
page2.canvas.drawstring("第二张页面", truetypefont, new pdfsolidbrush(color.black), 10, 10)
'javascript代码
dim script as [string] = "app.alert({" " cmsg: ""您好,我是第一个动作!""," " nicon: 3," " ctitle: ""javascript action""" "});"
' 创建pdfjavascriptaction实例 action1
dim action1 as new pdfjavascriptaction(script)
'设置action1在打开pdf文档后执行
document.afteropenaction = action1
添加在第一个动作完成后执行的动作
c#
//创建一个pdfgotoaction定向到pdf文档的第2页
pdfdestination dest = new pdfdestination(page1);
pdfgotoaction action2 = new pdfgotoaction(dest);
//设置action2在action1完成后执行,即跳转到pdf的第2页
action1.nextaction = action2;
vb.net
'创建一个pdfgotoaction定向到pdf文档的第2页
dim dest as new pdfdestination(page1)
dim action2 as new pdfgotoaction(dest)
'设置action2在action1完成后执行,即跳转到pdf的第2页
action1.nextaction = action2
添加在关闭pdf文档前执行的动作
c#
script
= "app.alert({"
" cmsg: \"谢谢您的查看,再见!\","
" nicon: 3,"
" ctitle: \"javascript action\""
"});";
pdfjavascriptaction action3 = new pdfjavascriptaction(script);
document.beforecloseaction = action3;
//保存文档
document.savetofile("结果文档.pdf");
vb.net
script = "app.alert({" " cmsg: ""谢谢您的查看,再见!""," " nicon: 3," " ctitle: ""javascript action""" "});"
dim action3 as new pdfjavascriptaction(script)
document.beforecloseaction = action3
'保存文档
document.savetofile("结果文档.pdf")