我正在瀏覽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)
您只會收到一條錯誤消息:「System.NullReferenceException」。您不顯示該錯誤的堆棧跟蹤。錯誤是否發生在所有PDF文件中,例如示例代碼附帶的HelloWorld.pdf?如果沒有,請提供您的Hi.pdf以使錯誤重現。錯誤信息之後你會得到斷言信息嗎?首先顯示的消息必須首先進行調查。 –
我沒有想過要檢查PDF,正如你猜測的那樣,HelloWorld.pdf不會發生錯誤。這裏是Hi.pdf https://www.dropbox.com/s/tby9bvw2h60xyj1/hi.pdf?dl=0。我在錯誤消息之前收到斷言消息。 – Ichigo