我正在使用 Ghostscript 通过 GhostScript.NET 包装库将 PDF 文件转换为文本。在将文本写入文件(“-sOutputFile=”+ outputPipeHandle)时,我正在对其进行转换,但我宁愿将其写入流(“-o”+ outputPipeHandle)。这是我在 C# 中的尝试:
try
{
Debug.WriteLine("Installed " + _lastInstalledVersion.LicenseType.ToString() + " Ghostscript " + _lastInstalledVersion.Version.ToString());
GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();
// pipe handle format: %handle%hexvalue
string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");
using (GhostscriptProcessor processor = new GhostscriptProcessor(_lastInstalledVersion, true))
{
processor.Processing += new GhostscriptProcessorProcessingEventHandler(ghostscript_Processing);
List<string> switches = new List<string>();
switches.Add("-empty");
switches.Add("-dQUIET");
switches.Add("-dSAFER");
switches.Add("-dBATCH");
switches.Add("-dNOPAUSE");
switches.Add("-dNOPROMPT");
if (this.Context.IsPreview)
{
switches.Add("-dLastPage=5");
}
switches.Add("-dTextFormat=3");
switches.Add("-sDEVICE=txtwrite");
switches.Add("-o" + outputPipeHandle);
switches.Add("-q");
switches.Add("-f");
switches.Add(Context.Path);
//"-sOutputFile=" + outputPipeHandle
try
{
//processor.Process(switches.ToArray());
// THROWS AN ERROR HERE
processor.StartProcessing(switches.ToArray(), new ConsoleStdIO(true, true, true));
// HERE IS WHERE I HOPE TO GRAB THE TEXT FROM THE PIPED OUTPUT
byte[] rawDocumentData = gsPipedOutput.Data;
returnVal = System.Text.Encoding.Default.GetString(rawDocumentData);
Debug.WriteLine(returnVal);
}
catch (Exception ex)
{
returnVal = ex.Message;
}
finally
{
gsPipedOutput.Dispose();
gsPipedOutput = null;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
returnVal = ex.Message;
}
return returnVal;
}
}
我的错误如下 - 我不太关心警告,因为我是关于“无法打开文件 958”的致命错误 - 因为我认为它会在内存中。
**** Warning: can't process font stream, loading font by the name.
**** Error reading a content stream. The page may be incomplete.
**** File did not complete the page properly and may be damaged.
**** Warning: File has unbalanced q/Q operators (too many q's)
The thread 0x59c0 has exited with code 0 (0x0).
**** Warning: can't process font stream, loading font by the name.
**** Error reading a content stream. The page may be incomplete.
**** File did not complete the page properly and may be damaged.
GPL Ghostscript 9.06: **** Could not open the file 958 .
Error: /ioerror in --showpage--
Operand stack:
1 true
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1926 1 3 %oparray_pop 1925 1 3 %oparray_pop 1909 1 3 %oparray_pop --nostringval-- --nostringval-- 5 1 5 --nostringval-- %for_pos_int_continue --nostringval-- --nostringval-- 1809 0 10 %oparray_pop --nostringval-- --nostringval--
Dictionary stack:
--dict:1173/1684(ro)(G)-- --dict:1/20(G)-- --dict:82/200(L)-- --dict:82/200(L)-- --dict:109/127(ro)(G)-- --dict:293/300(ro)(G)-- --dict:25/31(L)-- --dict:6/8(L)-- --dict:21/40(L)-- --dict:7/15(L)--
Current allocation mode is local
Last OS error: Bad file descriptor
GPL Ghostscript 9.06: Unrecoverable error, exit code 1
Exception thrown: 'Ghostscript.NET.GhostscriptAPICallException' in Ghostscript.NET.dll
更新
作为其他参考,我基本上使用了他们网站上 GhostScript.net 示例中的这段代码: PipedOutputSample.cs
我能看到的唯一区别是我将设备更改为“txtwrite”而不是“pdfwrite”。
更新 2
仔细阅读文档后
请注意,在 MS Windows 系统上,% 字符对于命令处理器 (shell) 也有特殊含义,因此您必须将其加倍。
当我碰巧在 Windows 上时,我将我的 outputPipeHandle 命名为:
string outputPipeHandle = "%%handle%%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");
所以我不再收到上述关于无法访问文件的错误。相反,我在这一行上的断点已到达
byte[] rawDocumentData = gsPipedOutput.Data;
但是在尝试将该数据字段的值复制到我的变量时,它完全脱离了我的线程......跳过了我的捕获,甚至我的 finally 块。我没有看到任何关于它为什么在输出控制台中爆发的消息。某种 GhostScript 指针错误???
我仍然不知所措。我什至不知道 %handle% 应该表示什么或为什么需要使用它。没有这些知识,我就瞎了眼,只是扔东西希望它能起作用。
谢谢你。