【问题标题】:Opening a Password Protected PDF Using PDFsharp使用 PDFsharp 打开受密码保护的 PDF
【发布时间】:2016-05-03 10:00:02
【问题描述】:

在我的程序中,我使用指定的密码写掉 PDF。这很重要,因为我知道我试图打开的 PDF 的密码(不是破解它或任何东西)。我像这样用密码保护PDF;

Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
    string sourcePath = sourceFilePath;
    string targetPath = @"C:\ExamplePath";

    useReturnedOHPath = true;

    string sourceFile = Path.Combine(sourcePath, fileName);

    var document = PdfReader.Open(sourceFile);
    var securitySettings = document.SecuritySettings;
    securitySettings.UserPassword = "ExamplePass";

    securitySettings.PermitAccessibilityExtractContent = false;
    securitySettings.PermitAnnotations = false;
    securitySettings.PermitAssembleDocument = false;
    securitySettings.PermitExtractContent = false;
    securitySettings.PermitFormsFill = true;
    securitySettings.PermitFullQualityPrint = false;
    securitySettings.PermitModifyDocument = true;
    securitySettings.PermitPrint = false;

    document.Save(sourceFile);
    MessageBox.Show(sourceFile);

    string cleanPath = CleanFileName(selectedPerson.ID + " " + DateTime.Now.ToString("dd-MM-yyyy hh-mm-ss") + ".pdf");
    ohDestFile = Path.Combine(targetPath, cleanPath);

    File.Copy(sourceFile, ohDestFile, true);
}), DispatcherPriority.ContextIdle);

因此,我知道 PDF 的密码是 ExamplePass。现在,当我从我的程序中打开 PDF 时,我尝试了几种方法,很简单;

if (selectedOHRecord.Path != string.Empty)
{
     Process.Start(selectedOHRecord.Path);
}

不过,这只是打开 Acrobat 并要求输入密码,这是可以理解的。我也尝试过添加:

PdfDocument document = PdfReader.Open(selectedOHRecord.Path, "ExamplePass");

这取自 PDFsharp 网站本身,但是当我调用它时,什么都没有发生。有没有一种方法可以打开 PDF 并为用户输入密码,这样他们就不必输入密码了?

【问题讨论】:

  • 密码的用途是什么?
  • @PDFsharpTeam 我们希望防止在打开它的程序之外打开 PDF 文件,即防止用户在 Windows 资源管理器中打开文件
  • PDFsharp API 完全独立于 Adob​​e。无法将密码从 PDFsharp 传递到 Adob​​e Reader。而且 AFAIK 无法使用命令行将密码传递给 Adob​​e Reader。
  • @CBreeze 仅供参考:>> 当我打电话给PdfDocument document = PdfReader.Open(selectedOHRecord.Path, "ExamplePass"); 时,什么也没有发生。

标签: c# wpf pdf pdfsharp


【解决方案1】:

如果 PDF 文件有用户密码,Adobe Reader 会提示输入密码。

您对PdfReader.Open 的调用未将密码传递给 Adob​​e Reader,因此它再次提示。

您可以设置所有者密码而不是用户密码。 Adobe Reader打开文件时没有提示,但会阻止编辑。

或如取消保护示例中所示删除密码,将没有密码的 PDF 文件保存到临时文件中,然后使用 Adob​​e Reader 打开该文件。请注意,用户可以将文件从 Adob​​e Reader 保存到他们选择的文件夹中,以保留不受保护的副本。
http://www.pdfsharp.net/wiki/UnprotectDocument-sample.ashx

【讨论】:

    最近更新 更多