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

问题描述 投票:0回答:2

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

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

c# .net pdf password-protection
2个回答
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)


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");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.