如何把pdf文件的每一頁都轉成一個圖片呢?首先說一下GhostScript這個軟件。
Ghostscript是一套建基於Adobe、PostScript及可移植文檔格式(PDF)的頁面描述語言等而編譯成的免費軟件。詳情請參考
百度百科:“http://baike.baidu.com/link?url=S1cISfrHO3F3K19xOKSmB10Vt3K4h02yS9fJH8zm27A2vIqEvtNVIlyi3AvoATv_”
Ghostscript官網:“http://www.ghostscript.com/”
參考資料:http://www.ghostscript.com/doc/current/Use.htm
使用方法:
Ghostscript使用方法很簡單。我用的是gs906w32這個版本。點擊安裝即可。
/// <summary> /// PDF轉成圖片 /// </summary> /// <param name="pdfFile">PDF文件路徑</param> /// <param name="imgPath">圖片輸出路徑</param> /// <returns>圖片數量</returns> public static int PDFToImg(string pdfFile, string imgPath) { PDFFile doc = PDFFile.Open(pdfFile); int pageCount = doc.PageCount; string pdfFileName = Path.GetFileName(pdfFile); string imgFile = Path.Combine(imgPath, "page");//轉換成的圖片文件 if (pageCount == 0) return 0; if (pageCount == 1) { imgFile += ".jpg"; if (File.Exists(imgFile)) { File.Delete(imgFile); } } else { //檢查是否存在相同名稱的圖片文件 for (int i = 0; i < pageCount; i++) { string _imgFile = imgFile + (i).ToString() + ".jpg"; if (File.Exists(_imgFile)) { File.Delete(_imgFile); } } imgFile += "%d.jpg"; } //啟動一個新的進程。傳入轉換參數 Process prc = new Process(); prc.StartInfo.UseShellExecute = false; prc.StartInfo.CreateNoWindow = true; prc.EnableRaisingEvents = true; prc.Exited += new EventHandler(prc_Exited); //-r150 圖片質量,數字越大越清晰,sDEVICE=png 轉換的圖片格式,imgFile 圖片輸出路徑(含文件名),pdfFile pdf文件地址 prc.StartInfo.Arguments = "-dSAFER -dBATCH -dNOPAUSE -r150 -sDEVICE=jpeg -dGraphicsAlphaBits=4" + @" -sOutputFile=" + imgFile + " " + pdfFile; //這里是gswin32.exe文件的路徑。請按照安裝路徑查找 prc.StartInfo.FileName = @"C:\Program Files\gs\gs9.06\bin\gswin32.exe"; prc.Start(); return pageCount; }
但是里有一個問題。那就是沒轉換玩一個都會提示你“轉換成功”。解決辦法,就是使用user32.dll。根據提示窗口名稱,抓取桌面的提示對話框。獲得該對話框后,使確定按鈕獲取焦點然后,根據sendKey 模擬敲擊回車。
如果誰有好的辦法,希望留言告知 謝謝 嘿嘿