我的代码:
/// <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