gpt4 book ai didi

c# - PDFsharp 1.50 测试版 3 : Empty owner password error when adding password to PDF

转载 作者:行者123 更新时间:2023-11-30 20:34:58 25 4
gpt4 key购买 nike

我正在探索 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";

我得到 System.NullReferenceException: Object reference not set to an instance of an object. 当我到达保存 PDF 的行时

文档.保存(文件名);我也收到错误消息

---- 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 的 future 版本,比 PDFsharp 1.50 beta 3 更新,应该不需要这个补丁。

关于c# - PDFsharp 1.50 测试版 3 : Empty owner password error when adding password to PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com