本文介绍如何使用spire.doc for .net将word转为pdf时设置pdf文档密码保护,可设置pdf文档打开密码保护以及权限密码保护。附vb.net代码,有需要可供参考。
c#
using spire.doc;
using spire.pdf.security;
namespace wordtopdfandencrypt_pdf
{
class program
{
static void main(string[] args)
{
//加载word测试文档
document doc = new document();
doc.loadfromfile("test.docx");
//转为pdf时,设置pdf打开密码和权限密码
topdfparameterlist topdf = new topdfparameterlist();
topdf.pdfsecurity.encrypt("open", "permission", pdfpermissionsflags.print | pdfpermissionsflags.copycontent, pdfencryptionkeysize.key128bit);
//将文档保存为pdf格式
doc.savetofile("result.pdf", topdf);
system.diagnostics.process.start("result.pdf");
}
}
}
vb.net
imports spire.doc
imports spire.pdf.security
namespace wordtopdfandencrypt_pdf
class program
private shared sub main(args as string())
'加载word测试文档
dim doc as new document()
doc.loadfromfile("test.docx")
'转为pdf时,设置pdf打开密码和权限密码
dim topdf as new topdfparameterlist()
topdf.pdfsecurity.encrypt("open", "permission", pdfpermissionsflags.print or pdfpermissionsflags.copycontent, pdfencryptionkeysize.key128bit)
'将文档保存为pdf格式
doc.savetofile("result.pdf", topdf)
system.diagnostics.process.start("result.pdf")
end sub
end class
end namespace
执行程序,生成文档。打开生成的pdf文件后,提示需输入密码,如图: