使用LEADTOOLS提供的PDFFile类也使PDF加密、解密和密码保护变得简单!
使用LEADTOOLS提供的PDFFile类也使PDF加密、解密和密码保护变得简单!
Leadtools提供两种类型的PDF密码保护。
- 用户密码:需要查看PDF。使用该密码可以对PDF文件进行加密。在Acrobat文档中也被称为 “文档打开密码”。
- 业主密码:不需要查看文件,但需要改变权限,如复制内容和打印。在Acrobat文档中也被称为 “权限密码”。
如果两个密码都应用于一个PDF,那么任何一个密码都可以用来查看该PDF文档。在设置用户密码时,你可以指定RC40位或RC128位加密。
下面是一些C#、VB和Java代码片段,可用于对PDF文件进行密码保护。
1. 在C#中加密PDF
private static void Main(string[] args){ // Check to make sure number of args is correct, files exist, etc... if (FailsPreFlight(args)) return; try { ProtectPdfFile(args[0], args[1]); } catch (Exception ex) { Console.WriteLine(ex.Message); }}private static void ProtectPdfFile(string destinationPdf, string sourcePdf){ string sourcePdfPassword = PDFFile.IsEncrypted(sourcePdf) "User_9@55w0Rd" : ""; new PDFFile(sourcePdf, sourcePdfPassword) { SecurityOptions = new PDFSecurityOptions { UserPassword = "User_9@55w0Rd", OwnerPassword = "Owner_9@55w0Rd", EncryptionMode = PDFEncryptionMode.RC128Bit, AssemblyEnabled = false, AnnotationsEnabled = true, CopyEnabled = false, CopyForAccessibilityEnabled = false, EditEnabled = false, PrintEnabled = false, HighQualityPrintEnabled = false, FormFieldFillingEnabled = false } }.Convert(1, -1, destinationPdf);}
2. 在VB中加密PDF
Dim sourcePdfPassword As String = If(PDFFile.IsEncrypted(sourcePdf), "User_9@55w0Rd", "")New PDFFile(sourcePdf, sourcePdfPassword) With { .SecurityOptions = New PDFSecurityOptions With { .UserPassword = "User_9@55w0Rd", .OwnerPassword = "Owner_9@55w0Rd", .EncryptionMode = PDFEncryptionMode.RC128Bit, .AssemblyEnabled = False, .AnnotationsEnabled = True, .CopyEnabled = False, .CopyForAccessibilityEnabled = False, .EditEnabled = False, .PrintEnabled = False, .HighQualityPrintEnabled = False, .FormFieldFillingEnabled = False }}.Convert(1, -1, destinationPdf)
3. 在Java中加密PDF
String sourcePdfPassword = PDFFile.isEncrypted(sourcePdf) "User_9@55w0Rd" : "";PDFSecurityOptions securityOptions = new PDFSecurityOptions();securityOptions.setUserPassword("User_9@55w0Rd");securityOptions.setOwnerPassword("Owner_9@55w0Rd");securityOptions.setEncryptionMode(PDFEncryptionMode.RC128_BIT);securityOptions.setAssemblyEnabled(false);securityOptions.setAnnotationsEnabled(true);securityOptions.setCopyEnabled(false);securityOptions.setCopyForAccessibilityEnabled(false);securityOptions.setEditEnabled(false);securityOptions.setFormFieldFillingEnabled(false);securityOptions.setHighQualityPrintEnabled(false);securityOptions.setPrintEnabled(false);PDFFile pdfFile = new PDFFile(sourcePdf, sourcePdfPassword);pdfFile.setSecurityOptions(securityOptions);pdfFile.convert(1, -1, destinationPdf);
这里用到的SDK是LEADTOOLS PDF Pro Developer Toolkit,点击这里即可下载。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!