[英]Ghostscript convert a PDF and output in a textfile
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
東西...... :-)
不,PostScript不了解Unicode。
更新
(備注:可靠地從PDF中提取文本(出於各種技術原因)非常困難。它可能根本不起作用,無論你嘗試哪種工具......)
在命令行上,以下兩個應該適用於最近發布的Ghostscript(當前版本是v9.05)。 這將是你自己的工作......
c#
代碼。 txtwrite
設備: gswin32c.exe ^
-o c:/path/to/output.txt ^
-dTextFormat=3 ^
-sDEVICE=txtwrite ^
input.pdf
筆記:
gswin64c.exe
(如果可用)。 -o
語法僅適用於最新版本的Ghostscript。 -o
語法也隱含地設置了-dBATCH
和-dNOPAUSE
參數。 -o
速記不起作用,請將其替換為-dBATCH -dNOPAUSE -sOutputFile=...
-dTextFormat
默認設置為3
,因此這里不需要它。 '合法'的價值觀是:
0
:這將輸出XML轉義的Unicode以及與文本格式相關的信息(位置,字體名稱,磅值等)。 僅供開發人員使用。 1
:與0
相同,但會輸出文本塊。 2
:輸出帶有BMO(字節順序標記)的Unicode(UCS2)文本; 嘗試近似原始文檔中的文本布局。 3
:( 默認)與2
相同,但文本以UTF-8編碼。 -dTextFormat
修飾符的txtwrite
設備是Ghostscript的一個相當新的資產,所以如果找到bug ,請報告錯誤 。 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
筆記:
txtwrite
設備完全不同的方法,不能與它混合使用! ps2ascii.ps
是一個文件 ,是Ghostscript調用以提取文本的PostScript程序。 它通常位於Ghostscript installdir的/lib
子目錄中。 去看看它是否真的存在。 -dSIMPLE
可以由dCOMPLEX
替換,以打印出額外的信息行(當前顏色,圖像的存在,矩形填充)。 -sstdout=...
是必需的,因為ps2ascii.ps
PostScript程序僅打印到stdout,不能告訴寫入文件。 所以-sstdout=...
告訴Ghostscript將其stdout重定向到一個文件。 不要忽略可能更容易使用的其他非Ghostscript方法。 以下所有內容都是跨平台的,也應該在Windows上可用:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.