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

含长亚洲语系文件名的PDF压缩时gsapi_init_with_args返回-100错误求助

问题分析与解决方案

问题根源

  1. Ghostscript 9.55.0 32位版本对**Unicode长文件名(含多字节亚洲语言)**的支持存在缺陷,当文件名长度超过阈值时,会导致参数解析错误,触发gsapi_init_with_args返回-100的初始化失败错误,同时造成文件名截断、输出文件损坏。
  2. 代码中存在参数格式错误:-dPDFSETTINGS =/default的空格会导致Ghostscript无法识别该参数,需改为-dPDFSETTINGS=/default。

解决方案

方案1:改用64位Ghostscript并启用Unicode支持

  • 卸载32位Ghostscript,安装64位最新版Ghostscript(10.x及以上),新版本对Unicode和长路径的支持大幅提升。
  • 修改代码,使用带版本信息的GhostscriptProcessor构造函数并启用Unicode兼容:
try
{
    string strFilePath = PDFFile.FullName;
    string strFileNameOnly = Path.GetFileNameWithoutExtension(PDFFile.Name);               
    string strFullnameOutput = Path.Combine(strOutput, strFileNameOnly) + ".pdf";

    // 指定64位Ghostscript的安装路径(需根据实际安装位置调整)
    GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(@"C:\Program Files\gs\gs10.02.0\bin\gswin64c.exe");
    
    var switches = new List<string>
    {
        "-dQUIET",
        "-dSAFER",
        "-dBATCH",
        "-dNOPAUSE",
        "-dNOPROMPT",
        "-sDEVICE=pdfwrite",
        "-dCompatibilityLevel=2.0",
        "-dPDFSETTINGS=/default", // 修正参数空格问题
        $"-sOutputFile={strFullnameOutput}",
        "-c",
        "-f",
        strFilePath
    };

    // 启用Unicode支持的构造函数
    using (var gsProc = new GhostscriptProcessor(gvi, true))
    {
        try
        {
            gsProc.StartProcessing(switches.ToArray(), null);
            isDone = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            isDone = false;
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
    isDone = false;
}

方案2:临时文件名中转(兼容旧版本Ghostscript)

如果无法升级Ghostscript,可通过短ASCII临时文件名中转处理,绕过Unicode长文件名的限制:

try
{
    string strFilePath = PDFFile.FullName;
    string strFileNameOnly = Path.GetFileNameWithoutExtension(PDFFile.Name);               
    string strFullnameOutput = Path.Combine(strOutput, strFileNameOnly) + ".pdf";

    // 创建短名称临时文件
    string tempInput = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");
    string tempOutput = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");
    File.Copy(strFilePath, tempInput, true);

    GhostscriptVersionInfo gvi = GhostscriptVersionInfo.GetLastInstalledVersion();
    var switches = new List<string>
    {
        "-dQUIET",
        "-dSAFER",
        "-dBATCH",
        "-dNOPAUSE",
        "-dNOPROMPT",
        "-sDEVICE=pdfwrite",
        "-dCompatibilityLevel=2.0",
        "-dPDFSETTINGS=/default", // 修正参数空格问题
        $"-sOutputFile={tempOutput}",
        "-c",
        "-f",
        tempInput
    };

    using (var gsProc = new GhostscriptProcessor(gvi))
    {
        try
        {
            gsProc.StartProcessing(switches.ToArray(), null);
            // 将处理后的临时文件重命名为原文件名
            if (File.Exists(strFullnameOutput)) File.Delete(strFullnameOutput);
            File.Move(tempOutput, strFullnameOutput);
            isDone = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            isDone = false;
        }
        finally
        {
            // 清理临时文件
            if (File.Exists(tempInput)) File.Delete(tempInput);
            if (File.Exists(tempOutput)) File.Delete(tempOutput);
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
    isDone = false;
}

方案3:升级Ghostscript.NET到最新版本

部分新版本Ghostscript.NET优化了Unicode参数传递逻辑,可尝试更新到最新正式版或预览版。

关键注意事项

  • 确保输出目录具备读写权限。
  • 保持系统区域设置与文件名语言匹配(当前已设置为日语,无需调整)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 22:20:26