Aspose.PDF 是一款高级PDF处理API,可以在跨平台应用程序中轻松生成,修改,转换,呈现,保护和打印文档。无需使用Adobe Acrobat。此外,API提供压缩选项,表创建和处理,图形和图像功能,广泛的超链接功能,图章和水印任务,扩展的安全控件和自定义字体处理。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
PDF加密是使用密码以及某些加密算法(包括AES或RC4)来保护文档的过程。您还可以设置不同的特权,以限制用户对不同操作的访问。例如,您只能允许打印,添加注释,填写表格等。
在本文中,您将学习如何使用C ++加密PDF文件。此外,本文还介绍了如何使用C ++设置不同的特权和解密受密码保护的PDF文件。
- 使用C ++加密PDF文件
- 使用C ++解密PDF文件
点击下载最新版Aspose.PDF for C++(qun:761297826)https://www.evget.com/product/4118/download
使用C ++加密PDF文件
以下是使用Aspose.PDF for C ++加密PDF文件的步骤。
- 使用Document类加载PDF文档。
- 使用DocumentPrivilege类设置不同的特权,例如允许打印,允许修改内容等。
- 调用Document.Encrypt(String,String,SharedPtr,CryptoAlgorithm,bool)方法来加密PDF。
- 使用Document-> Save(String)方法保存PDF 。
以下代码示例显示了如何使用C ++加密PDF文件。
// Load an existing PDF document auto doc = MakeObject(L"..\\Data\\SecurityAndSignatures\\input.pdf"); // Way1: Using predefined privilege directly. System::SharedPtrprivilege = Aspose::Pdf::Facades::DocumentPrivilege::get_Print(); doc->Encrypt(L"user", L"owner", privilege, CryptoAlgorithm::AESx128, false); doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay1_out.pdf"); // Way2: Based on a predefined privilege and change some specifical permissions. System::SharedPtrprivilege2 = Aspose::Pdf::Facades::DocumentPrivilege::get_AllowAll(); privilege->set_AllowPrint(false); privilege->set_AllowModifyContents(false); doc->Encrypt(L"user", L"owner", privilege2, CryptoAlgorithm::AESx128, false); doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay2_out.pdf"); // Way3: Based on a predefined privilege and change some specifical Adobe Professional permissions combination. System::SharedPtrprivilege3 = Aspose::Pdf::Facades::DocumentPrivilege::get_ForbidAll(); privilege->set_ChangeAllowLevel(1); privilege->set_PrintAllowLevel(2); doc->Encrypt(L"user", L"owner", privilege3, CryptoAlgorithm::AESx128, false); doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay3_out.pdf"); // Way4: Mixes the way2 and way3 System::SharedPtrprivilege4 = Aspose::Pdf::Facades::DocumentPrivilege::get_ForbidAll(); privilege->set_ChangeAllowLevel(1); privilege->set_AllowPrint(true); doc->Encrypt(L"user", L"owner", privilege4, CryptoAlgorithm::AESx128, false); doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay4_out.pdf");
使用C ++解密PDF文件
以下是使用Aspose.PDF for C ++解密PDF文件的步骤。
- 使用Document类加载PDF文件,并提供文档的路径和密码。
- 使用Document-> Decrypt()方法解密文件。
- 使用Document-> Save(String)方法保存解密的PDF 。
以下代码示例显示了如何使用C ++解密PDF。
// Load an existing PDF document auto doc = MakeObject(L"..\\Data\\SecurityAndSignatures\\Decrypt.pdf", L"password"); // Decrypt PDF doc->Decrypt(); // Save the updated document doc->Save(L"..\\Data\\SecurityAndSignatures\\Decrypt_out.pdf");