Tho*_*urn 5 c# pdf ghostscript text-files
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"
};
}
Run Code Online (Sandbox Code Playgroud)
2.有一个unicode speficic .ps吗?
更新: 发布我的完整代码,可能错误在其他地方.
public static string[] GetArgs(string inputPath, string outputPath)
{
return new[]
{ "-o c:/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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
2个问题,2个答案:
要获得输出到文件,请-sOutputFile=/path/to/file在命令行上使用,或添加行
"-sOutputFile=/where/it/should/go",
Run Code Online (Sandbox Code Playgroud)
你的c#代码(可能是第一个参数,但应该在你的第一个参数之前"-c".但首先要摆脱-sOutputFile你已经在那里的其他东西...... :-)
不,PostScript不了解Unicode.
更新
(备注:可靠地从PDF中提取文本(出于各种技术原因)非常困难.它可能根本不起作用,无论你尝试哪种工具......)
在命令行上,以下两个应该适用于最近发布的Ghostscript(当前版本是v9.05).这将是你自己的工作......
c#代码.txtwrite设备:gswin32c.exe ^
-o c:/path/to/output.txt ^
-dTextFormat=3 ^
-sDEVICE=txtwrite ^
input.pdf
Run Code Online (Sandbox Code Playgroud)
笔记:
gswin64c.exe如果系统是64位,您可能希望在系统上使用(如果可用).-o输出的语法仅适用于最新版本的Ghostscript.-o语法不也隐含设置-dBATCH和-dNOPAUSE参数.-o速记不起作用,请将其替换为-dBATCH -dNOPAUSE -sOutputFile=....-dTextFormat默认情况下设置为3,无论如何,所以它在这里不需要.'合法'的价值观是:
0:这将输出XML转义的Unicode以及与文本格式相关的信息(位置,字体名称,磅值等).仅供开发人员使用.1:相同0,但会输出文本块.2:这将输出带有BMO(字节顺序标记)的Unicode(UCS2)文本; 尝试近似原始文档中的文本布局.3:( 默认)相同2,但文本以UTF-8编码.txtwrite具有此-dTextFormat修饰符的设备是Ghostscript的一项新资产,因此如果找到错误,请报告错误.ps2ascii.psgswin32c.exe ^
-sstdout=c:/path/to/output.txt ^
-dSIMPLE ^
-sFONTPATH=c:/windows/fonts ^
-dNODISPLAY
-dDELAYBIND ^
-dWRITESYSTEMDICT ^
-f /path/to/ps2ascii.ps ^
input.pdf
Run Code Online (Sandbox Code Playgroud)
笔记:
txtwrite设备完全不同的方法,不能与它混合使用!ps2ascii.ps是一个文件,Ghostscript调用以提取文本的PostScript程序.它通常位于Ghostscript installdir的/lib子目录中.去看看它是否真的存在.-dSIMPLE可以替换dCOMPLEX为打印出额外的信息行(当前颜色,图像的存在,矩形填充).-sstdout=...是必需的,因为ps2ascii.psPostScript程序只打印到stdout,不能告诉写入文件.因此-sstdout=...告诉Ghostscript将其stdout重定向到一个文件.不要忽略可能更容易使用的其他非Ghostscript方法.以下所有内容都是跨平台的,也应该在Windows上可用:
mudraw -tpdftotextpdftotext).podofotxtextract