如何通过编程打开受密码保护的PDF文件?

4
Adobe IFilter没有提供一种机制来提供密码以打开受密码保护的PDF文件,因此不能用于打开受密码保护的文件。
我想知道,是否有相对简单的方法以编程方式检索PDF文件中的实际加密数据,使用标准密码学API将其解密,然后构建包含已解密数据的新PDF文件?
2个回答

2
如果您使用SpirePDF,则可以像这样从加密的PDF中获取页面图像:
using System;
using System.Drawing;
using Spire.Pdf;
namespace PDFDecrypt
{
    class Decrypt
    {
        static void Main(string[] args)
        {
            //Create Document
            String encryptedPdf = @"D:\work\My Documents\Encryption.pdf";
            PdfDocument doc = new PdfDocument(encryptedPdf, "123456");

            //Extract Image
            Image image = doc.Pages[0].ImagesInfo[0].Image;

            doc.Close();

            //Save
            image.Save("EmployeeInfo.png", System.Drawing.Imaging.ImageFormat.Png);

            //Launch
            System.Diagnostics.Process.Start("EmployeeInfo.png");
        }
    }
}

2
打开一个受密码保护的PDF文件,您需要开发至少一个PDF解析器、解密器和生成器。虽然我不建议这样做,因为这并不是一项容易完成的任务。
借助PDF库,一切都变得简单得多。您可以尝试使用Docotic.Pdf库来完成此任务(免责声明:我是该库的供应商)。
以下是您的任务示例:
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还可以从PDF中提取文本(格式化或非格式化)。这可能对索引很有用(我猜这就是你要做的,因为你提到了Adobe IFilter)。

假设用户已知密码。 - stephanmg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接