You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iText7 v8.0.1 .NET6程序Win10正常,macOS Ventura崩溃求助

问题:macOS Ventura下iText7调用pdfDoc.Close()时程序崩溃

我编写了一段控制台代码,用于为输入文件pdfMaster.pdf生成带密码保护的副本,并输出到原文件同目录。这段代码在Win10上运行完全正常,但在macOS Ventura系统中,执行pdfDoc.Close();语句时程序直接崩溃。

最初我怀疑是Mac系统的文件创建权限问题,但注释掉pdfDoc.Close();后,后续的File.WriteAllText调用能正常工作并创建测试文件,说明权限并非问题所在。

程序相关的所有DLL及文件都位于同一个Mac目录中,我通过Terminal进入该目录运行应用,尝试过以下两种命令:

  • dotnet Watermark.dll
  • sudo dotnet Watermark.dll
    但结果完全一致。我猜测问题要么出在操作方式上,要么是pdfDoc.Close()方法本身或Mac平台的.NET6存在异常。

既然标准的File.WriteAllText方法可以正常创建文件,我想了解是否可以通过iText7将加密后的PDF内容写入字节数组,再通过File.WriteAllBytes方法写入文件。

代码实现

using iText.Kernel.Pdf;
using System.Text;

namespace Watermark
{
   internal class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Watermark!");

         PdfReader reader = new PdfReader(@"pdfMaster.pdf");

         byte[] userPW = Encoding.ASCII.GetBytes("My");
         byte[] ownerPW = Encoding.ASCII.GetBytes("PW");

         WriterProperties props = new WriterProperties().
            SetStandardEncryption(userPW, ownerPW,
            EncryptionConstants.ALLOW_PRINTING,
            EncryptionConstants.ENCRYPTION_AES_128 |
            EncryptionConstants.DO_NOT_ENCRYPT_METADATA);
         
         PdfWriter writer = new PdfWriter(@"pdfOUT_PW.pdf", props);
         PdfDocument pdfDoc = new PdfDocument(reader, writer);
         pdfDoc.Close();

         File.WriteAllText("pdfOutputTest.pdf", "File.WriteAllText test\n");
      }
   }
}

macOS崩溃信息

admin@Macs-Mac watermark % dotnet watermark.dll
realpath(watermark.dll) failed: Operation not permitted
getcwd() failed: Operation not permitted
System.TypeInitializationException: The type initializer for 'Microsoft.DotNet.Cli.Parser' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'Microsoft.DotNet.Cli.AddCommandParser' threw an exception.
 ---> System.UnauthorizedAccessException: Access to the path is denied.
 ---> System.IO.IOException: Operation not permitted
   --- End of inner exception stack trace ---
   at Interop.Sys.GetCwdHelper(Byte* ptr, Int32 bufferSize)
   at Interop.Sys.GetCwd()
   at System.IO.Directory.GetCurrentDirectory()
   at Microsoft.DotNet.Cli.CommonOptions.DefaultToCurrentDirectory[T](Argument`1 arg)
   at Microsoft.DotNet.Cli.AddCommandParser..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.DotNet.Cli.Parser..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime, ITelemetry telemetryClient)
   at Microsoft.DotNet.Cli.Program.Main(String[] args)

内容的提问来源于stack exchange,提问作者BenevolentDeity

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 07:10:31