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

C#虚拟打印机生成PostScript文件转文本的技术求助(Ghostscript/ps2txt)

解决PostScript转文本的C#实现方案

看起来你在借助Printer++开发虚拟打印机时,卡在了PostScript转文本这一步——之前试了Ghostscript和Verydoc没成功,大概率是参数配置或调用方式不对,我分享几个靠谱的方案和代码示例给你:

方法1:正确调用Ghostscript命令行(最稳定的开源方案)

很多人用Ghostscript失败是因为没选对输出设备,Ghostscript专门有个txtwrite设备用于提取PS文本,下面是C#里调用命令行的示例:

using System.Diagnostics;
using System.IO;

public class PsTextConverter
{
    public bool ConvertPsToTxt(string psTempPath, string outputTxtPath, string gsExePath)
    {
        // 先校验文件路径有效性
        if (!File.Exists(psTempPath) || !File.Exists(gsExePath))
        {
            Console.WriteLine("PS文件或Ghostscript路径无效,请检查");
            return false;
        }

        // Ghostscript核心参数:-sDEVICE=txtwrite是文本输出设备,-dNOPAUSE -dBATCH避免交互
        string arguments = $"-dNOPAUSE -dBATCH -sDEVICE=txtwrite -sOutputFile=\"{outputTxtPath}\" \"{psTempPath}\"";

        var processStartInfo = new ProcessStartInfo
        {
            FileName = gsExePath,
            Arguments = arguments,
            RedirectStandardError = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true // 隐藏命令行窗口
        };

        using (var process = Process.Start(processStartInfo))
        {
            process.WaitForExit();
            // 读取错误输出,方便排查问题
            string errorMsg = process.StandardError.ReadToEnd();
            if (!string.IsNullOrEmpty(errorMsg))
            {
                Console.WriteLine($"Ghostscript执行出错:{errorMsg}");
                return false;
            }
            return process.ExitCode == 0;
        }
    }
}

注意事项:

  • 确保你用的是命令行版本的Ghostscript:比如gswin64c.exe(64位系统)或gswin32c.exe(32位),别用GUI版本的gswin64.exe;
  • 建议下载最新版Ghostscript,旧版本可能不支持txtwrite设备;
  • 如果PS是多页的,这个命令会自动把所有页的文本合并到输出文件里。

方法2:用Ghostscript.NET封装库(更适合C#项目集成)

如果不想直接写命令行,可以用Ghostscript.NET这个NuGet包,它把Ghostscript的API封装成了.NET友好的接口:

  1. 先通过NuGet安装Ghostscript.NET;
  2. 然后用下面的代码提取文本:
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer;
using System.IO;

public class GhostscriptNetTextExtractor
{
    public void ExtractPsText(string psFilePath, string outputTxtPath)
    {
        // 根据你的系统选择Ghostscript的dll路径(64位用gsdll64.dll,32位用gsdll32.dll)
        string gsDllPath = @"C:\Program Files\gs\gs10.02.1\bin\gsdll64.dll";

        using (var rasterizer = new GhostscriptRasterizer())
        {
            rasterizer.Open(psFilePath, gsDllPath, true);

            // 逐页提取文本并写入文件
            using (var writer = new StreamWriter(outputTxtPath))
            {
                for (int page = 1; page <= rasterizer.PageCount; page++)
                {
                    string pageText = rasterizer.GetText(page);
                    writer.WriteLine($"--- 第{page}页 ---");
                    writer.WriteLine(pageText);
                }
            }

            rasterizer.Close();
        }
    }
}

常见问题排查

  • PS文件不完整:虚拟打印机生成的临时PS可能还没写完就被读取了,建议等待Printer++完全生成文件后再触发转换;
  • 权限问题:如果你的C#应用是服务或在受限环境运行,要确保有访问临时目录和Ghostscript路径的权限;
  • Verydoc失败原因:Verydoc很多功能需要付费授权,而且配置参数相对复杂,相比之下开源的Ghostscript更适合无授权场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:32:35