在網(wǎng)上找遍了有關(guān) C# 如何將PDF文檔轉(zhuǎn)換成圖片的資料,最終在CSDN上面得到了一位高手的指點(diǎn),根據(jù)他的指點(diǎn),我做了這個(gè)項(xiàng)目,希望這個(gè)項(xiàng)目能對(duì)和我之前有同樣需求的人有所幫助!
1、項(xiàng)目中用到了GhostScript,有關(guān)GhostScript的詳細(xì)說(shuō)明,請(qǐng)參考:http://downloads.sourceforge.net/ghostscript/gs861w32.exe?modtime=1196280996&big_mirror=1
2、在運(yùn)行本項(xiàng)目前,你必須確保在本地計(jì)算機(jī)上已安裝GhostScript,至于GhostScript的安裝目錄你可隨意選擇,但在項(xiàng)目中的配置文件中的<add key="GhostScriptView" value="C:/Program Files/gs/gs8.71/bin"/>也要和你的安裝目錄同步。
3、根目錄中的 gs871w32.exe 為 GhostScript 安裝文件,可直點(diǎn)擊安裝到本地計(jì)算機(jī)。
4、根目錄中還有 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll、IKVM.Runtime.dll、FontBox-0.1.0-dev.dll 這四個(gè)程序集文件,它們是PDFBox(有關(guān)PDFBox的詳細(xì)說(shuō)明請(qǐng)參考:http://sourceforge.net/projects/pdfbox/)中的程序集文件,主要用于輔助操作pdf文檔,因?yàn)槭褂肞DFBox將PDF文檔轉(zhuǎn)為圖片的.net版本目前還有些BUG,所以在項(xiàng)目中用了GhostScript來(lái)實(shí)現(xiàn)轉(zhuǎn)換。請(qǐng)將 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll引入項(xiàng)目中,再把 IKVM.Runtime.dll、FontBox-0.1.0-dev.dll置于運(yùn)行目錄下,具體請(qǐng)看項(xiàng)目及其代碼。
項(xiàng)目很簡(jiǎn)單,下面貼出了主要的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Diagnostics;
using org.pdfbox.util;
using org.pdfbox.pdmodel;
#region 轉(zhuǎn)換PDF文檔
/// <summary>
/// 將PDF文檔轉(zhuǎn)換成文本
/// </summary>
/// <param name="pdfFile">PDF文檔物理路徑</param>
/// <returns>返回從PDF文檔剝離的文本</returns>
public string PdfToText(string pdfFile)
{
PDDocument doc = PDDocument.load(pdfFile);
PDFTextStripper stripper = new PDFTextStripper();
return stripper.getText(doc);
}
/// <summary>
/// 將PDF文檔轉(zhuǎn)換成圖片
/// </summary>
/// <param name="pdfFile">PDF文檔物理路徑</param>
/// <param name="imgPath">轉(zhuǎn)換成的圖片文件的存放物理路徑</param>
/// <param name="isDeletePDF">轉(zhuǎn)換成圖片文件以后是否刪除原PDF文檔</param>
/// <returns>返回轉(zhuǎn)換成的圖片文件物理路徑的集合</returns>
public IList<string> PdfToImages(string pdfFile, string imgPath, bool isDeletePDF)
{
IList<string> imgList = new List<string>();
PDDocument doc = PDDocument.load(pdfFile);
int pageCount = doc.getDocumentCatalog().getAllPages().size();//計(jì)算pdf文檔的總頁(yè)數(shù)
string pdfFileName = Path.GetFileName(pdfFile);
int index = pdfFileName.LastIndexOf('.');
if (index != -1)
pdfFileName = pdfFileName.Substring(0, index);
string imgFile = Path.Combine(imgPath, pdfFileName);//轉(zhuǎn)換成的圖片文件
if (pageCount == 0) return null;
if (pageCount == 1)
{
imgFile += ".jpg";
imgList.Add(imgFile);
if (File.Exists(imgFile)) File.Delete(imgFile);
}
else
{
for (int i = 0; i < pageCount; i++)
{
string _imgFile = imgFile + (i + 1).ToString() + ".jpg";
imgList.Add(_imgFile);
if (File.Exists(_imgFile)) File.Delete(_imgFile);
}
imgFile += "%d.jpg";
}
ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"];
info.Arguments = System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + " " + pdfFile;
info.FileName = @"gswin32c.exe";
Process subProcess = new Process();
subProcess.StartInfo = info;
subProcess.Start();
subProcess.WaitForExit(int.MaxValue);
if (isDeletePDF)
{
File.Delete(pdfFile);
}
return imgList;
}
#endregion
代碼中會(huì)用到的配置信息:
<appSettings>
<add key="GhostScriptView" value="C:/Program Files/gs/gs8.71/bin"/>
<add key="GhostScriptArguments" value="-dSAFER -dBATCH -dNOPAUSE -r150 -sDEVICE=jpeg -dGraphicsAlphaBits=4"/>
</appSettings>
簡(jiǎn)單吧,下面請(qǐng)看看本項(xiàng)目運(yùn)行的效果圖:
注:完整項(xiàng)目解決方案我已上傳為資源,歡迎大家前去下載使用!
聯(lián)系客服