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 特定的 .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);
}
}
}