Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
加密 PDF 文档可以保护其内容不被未经授权的外部访问,特别是在文件共享或归档期间。
机密 PDF 文档可以被加密并设置密码保护。只有知道密码的用户才能解密、打开和查看这些文档。
让我们看看 Aspose.PDF 库中的 PDF 加密是如何工作的。
为了加密 PDF 文件,您需要创建 PdfFileSecurity 对象,然后调用 EncryptFile 方法。您可以将用户密码、所有者密码和权限传递给 EncryptFile 方法。您还需要将 KeySize 和 Algorithm 值传递给此方法。
查看可能的 CryptoAlgorithm 列表:
成员名称 | 值 | 描述 |
---|---|---|
RC4x40 | 0 | 密钥长度为 40 的 RC4。 |
RC4x128 | 1 | 密钥长度为 128 的 RC4。 |
AESx128 | 2 | 密钥长度为 128 的 AES。 |
AESx256 | 3 | 密钥长度为 256 的 AES。 |
以下代码片段演示了如何加密 PDF 文件。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void EncryptPDFFile()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var fileSecurity = new Aspose.Pdf.Facades.PdfFileSecurity())
{
// Bind PDF document
fileSecurity.BindPdf(dataDir + "input.pdf");
// Encrypt file using 256-bit encryption
fileSecurity.EncryptFile("User_P@ssw0rd", "OwnerP@ssw0rd", Aspose.Pdf.Facades.DocumentPrivilege.Print, Aspose.Pdf.Facades.KeySize.x256,
Aspose.Pdf.Facades.Algorithm.AES);
// Save PDF document
fileSecurity.Save(dataDir + "SampleEncrypted_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.