Ghostscript转换PDF并输出文本文件

1.我需要将PDF文件转换为txt.file。 我的命令似乎工作,因为我在屏幕上获得转换后的文本,但不知何故,我无法将输出定向到文本文件。

public static string[] GetArgs(string inputPath, string outputPath) { return new[] { "-q", "-dNODISPLAY", "-dSAFER", "-dDELAYBIND", "-dWRITESYSTEMDICT", "-dSIMPLE", "-c", "save", "-f", "ps2ascii.ps", inputPath, "-sDEVICE=txtwrite", String.Format("-sOutputFile={0}", outputPath), "-c", "quit" }; } 

2.有一个unicode speficic .ps吗?

更新:发布我的完整代码,可能错误是在其他地方。

 public static string[] GetArgs(string inputPath, string outputPath) { return new[] { "-oc:/test.txt", "-dSIMPLE", "-sFONTPATH=c:/windows/fonts", "-dNODISPLAY", "-dDELAYBIND", "-dWRITESYSTEMDICT", "-f", "C:/Program Files/gs/gs9.05/lib/ps2ascii.ps", inputPath, }; } [DllImport("gsdll64.dll", EntryPoint = "gsapi_new_instance")] private static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle); [DllImport("gsdll64.dll", EntryPoint = "gsapi_init_with_args")] private static extern int InitAPI(IntPtr instance, int argc, string[] argv); [DllImport("gsdll64.dll", EntryPoint = "gsapi_exit")] private static extern int ExitAPI(IntPtr instance); [DllImport("gsdll64.dll", EntryPoint = "gsapi_delete_instance")] private static extern void DeleteAPIInstance(IntPtr instance);` private static object resourceLock = new object(); private static void Cleanup(IntPtr gsInstancePtr) { ExitAPI(gsInstancePtr); DeleteAPIInstance(gsInstancePtr); } private static object resourceLock = new object(); public static void ConvertPdfToText(string inputPath, string outputPath) { CallAPI(GetArgs(inputPath, outputPath)); } public static void ConvertPdfToText(string inputPath, string outputPath) { CallAPI(GetArgs(inputPath, outputPath)); } private static void CallAPI(string[] args) { // Get a pointer to an instance of the Ghostscript API and run the API with the current arguments IntPtr gsInstancePtr; lock (resourceLock) { CreateAPIInstance(out gsInstancePtr, IntPtr.Zero); try { int result = InitAPI(gsInstancePtr, args.Length, args); if (result < 0) { throw new ExternalException("Ghostscript conversion error", result); } } finally { Cleanup(gsInstancePtr); } } } 

2个问题,2个答案:

  1. 要获取输出到文件,请在命令行上使用-sOutputFile=/path/to/file ,或添加行

     "-sOutputFile=/where/it/should/go", 

    你的c#代码(可以是第一个参数,但应该在你的第一个"-c" 。但首先要摆脱你已经在那里的其他-sOutputFile东西…… 🙂

  2. 不,PostScript不了解Unicode。


更新

(备注:可靠地从PDF中提取文本(出于各种技术原因)非常困难。并且它可能根本不起作用,无论您尝试哪种工具……)

在命令行上,以下两个应该适用于最近发布的Ghostscript(当前版本是v9.05)。 这将是你自己的工作……

  • …测试哪个命令更适合您的用例,以及
  • …将这些转换为c#代码。

1. txtwrite设备:

 gswin32c.exe ^ -oc:/path/to/output.txt ^ -dTextFormat=3 ^ -sDEVICE=txtwrite ^ input.pdf 

笔记:

  1. 如果是64位,您可能希望在系统上使用gswin64c.exe (如果可用)。
  2. 输出的-o语法仅适用于最新版本的Ghostscript。
  3. -o语法也隐含地设置-dBATCH-dNOPAUSE参数。
  4. 如果您的Ghostscript太旧且-o速记不起作用,请将其替换为-dBATCH -dNOPAUSE -sOutputFile=...
  5. 即使在Windows上,Ghostscript也可以在路径参数内处理正斜杠。
  6. -dTextFormat默认设置为3 ,因此这里不需要它。 它的“合法”价值是:
    • 0 :这将输出XML转义的Unicode以及与文本格式相关的信息(位置,字体名称,磅值等)。 仅供开发人员使用。
    • 1 :与0相同,但会输出文本块。
    • 2 :这输出带有BMO(字节顺序标记)的Unicode(UCS2)文本; 尝试近似原始文档中的文本布局。
    • 3 🙁 默认)2相同,但文本以UTF-8编码。
  7. 带有这个-dTextFormat修饰符的txtwrite设备是Ghostscript的一个相当新的资产,所以如果你找到了bug ,请报告bug 。

2.使用ps2ascii.ps

 gswin32c.exe ^ -sstdout=c:/path/to/output.txt ^ -dSIMPLE ^ -sFONTPATH=c:/windows/fonts ^ -dNODISPLAY -dDELAYBIND ^ -dWRITESYSTEMDICT ^ -f /path/to/ps2ascii.ps ^ input.pdf 

笔记:

  1. 这是一种与txtwrite设备完全不同的方法,不能与它混合使用!
  2. ps2ascii.ps是一个文件 ,是Ghostscript调用以提取文本的PostScript程序。 它通常位于Ghostscript installdir的/lib子目录中。 去看看它是否真的存在。
  3. -dSIMPLE可以由dCOMPLEX替换,以打印出额外的信息行(当前颜色,图像的存在,矩形填充)。
  4. -sstdout=...是必需的,因为ps2ascii.ps PostScript程序只打印到stdout,不能告诉写入文件。 所以-sstdout=...告诉Ghostscript将其stdout重定向到一个文件。

3.非Ghostscript方法

不要忽略可能更容易使用的其他非Ghostscript方法。 以下所有内容都是跨平台的,也应该在Windows上提供:

  • mudraw -t
    GPL许可(或商业,如果您需要)。 MuPDF的命令行实用程序从PDF中提取文本(由开发Ghostscript的同一组开发人员开发)。
  • pdftotext
    GPL许可。 Poppler的命令行实用程序(它是XPDF的一个分支,也提供了pdftotext )。
  • podofotxtextract
    GPL许可。 Commandline实用程序基于PoDoFo PDF处理库。
  • TET
    来自PDFlib.com的文本提取工具包 (商业,但可能是免费供个人使用 – 我没有查看最近的新闻)。 可能是他们最强大的文本提取工具……