我正在探索PDFsharp库,并且遇到保护PDF的密码问题。

在他们的网站http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=36&Itemid=47上遵循此示例,

这是我的代码

 try
 {
   string filename = "hi.pdf";
   File.Copy(Path.Combine("C:/User/Ichigo/Desktop", filename),
   Path.Combine(Directory.GetCurrentDirectory(), filename), true);

   PdfDocument document = PdfReader.Open(filename, "some text");

   PdfSecuritySettings securitySettings = document.SecuritySettings;

   securitySettings.UserPassword="user";
   securitySettings.OwnerPassword="owner";

   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(filename);
 }
 catch (Exception e)
 { throw new Exception("Something went wrong : " + e); }


每当我添加设置密码的代码时:

securitySettings.UserPassword="user";
securitySettings.OwnerPassword="owner";


当我到达保存PDF的行时得到System.NullReferenceException: Object reference not set to an instance of an object.

document.Save(文件名);
我也收到错误消息

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
Empty owner password.
---- Assert Long Message ----

   at PdfSharp.Pdf.Security.PdfStandardSecurityHandler.PrepareEncryption() in c:\Users\Ichigo\Downloads\PDFsharp-MigraDocFoundation-1_50-beta3b\PDFsharp\src\PdfSharp\Pdf.Security\PdfStandardSecurityHandler.cs:line 590
   at PdfSharp.Pdf.PdfDocument.DoSave(PdfWriter writer) in c:\Users\Ichigo\Downloads\PDFsharp-MigraDocFoundation-1_50-beta3b\PDFsharp\src\PdfSharp\Pdf\PdfDocument.cs:line 392
   at PdfSharp.Pdf.PdfDocument.Save(Stream stream, Boolean closeStream) in c:\Users\Ichigo\Downloads\PDFsharp-MigraDocFoundation-1_50-beta3b\PDFsharp\src\PdfSharp\Pdf\PdfDocument.cs:line 325
   at PdfSharp.Pdf.PdfDocument.Save(Stream stream) in c:\Users\Ichigo\Downloads\PDFsharp-MigraDocFoundation-1_50-beta3b\PDFsharp\src\PdfSharp\Pdf\PdfDocument.cs:line 352
   at PdfSharp.Pdf.PdfDocument.Save(String path) in c:\Users\Ichigo\Downloads\PDFsharp-MigraDocFoundation-1_50-beta3b\PDFsharp\src\PdfSharp\Pdf\PdfDocument.cs:line 258


为什么会出现这些错误?

编辑-这是System.NullReferenceException.的堆栈跟踪

Something went wrong : `System.NullReferenceException: Object reference not set to an instance of an object.`
 at PdfSharp.Pdf.Security.PdfStandardSecurityHandler.SetHashKey(PdfObjectID id)
   at PdfSharp.Pdf.IO.PdfWriter.WriteBeginObject(PdfObject obj)
   at PdfSharp.Pdf.PdfDictionary.WriteObject(PdfWriter writer)
   at PdfSharp.Pdf.PdfPage.WriteObject(PdfWriter writer)
   at PdfSharp.Pdf.PdfDocument.DoSave(PdfWriter writer)
   at PdfSharp.Pdf.PdfDocument.Save(Stream stream, Boolean closeStream)
   at PdfSharp.Pdf.PdfDocument.Save(Stream stream)
   at PdfSharp.Pdf.PdfDocument.Save(String path)

最佳答案

原来PDFsharp 1.50 beta 3有问题。您的代码没有错。

要快速修复,您可以下载PDFsharp源代码并进行以下更改:

在方法“ void DoSave(PdfWriter writer)”的文件“ PdfDocument.cs”中,查找“ // HACK:Remove XRefTrailer”(在第375行附近)并进行如下更改(7个新行替换了2个旧行):

// HACK: Remove XRefTrailer
if (_trailer is PdfCrossReferenceStream)
{
    // HACK^2: Preserve the SecurityHandler.
    PdfStandardSecurityHandler securityHandler = _securitySettings.SecurityHandler;
    _trailer = new PdfTrailer((PdfCrossReferenceStream) _trailer);
    _trailer._securityHandler = securityHandler;
}


免责声明:我仅使用Hi.pdf和HelloWorld.pdf对此进行了测试。尽管我确实希望新代码的性能永远不会比旧版本差,但并不能保证。

比PDFsharp 1.50 beta 3更高的未来版本的PDFsharp不需要此修补程序。

10-08 07:50