为有中文需求的客户提供多渠道中文技术支持.

Wed Jul 16, 2025 4:18 am

我的代码:
/// <summary>
/// 使用Spire.PDF将PDF文件转换为OFD格式
/// </summary>
/// <param name="inputPath">输入的PDF文件路径</param>
/// <param name="outputPath">输出的OFD文件路径</param>
public static void GetPdfToOfd(string inputPath, string outputPath)
{
// 验证输入参数有效性
if (string.IsNullOrWhiteSpace(inputPath))
throw new ArgumentNullException(nameof(inputPath), "输入PDF路径不能为空");
if (string.IsNullOrWhiteSpace(outputPath))
throw new ArgumentNullException(nameof(outputPath), "输出OFD路径不能为空");

// 检查输入文件是否存在
if (!File.Exists(inputPath))
throw new FileNotFoundException("指定的PDF文件不存在", inputPath);

// 创建PDF文档对象
Spire.Pdf.PdfDocument pdf = null;

try
{
// 实例化PDF文档对象
pdf = new Spire.Pdf.PdfDocument();

// 加载指定路径的PDF文件
pdf.LoadFromFile(inputPath);

// 检查文档是否加载成功
if (pdf.Pages.Count == 0)
{
Logger.Warning($"PDF文件不包含任何页面: {inputPath}");
}

// 将文档保存为OFD格式到指定路径
pdf.SaveToFile(outputPath, Spire.Pdf.FileFormat.OFD);

//// 记录转换成功日志
//Logger.Information($"PDF转换成功: {inputPath} -> {outputPath}");
}
catch (Exception ex)
{
// 记录详细错误信息到日志系统
Logger.Error(ex, $"PDF转OFD失败: {inputPath} -> {outputPath}");

// 包装原始异常并抛出,保留原始堆栈跟踪
throw new InvalidOperationException($"PDF转OFD失败: {ex.Message}", ex);
}
finally
{
// 确保PDF文档对象被正确释放
if (pdf != null)
{
pdf.Close(); // Spire.PDF推荐使用Close()释放资源
pdf.Dispose(); // 双重保障释放资源
}
}
}

报错信息:
"时间戳": "2025-07-16 11:34:29",
"日志级别": "Error",
"日志消息": "PDF转OFD失败: /data/khd/bc/PDF文件/jpg/002.pdf -> /data/khd/bc/OFD文件/jpg/002.ofd",
"异常信息": "System.TypeInitializationException: The type initializer for 'Windows.Win32.PInvoke' threw an exception.\n ---> System.TypeInitializationException: The type initializer for 'System.Drawing.Gdip' threw an exception.\n ---> System.TypeInitializationException: The type initializer for 'Windows.Win32.Graphics.GdiPlus.GdiPlusInitialization' threw an exception.\n ---> System.DllNotFoundException: Unable to load shared library 'gdiplus.dll' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: \n/data/khd/spire11/linux-x64/gdiplus.dll.so: cannot open shared object file: No such file or directory\n/data/khd/spire11/linux-x64/libgdiplus.dll.so: cannot open shared object file: No such file or directory\n/data/khd/spire11/linux-x64/gdiplus.dll: cannot open shared object file: No such file or directory\n/data/khd/spire11/linux-x64/libgdiplus.dll: cannot open shared object file: No such file or directory\n\n at Windows.Win32.PInvokeCore.GdiplusStartup(UIntPtr* token, GdiplusStartupInput* input, GdiplusStartupOutput* output)\n at Windows.Win32.Graphics.GdiPlus.GdiPlusInitialization.Init()\n at Windows.Win32.Graphics.GdiPlus.GdiPlusInitialization..cctor()\n --- End of inner exception stack trace ---\n at Windows.Win32.Graphics.GdiPlus.GdiPlusInitialization.EnsureInitialized()\n at System.Drawing.Gdip.Init()\n at System.Drawing.Gdip..cctor()\n --- End of inner exception stack trace ---\n at System.Drawing.Gdip.get_Initialized()\n at Windows.Win32.PInvoke..cctor()\n --- End of inner exception stack trace ---\n at Windows.Win32.PInvoke.GdipCreateMatrix(Matrix** matrix)\n

fhn123456
 
Posts: 4
Joined: Tue Sep 10, 2019 12:35 am

Wed Jul 16, 2025 6:34 am

您好,

感谢您的询问。

根据您的报错信息来看,问题原因在于您在Linux环境下错误地选用了Spire.PDF 版本。

在Linux下,请在您的项目中使用Spire.PDF for .NET Standard组件。Spire.PDF支持通过.NetCore和.NetStandard 两个版本进行跨平台部署。它们之间的区别在于前者使用System.Drawing.Common依赖项,而后者使用SkiaSharp来处理图像。但是从.Net 6开始,System.Drawing.Common不再支持非windows平台。在Linux等跨平台环境下,对于.Net 6或更高版本为目标的项目,需要使用Spire.PDFfor.NETStandard版本 。

您可以从以下链接下载最新版 Spire.PDF Pack(Hot Fix)版本: 11.7.10:
https://www.e-iceblue.cn/Downloads/Spire-PDF-NET.html
下载完成后,请解压压缩包,并将 netstandard2.0 文件夹中的 Spire.PDF.dll 文件导入您的项目。同时,需要通过 NuGet 安装以下依赖项:
Code: Select all
SkiaSharp >= 3.116.1
System.Buffers >= 4.5.1
System.Memory >= 4.5.5
Microsoft.Win32.Registry >= 4.5.0
System.Text.Encoding.CodePages >= 4.7.0
System.Security.Cryptography.Pkcs >= 4.7.0
System.Security.Cryptography.Xml >=4.7.1
System.Security.Permissions >= 4.7.0
HarfBuzzSharp >=8.3.0.1


您也可以在 NuGet 中直接搜索安装 "Spire.PDFfor.NETStandard ":
NuGet 安装地址:https://www.nuget.org/packages/Spire.PDFfor.NETStandard/11.7.10

如果您测试过程中遇到任何问题,请随时联系我们。
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 331
Joined: Mon Apr 14, 2025 3:33 am

Return to 中文技术支持

cron