Search code examples
c#pdfpasswordspdf-generationpdfsharp

PDFsharp 1.50 beta 3: Empty owner password error when adding password to PDF


I'm exploring the PDFsharp library and am having issues password protecting PDFs.

Following this example on their website http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=36&Itemid=47,

Here is my code

 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); }

Whenever I add the code that sets the passwords:

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

I get System.NullReferenceException: Object reference not set to an instance of an object. when I reach the line that saves the PDF

document.Save(filename); I'm also getting error messages

---- 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

Why am I getting these errors?

Edit- here's the stack trace for the 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)

Solution

  • Turns out there is a problem with PDFsharp 1.50 beta 3. Nothing wrong with your code.

    For a quick fix, you can download the PDFsharp source code and make the following change:

    In file "PdfDocument.cs" in method "void DoSave(PdfWriter writer)" look for "// HACK: Remove XRefTrailer" (around line 375) and change it like this (7 new lines replace 2 old lines):

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

    Disclaimer: I tested this with Hi.pdf and HelloWorld.pdf only. While I do hope the new code will never behave worse than the old version, there is no guarantee.

    Future versions of PDFsharp, newer than PDFsharp 1.50 beta 3, should not require this patch.