C# pdf转图片
方法一:
引用:O2S.Components.PDFRender4NET.dll
/// <summary> /// PDF文档所有页全部转换为图片 /// </summary> /// <param name="pdfInputPath">PDF文件流</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static void ConvertAllPDF2Images(string pdfStream, string imageOutputPath, string imageName, ImageFormat imageFormat, Definition definition) { PDFFile pdfFile = PDFFile.Open(pdfStream); int startPageNum = 1; int endPageNum = pdfFile.PageCount; // var bitMap = new Bitmap[endPageNum]; for (int i = startPageNum; i <= endPageNum; i++) { try { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); int canKao = pageImage.Width > pageImage.Height ? pageImage.Height : pageImage.Width; int newHeight = canKao > 1080 ? pageImage.Height / 2 : pageImage.Height; int newWidth = canKao > 1080 ? pageImage.Width / 2 : pageImage.Width; Bitmap newPageImage = new Bitmap(newWidth, newHeight); Graphics g = Graphics.FromImage(newPageImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(pageImage, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, pageImage.Width, pageImage.Height), GraphicsUnit.Pixel); newPageImage.Save(imageOutputPath + imageName + "." + imageFormat);//+ i.ToString() imageFormat g.Dispose(); newPageImage.Dispose(); pageImage.Dispose(); } catch (Exception ex) { string ss = ex.ToString(); } } //合并图片 //var mergerImg = MergerImg(bitMap); //保存图片 // mergerImg.Save(imageOutputPath, imageFormat); pdfFile.Dispose(); }
/// <summary> /// 转换的图片清晰度,1最不清醒,10最清晰 /// </summary> public enum Definition { One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10 }
方法二:
引用:1.PdfiumViewer.dll
2.pdfium.dll X86和pdfium.dll X64两个补丁
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using PdfiumViewer; public class PDFChangeImage { #region 获取PDF页数 /// <summary> /// 获取PDF页数 /// </summary> /// <param name="pdfpath">PDF路径</param> /// <returns></returns> public int PDFpage(string pdfpath) { var pdf = PdfDocument.Load(pdfpath); //pdf页数 int pdfpage = pdf.PageCount; return pdfpage; } #endregion #region PDF转图片 /// <summary> /// PDF转图片 /// </summary> /// <param name="pdfpath">PDF路径</param> /// <param name="imagepath">图片保存路径</param> /// <param name="imageFormat">图片格式</param> /// <param name="imageName">图片名称</param> /// <param name="page">PDF当前页</param> /// <returns></returns> public void PDFChangeToImage(string pdfpath, string imagepath, ImageFormat imageFormat, string imageName, int page, int dpi) { try { var pdf = PdfDocument.Load(pdfpath); //pdf页数 var pdfpage = pdf.PageCount; //pdf大小 var pagesizes = pdf.PageSizes; if (pdfpage >= page) { Size size = new Size(); size.Height = (int)pagesizes[page - 1].Height; size.Width = (int)pagesizes[page - 1].Width; string imagepathName = imagepath + "\\" + imageName + "." + imageFormat; RenderPage(pdf, page, size, imagepathName, imageFormat, dpi); } } catch (Exception) { throw; } } /// <summary> /// 得到路径下文件的大小 MB /// </summary> /// <param name="path"></param> /// <returns></returns> public double getImageSize(string path) { FileInfo fileInfo = new FileInfo(path); double length = Convert.ToDouble(fileInfo.Length); double Size = length / 1024 / 1024; return Size; } /// <summary> /// /// </summary> /// <param name="document">PDF文件</param> /// <param name="pageNumber">PDF当前页</param> /// <param name="size">PDF大小</param> /// <param name="outputPath">保存路径</param> /// <param name="imageFormat">图片类型</param> /// <param name="dpi">dpi</param> public void RenderPage(PdfDocument document, int pageNumber, System.Drawing.Size size, string outputPath, ImageFormat imageFormat, int dpi) { //using (var document = PdfDocument.Load(pdfPath)) using (var stream = new FileStream(outputPath, FileMode.Create)) using (var image = GetPageImage(pageNumber, size, document, dpi)) { image.Save(stream, imageFormat); } } /// <summary> /// 生成图片 /// </summary> /// <param name="pageNumber">pdf文件张数</param> /// <param name="size">pdf文件尺寸</param> /// <param name="document">pdf文件位置</param> /// <param name="dpi"></param> /// <returns></returns> private System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfiumViewer.PdfDocument document, int dpi) { return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.CorrectFromDpi); } #endregion }