Skip to main content
 首页 » 编程设计

c#之如何以编程方式打开受密码保护的 PDF 文件

2024年02月20日12haluo1

Adobe IFilter 不提供提供密码以打开受密码保护的 PDF 文件的机制,因此它不能用于打开受密码保护的文件。

我想知道,是否有一种相对简单的方法以编程方式检索 PDF 文件中的实际加密数据,使用标准加密 API 对其进行解密,然后使用解密后的数据构建新的 PDF 文件?

请您参考如下方法:

要打开受密码保护的 PDF,您至少需要开发 PDF 解析器、解密器和生成器。不过,我不建议这样做。这绝非易事。

在 PDF 库的帮助下,一切都变得简单多了。您可能想尝试 Docotic.Pdf library任务(免责声明:我为图书馆的供应商工作)。

这是您的任务示例:

public static void unprotectPdf(string input, string output) 
{ 
    bool passwordProtected = PdfDocument.IsPasswordProtected(input); 
    if (passwordProtected) 
    { 
        string password = null; // retrieve the password somehow 
 
        using (PdfDocument doc = new PdfDocument(input, password)) 
        { 
            // clear both passwords in order 
            // to produce unprotected document 
            doc.OwnerPassword = ""; 
            doc.UserPassword = ""; 
 
            doc.Save(output); 
        } 
    } 
    else 
    { 
        // no decryption is required 
        File.Copy(input, output, true); 
    } 
} 

Docotic.Pdf也可以extract text (formatted or not) from PDFs .它可能对索引有用(我猜这就是你要做的,因为你提到了 Adob​​e IFilter)