首页 文章

使用PDFsharp打开受密码保护的PDF

提问于
浏览
0

在我的程序中,我用我指定的密码写出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并为用户输入密码,这样他们就不必输入密码了?

1 回答

  • 0

    如果PDF文件具有用户密码,则Adobe Reader将提示输入密码 .

    您对 PdfReader.Open 的调用未将密码传递给Adobe Reader,因此会再次提示 .

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

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

相关问题