c# – Ghostscript转换PDF并输出文本文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Ghostscript转换PDF并输出文本文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2518字,纯文字阅读大概需要4分钟。
内容图文
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[]
{ "-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);
}
}
}
解决方法:
2个问题,2个答案:
>要获取输出到文件,请在命令行上使用-sOutputFile = / path / to / file,或添加行
"-sOutputFile=/where/it/should/go",
你的c#代码(可以是第一个参数,但应该在你的第一个“-c”之前.但首先要摆脱你已经在那里的其他-sOutputFile东西……
内容总结
以上是互联网集市为您收集整理的c# – Ghostscript转换PDF并输出文本文件全部内容,希望文章能够帮你解决c# – Ghostscript转换PDF并输出文本文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。