我正在探索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)
答案 0 :(得分:4)
原来是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的未来版本,比PDFsharp 1.50 beta 3更新,不应该需要此补丁。