使用Ghostscript API 及 iTextSharp 将PDF转JPEG
//已知PDF文件的路径 fileName
//截取所有的页面
//返回截取后的所有JPEG文件的路径
Convert PDF to Image Format(JPEG) using Ghostscript API
using iTextSharp.text.pdf;
public static string[] ConvertToImages(string fileName)
string name = Path.GetFileNameWithoutExtension(fileName);
string jpgFileFolder = Path.Combine(Path.GetDirectoryName(fileName), "Jpg");//切割后jpg的文件夹
if (!Directory.Exists(jpgFileFolder))
Directory.CreateDirectory(jpgFileFolder);
string outPutFilePath = String.Format("{0}\\{1}_{2}.jpg", jpgFileFolder, name, "%d");
PDFConvert _converter = new PDFConvert();
if (_converter.ConvertAllOfPdf(fileName, outPutFilePath, "jpeg"))
{
PdfReader _src = new PdfReader(fileName);
int totalPage = _src.NumberOfPages; //获取PDF的页数
string[] jpegPaths = new string[totalPage];
for (int i = 1; i <= totalPage; i++)
{
jpegPaths[i - 1] = String.Format("{0}\\{1}_{2}.jpg", jpgFileFolder, name, i.ToString());
}
return jpegPaths;
}
else
return null;
}