PDFsharp 1.50测试版3:将密码添加到PDF时出现空所有者密码错误
本文关键字:密码 PDF 错误 所有者 添加 测试版 PDFsharp | 更新日期: 2023-09-27 17:58:36
我正在探索PDFsharp库,遇到了密码保护PDF的问题。
在他们的网站上以这个例子为例http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&任务=视图&id=36&项目ID=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源代码并进行以下更改:
在文件"PdfDocument.cs"中,方法"void DoSave(PdfWriter writer)"中查找"//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的未来版本,比PDFsharp1.50beta 3更新,不应该需要此补丁。