using Ghostscript.NET.Rasterizer;
using Ghostscript.NET;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System;
using System.Runtime.Remoting.Channels;
using System.Web;
namespace CommonHelper
{
public static class PdfHelper
{
///
/// pdf转图片
///
/// pdf路径,尽量避免中文
///
public static IEnumerable Pdf2Imgs(string inputPdfPath)
{
int desired_dpi = 96;
var pdfName = Path.GetFileNameWithoutExtension(inputPdfPath);
string outputPath = HttpContext.Current.Server.MapPath("~/CachePdfImages");
Directory.CreateDirectory(outputPath);
var dllPath = HttpContext.Current.Server.MapPath("~/Depends/gsdll64.dll");
#if DEBUG
dllPath = HttpContext.Current.Server.MapPath("~/bin/Depends/gsdll64.dll");
#endif
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(dllPath);
using (var rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(inputPdfPath, gvi, false);
for (var pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
var pageFilePath = Path.Combine(outputPath, string.Format("p{0}_{1}_{2}.png", pageNumber,pdfName,DateTime.Now.ToString("HHmmssfff")));
var img = rasterizer.GetPage(desired_dpi, pageNumber);
img.Save(pageFilePath, ImageFormat.Png);
yield return pageFilePath;
}
}
}
}
}