解密 PDF 文件

在对加密的 PDF 文档执行其他操作之前,必须先解锁它。如果您尝试对加密的 PDF 文档进行操作,将会抛出异常。解锁加密的 PDF 后,您可以对其执行一个或多个操作。

使用所有者密码解密 PDF 文件

为了解密 PDF 文件,您需要创建 PdfFileSecurity 对象,然后调用 DecryptFile 方法。您还需要将所有者密码传递给 DecryptFile 方法。以下代码片段向您展示了如何解密 PDF 文件。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void DecryptPDFFile()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var pdfFileInfo = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "sample_encrypted.pdf"))
    {
        if (pdfFileInfo.IsEncrypted)
        {
            using (var fileSecurity = new Aspose.Pdf.Facades.PdfFileSecurity())
            {
                // Bind PDF document
                fileSecurity.BindPdf(dataDir + "sample_encrypted.pdf");
                // Decrypt PDF document
                fileSecurity.DecryptFile("P@ssw0rd");
                // Save PDF document
                fileSecurity.Save(dataDir + "SampleDecrtypted_out.pdf");
            }
        }
    }
}