由於項目需求,需要將原來的PDF文檔轉換成圖片文件,在網上找了一些PDF轉圖片的方法;測試了幾個后,都有這樣或那樣的問題
1、PDFLibNet.dll,這個類型最初還是挺好用的,能轉圖片和HTML,但現在好象已經不再更新了;而且轉換時(部分文檔)會出現文字丟失的問題
2、Adobe Acrobat X Pro,這個太大,還需要注冊激活;且網上找到的代碼在WEB方式下不太好用,需要建個服務或命令行程序來作為中間件調用
3、Ghostscript,這個看網上也有比較多的演示代碼,不多,也比較適合我的要求,於是着重測試它了
但在網上的演示代碼,需要用到PDFBOX,IKVM等DLL文件(PDFBOX好象也很長時間沒更新了),但演示代碼中卻只需要用到一個得到PDF頁數的操作;測試下來,感覺有點多余
Ghostscript,可以直接輸出所有的PDF頁面;如果需要輸出指定頁時,才需要用到PDF頁數這個數據。
所以,只需要在服務器上安裝Ghostscript就可以了;不過,在這里需要提出來的是,測試下的9.00版以上的感覺不是很好用,8.71版不錯,挺好用的
9.00及以上版本,對於字體的處理好象還存在BUG或是我自己沒配置好(通過修改:lib/cidfmap)文檔
在測試中,字體不能正常加載處理;
Substituting font Times-Bold for TimesNewRomanPS-BoldMT.
Loading NimbusRomNo9L-Medi font from %rom%Resource/Font/NimbusRomNo9L-Medi... 2432860 1126845 2423648 1025245 3 done.
Loading a TT font from C:/WINDOWS/fonts/simsun.ttc to emulate a CID font SimSun ... Done.
Substituting font Times-Roman for TimesNewRomanPSMT.
Loading NimbusRomNo9L-Regu font from %rom%Resource/Font/NimbusRomNo9L-Regu... 3770200 2395423 2776248 1391721 3 done.
軒換文檔時依然出現文字丟失的情況
后來換成8.71版,再進行測試:
修改:lib/cidfmap:
在% Substitutions節下方追加(下面的內容只是針對我在測試中不能正常轉換的文檔設置,修改后,文檔轉換成功):
/MicrosoftYaHei << /FileType /TrueType /SubfontID 0 /CSI [(GB1) 2] /Path (C:/WINDOWS/fonts/msyh.ttf) >> ;
/LucidaSansUnicode << /FileType /TrueType /SubfontID 0 /CSI [(GB1) 2] /Path (C:/WINDOWS/fonts/msyh.ttf) >> ;
運行結果:
Loading a TT font from C:/WINDOWS/fonts/msyh.ttf to emulate a CID font MicrosoftYaHei ... Done.
**** Warning: Pattern stream has unbalanced q/Q operators (too many q's)
Loading a TT font from C:/WINDOWS/fonts/msyh.ttf to emulate a CID font LucidaSansUnicode ... Done.
生成的圖片文字正常,原來不能正常轉換的PDF文檔都能正確的轉換成圖片了,文字未出現丟失的情況
在實際的操作中,我去除了PDFBOX的DLL文檔引用,因為項目中已經引用了itextsharp.dll,它是可以得到分頁數的。
#region PdfToImages
/// <summary>
/// PDF 轉 PNG,需要事先安裝<c>GhostScript</c>
/// </summary>
/// <param name="pdfFile">PDF文檔路徑</param>
/// <param name="imgPath">圖片存儲文件夾路徑</param>
public static void PdfToImages(string pdfFile, string imgPath)
{
//如果文檔不存在
if (!FileHelper.FileExists(pdfFile))
{
return;
}
int pageCount = Utils.GetPdfPageCount(pdfFile);
//如果文檔為空
if (pageCount == 0)
{
return;
}
//轉換成的圖片文件
//生成文件名規則 %d 表示以數據進行標號
//%03d 表示001.png,002.png
string imgFile = string.Concat(imgPath, "%d.png");
string bin = System.Web.Configuration.WebConfigurationManager.AppSettings["GhostScriptBin"];
string exe = @"gswin32c.exe";
string arguments = System.Web.Configuration.WebConfigurationManager.AppSettings["GhostScriptArguments"];
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
try
{
proc.StartInfo.FileName = string.Concat(bin, exe);
proc.StartInfo.WorkingDirectory = bin;
proc.StartInfo.Arguments = string.Concat(arguments, " -sOutputFile=\"", imgFile, "\" \"", pdfFile, "\"");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.ErrorDataReceived += new DataReceivedEventHandler(PdfToImages_ErrorDataReceived);//重定向錯誤輸出
proc.OutputDataReceived += new DataReceivedEventHandler(PdfToImages_OutputDataReceived);//重定向輸出
proc.Start();
proc.BeginErrorReadLine();//此行不加的話,可能有些PDF文檔會轉換失敗
proc.BeginOutputReadLine();
//等待退出
proc.WaitForExit(int.MaxValue);
}
finally
{
proc.Close();
}
}
}
