口令包含了文档打开口令和许可口令。文档打开口令只能用于打开文档,而许可口令则是级别更高的口令,它既可以打开文档,也可以用来编辑和修改文档的权限。我们可以对一个文档设置打开口令或许可口令中的一种,也可以二者都设置。
文档打开口令保护
c#
//新建pdf文档
pdfdocument pdf = new pdfdocument();
//添加新页面
pdfpagebase page = pdf.pages.add();
//添加文本
page.canvas.drawstring("test encryption with aes 256bit by spire.pdf", new pdffont(pdffontfamily.helvetica, 13f), pdfbrushes.black, pointf.empty);
//设置文档加密级别
pdf.security.keysize = pdfencryptionkeysize.key256bit;
//设置文档打开口令
pdf.security.userpassword = "e-iceblue";
//保存文档
pdf.savetofile("aes-256bit.pdf");
vb.net
'新建pdf文档
dim pdf as new pdfdocument()
'添加新页面
dim page as pdfpagebase = pdf.pages.add()
'添加文本
page.canvas.drawstring("test encryption with aes 256bit by spire.pdf", new pdffont(pdffontfamily.helvetica, 13f), pdfbrushes.black, pointf.empty)
'设置文档加密级别
pdf.security.keysize = pdfencryptionkeysize.key256bit
'设置文档打开口令
pdf.security.userpassword = "e-iceblue"
'保存文档
pdf.savetofile("aes-256bit.pdf")
效果图:
许可口令保护
在设置许可口令保护时,我们可以设置文档权限。文档权限可分为允许打印、允许修改、启用复制文本、图像和其他内容等。
c#
//新建pdf文档
pdfdocument pdf = new pdfdocument();
//添加新页面
pdfpagebase page = pdf.pages.add();
//添加文本
page.canvas.drawstring("this document is protected with owner password, and the following permissions are granted when the document is opened: print, fillfields, copycontent", new pdffont(pdffontfamily.helvetica, 13f), pdfbrushes.black, new rectanglef(0, 0, 480, 300));
//设置文档加密级别
pdf.security.keysize = pdfencryptionkeysize.key128bit;
//设置许可口令
pdf.security.ownerpassword = "e-iceblue";
//设置文档权限
pdf.security.permissions = pdfpermissionsflags.print | pdfpermissionsflags.fillfields | pdfpermissionsflags.copycontent;
//保存文档
pdf.savetofile("permissions.pdf");
vb.net
'新建pdf文档
dim pdf as new pdfdocument()
'添加新页面
dim page as pdfpagebase = pdf.pages.add()
'添加文本
page.canvas.drawstring("this document is protected with owner password, and the following permissions are granted when the document is opened: print, fillfields, copycontent", new pdffont(pdffontfamily.helvetica, 13f), pdfbrushes.black, new rectanglef(0, 0, 480, 300))
'设置文档加密级别
pdf.security.keysize = pdfencryptionkeysize.key128bit
'设置许可口令
pdf.security.ownerpassword = "e-iceblue"
'设置文档权限
pdf.security.permissions = pdfpermissionsflags.print or pdfpermissionsflags.fillfields or pdfpermissionsflags.copycontent
'保存文档
pdf.savetofile("permissions.pdf")
效果图:
取消文档打开口令
c#
//加载pdf文档
pdfdocument pdf = new pdfdocument("aes-256bit.pdf", "e-iceblue");
//取消文档打开口令
pdf.security.userpassword = string.empty;
//保存文档
pdf.savetofile("nouserpassword.pdf");
vb.net
'加载pdf文档
dim pdf as new pdfdocument("aes-256bit.pdf", "e-iceblue")
'取消文档打开口令
pdf.security.userpassword = string.empty
'保存文档
pdf.savetofile("nouserpassword.pdf")
取消许可口令
c#
//加载pdf文档
pdfdocument pdf = new pdfdocument("permissions.pdf", "e-iceblue");
//取消许可口令
pdf.security.ownerpassword = string.empty;
//重置文档权限到默认状态
pdf.security.permissions = pdfpermissionsflags.default;
//保存文档
pdf.savetofile("noownerpassword.pdf");
vb.net
'加载pdf文档
dim pdf as new pdfdocument("permissions.pdf", "e-iceblue")
'取消许可口令
pdf.security.ownerpassword = string.empty
'重置文档权限到默认状态
pdf.security.permissions = pdfpermissionsflags.[default]
'保存文档
pdf.savetofile("noownerpassword.pdf")