///
/// <param name="pdfFile">PDF文档物理路径</param>
/// <param name="imgPath">转换成的图片文件的存放物理路径</param>
///
public static void PdfToImages(string pdfFile, string imgPath)
{
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;
if (pageCount == 1)
{
imgFile += ".png";
if (File.Exists(imgFile))
{
File.Delete(imgFile);
}
}
else
{
for (int i = 0; i < pageCount; i++)
{
string _imgFile = imgFile + (i + 1).ToString() + ".png";
if (File.Exists(_imgFile))
{
File.Delete(_imgFile);
}
}
imgFile += "%d.png";
}
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);
}