Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
>>Aspose.PDF for .NET更新至最新版v20.4,欢迎下载体验。
在现有的pdf文件上设置权限
要在PDF文件上设置特权,请创建DocumentPrivilege类的对象,然后指定要应用于文档的权限。定义特权后,将此对象作为参数传递给该Document对象的Encrypt(..)方法。以下代码段显示了如何设置PDF文件的权限。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();// Load a source PDF fileusing (Document document = new Document(dataDir + "input.pdf")){ // Instantiate Document Privileges object // Apply restrictions on all privileges DocumentPrivilege documentPrivilege = DocumentPrivilege.ForbidAll; // Only allow screen reading documentPrivilege.AllowScreenReaders = true; // Encrypt the file with User and Owner password. // Need to set the password, so that once the user views the file with user password, // Only screen reading option is enabled document.Encrypt("user", "owner", documentPrivilege, CryptoAlgorithm.AESx128, false); // Save updated document document.Save(dataDir + "SetPrivileges_out.pdf");}
使用不同的加密类型和算法加密PDF文件
使用Document对象的Encrypt方法来加密PDF文件。您可以将用户密码,所有者密码和权限传递给Encrypt方法。除此之外,还可以传递CryptoAlgorithm枚举的任何值。该枚举提供了加密算法和密钥大小的不同组合。最后,使用Document对象的Save方法保存加密的PDF文件。
注意:
- 用户密码:为了打开PDF提供。Acrobat / Reader将提示用户输入用户密码。如果不正确,则该文档将不会打开。
以下代码段显示了如何加密PDF文件。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();// Open documentDocument document = new Document(dataDir+ "Encrypt.pdf");// Encrypt PDFdocument.Encrypt("user", "owner", 0, CryptoAlgorithm.RC4x128);dataDir = dataDir + "Encrypt_out.pdf";// Save updated PDFdocument.Save(dataDir);
使用所有者密码解密PDF文件
为了解密PDF文件,首先需要创建一个Document对象,然后使用所有者的密码打开PDF。之后,需要调用Document对象的Decrypt方法。最后,使用Document对象的Save方法保存更新的PDF文件。以下代码段显示了如何解密PDF文件。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();// Open documentDocument document = new Document(dataDir+ "Decrypt.pdf", "password");// Decrypt PDFdocument.Decrypt();dataDir = dataDir + "Decrypt_out.pdf";// Save updated PDFdocument.Save(dataDir);
更改PDF文件的密码
如果要更改PDF文件的密码,首先需要使用带有Document对象的所有者密码来打开PDF文件。之后,需要调用Document对象的 ChangePasswords方法。您需要将当前的所有者密码以及新的用户密码和新的所有者密码传递给此方法。最后,使用Document对象的Save方法保存更新的PDF文件。
以下代码段显示了如何更改PDF文件的密码。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();// Open documentDocument document = new Document(dataDir+ "ChangePassword.pdf", "owner");// Change passworddocument.ChangePasswords("owner", "newuser", "newowner");dataDir = dataDir + "ChangePassword_out.pdf";// Save updated PDFdocument.Save(dataDir);
还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!