Csharp/C#教程:Ghostscript转换PDF并输出文本文件分享


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)。 这将是你自己的工作……

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 ,因此这里不需要它。 它的“合法”价值是:
  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上提供:

上述就是C#学习教程:Ghostscript转换PDF并输出文本文件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/992698.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐