在网上找遍了有关 C# 如何将PDF文档转换成图片的资料,最终在CSDN上面得到了一位高手的指点,根据他的指点,我做了这个项目,希望这个项目能对和我之前有同样需求的人有所帮助!
1、项目中用到了GhostScript,有关GhostScript的详细说明,请参考:http://downloads.sourceforge.net/ghostscript/gs861w32.exe?modtime=1196280996&big_mirror=1
2、在运行本项目前,你必须确保在本地计算机上已安装GhostScript,至于GhostScript的安装目录你可随意选择,但在项目中的配置文件中的<add key="GhostScriptView" value="C:/Program Files/gs/gs8.71/bin"/>也要和你的安装目录同步。
3、根目录中的 gs871w32.exe 为 GhostScript 安装文件,可直点击安装到本地计算机。
4、根目录中还有 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll、IKVM.Runtime.dll、FontBox-0.1.0-dev.dll 这四个程序集文件,它们是PDFBox(有关PDFBox的详细说明请参考:http://sourceforge.net/projects/pdfbox/)中的程序集文件,主要用于辅助操作pdf文档,因为使用PDFBox将PDF文档转为图片的.net版本目前还有些BUG,所以在项目中用了GhostScript来实现转换。请将 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll引入项目中,再把 IKVM.Runtime.dll、FontBox-0.1.0-dev.dll置于运行目录下,具体请看项目及其代码。
项目很简单,下面贴出了主要的代码:
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 转换PDF文档
/// <summary>
/// 将PDF文档转换成文本
/// </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文档转换成图片
/// </summary>
/// <param name="pdfFile">PDF文档物理路径</param>
/// <param name="imgPath">转换成的图片文件的存放物理路径</param>
/// <param name="isDeletePDF">转换成图片文件以后是否删除原PDF文档</param>
/// <returns>返回转换成的图片文件物理路径的集合</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();//计算pdf文档的总页数
string pdfFileName = Path.GetFileName(pdfFile);
int index = pdfFileName.LastIndexOf('.');
if (index != -1)
pdfFileName = pdfFileName.Substring(0, index);
string imgFile = Path.Combine(imgPath, pdfFileName);//转换成的图片文件
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
代码中会用到的配置信息:
<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>
简单吧,下面请看看本项目运行的效果图:
注:完整项目解决方案我已上传为资源,欢迎大家前去下载使用!