1 winform将pdf转图片,有案例,连接为:https://download.csdn.net/download/mingjing941018/20216747
2 Asp.net MVC将pdf转图片
使用Nuget安装包安装Freespire.pdf,控制器中相关代码:
/// <summary>
/// 将本地的pdf文件保存为图片
/// </summary>
/// <param name="pdfName"></param>
/// <returns></returns>
[HttpPost]
public ActionResult GetPdtPic(string folder, string pdfName)
{
try
{
string bPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Content\\tjbg";
string bFileName = pdfName + ".pdf";
string bPdfPath = $"{bPath}\\{folder}\\{bFileName}";
//var pdf = PdfDocument.Load(bPdfPath);
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(bPdfPath);
var pdfpage = pdf.Pages.Count;
//var pagesizes = pdf.PageSizes;
for (int i = 1; i <= pdfpage; i++)
{
//Size size = new Size();
//size.Height = (int)pagesizes[(i - 1)].Height;
//size.Width = (int)pagesizes[(i - 1)].Width;
可以把".jpg"写成其他形式
//RenderPage(bPdfPath, i, size, bPath + "\\" + i.ToString() + @".jpg", "");
Image bImg = pdf.SaveAsImage(i);
MemoryStream imgstream = new MemoryStream();
bImg.Save(imgstream, ImageFormat.Jpeg);
byte[] byData = new Byte[imgstream.Length];
imgstream.Position = 0;
imgstream.Read(byData, 0, byData.Length);
imgstream.Close();
//base64 = Convert.ToBase64String(byData);
System.Drawing.Image bmp = System.Drawing.Image.FromStream(imgstream);
string fileName = string.Format(i.ToString() + @".jpg");
bmp.Save(bPath + "\\"+folder+"\\" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
pdf.Dispose();
return Json(new { Code = 0,Msg = "",Count = pdfpage });
}
catch (Exception e) {
return Json(new { Code = -1,Msg = e.Message,Count = 0 });
}
}
执行后提示:Spire.PDF Free version is limited to 10 pages of PDF. This limitation is enforced during loading and creating files.When converting PDF to Image, the first 3 pages of PDF files will be converted to Image format successfully。
3 使用magick.net-Q16-AnyCPU进行转换,速度太慢,绝对不能用,网上有资料说这个只能转gif图片,未进行验证
相关代码:
try
{
string bPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Content\\tjbg";
string bFileName = pdfName + ".pdf";
string bPdfPath = $"{bPath}\\{folder}\\{bFileName}";
int bCount = 0;
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new Density(900, 900); //设置质量
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(bPdfPath, settings);
bCount = images.Count;
for (int i = 0; i < bCount; i++)
{
MagickImage image = (MagickImage)images[i];
image.Format = MagickFormat.Jpeg;
string fileName = string.Format(i.ToString() + @".jpg");
image.Write(bPath + "\\" + folder + "\\" + fileName);
}
}
return Json(new { Code = 0, Msg = "", Count = bCount });
}
catch (Exception ex)
{
return Json(new { Code = -1, Msg = ex.Message, Count = 0 });
}
4 使用Aspose.PDF.dll进行转换
相关代码:
try
{
string bPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Content\\tjbg";
string bFileName = pdfName + ".pdf";
string bPdfPath = $"{bPath}\\{folder}\\{bFileName}";
Document pdfDocument = new Document(bPdfPath);
int bCount = pdfDocument.Pages.Count;
for (int pageCount = 1; pageCount <= bCount; pageCount++)
{
string fileName = string.Format(pageCount.ToString() + @".jpg");
using (FileStream imageStream = new FileStream(bPath + "\\" + folder + "\\" + fileName, FileMode.Create))
{
// Create PNG device with specified attributes
// Width, Height, Resolution, Quality
// Quality [0-100], 100 is Maximum
// Create Resolution object
Resolution resolution = new Resolution(300);
PngDevice pngDevice = new PngDevice(resolution);
// Convert a particular page and save the image to stream
pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);
// Close stream
imageStream.Close();
}
}
return Json(new { Code = 0, Msg = "", Count = bCount });
}
catch (Exception ex)
{
return Json(new { Code = -1, Msg = ex.Message, Count = 0 });
}
执行后提示:At most 4 elements (for any collection) can be viewed in evaluation mode