2016-07-25 28 views
2

我正在瀏覽PDFsharp庫並且遇到了有關密碼保護PDF的問題。PDFsharp 1.50測試版3:向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"; 

我得到System.NullReferenceException: Object reference not set to an instance of an object.,當我到達節省PDF的生產線

document.Save(filename); 我也收到錯誤信息

---- 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) 
+0

您只會收到一條錯誤消息:「System.NullReferenceException」。您不顯示該錯誤的堆棧跟蹤。錯誤是否發生在所有PDF文件中,例如示例代碼附帶的HelloWorld.pdf?如果沒有,請提供您的Hi.pdf以使錯誤重現。錯誤信息之後你會得到斷言信息嗎?首先顯示的消息必須首先進行調查。 –

+0

我沒有想過要檢查PDF,正如你猜測的那樣,HelloWorld.pdf不會發生錯誤。這裏是Hi.pdf https://www.dropbox.com/s/tby9bvw2h60xyj1/hi.pdf?dl=0。我在錯誤消息之前收到斷言消息。 – Ichigo

回答

4

堆棧跟蹤原來有一個與PDFsharp 1.50測試版3.沒有錯,你的代碼有問題。

快速修復,您可以下載PDFsharp源代碼並進行以下修改:

在文件「PdfDocument.cs」的方法,「無效DoSave就會(PdfWriter作家)」尋找「// HACK:刪除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,比PDFsharp 1.50 beta 3更新,不應該要求這個補丁。

+0

非常感謝!代碼現在完美地工作。 – Ichigo