c# pdf转图片

需安装nuget包:

<package id="PdfiumViewer" version="2.13.0.0" targetFramework="net45" />

<package id="HiraokaHyperTools.PdfiumViewer.Native.Windows" version="0.1.5637" targetFramework="net45" />




using PdfiumViewer; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; namespace IDH.Common.Utility { public partial class PDFHelper { /// <summary> /// <param name="filePath">pdf文件路径</param> /// <param name="imageSaveNameList">图片名</param> /// <param name="image">文件格式:Jpeg (转换后文件格式,如:Jpeg,Png等)</param> /// </summary> public static List<string> PdfToPic(string filePath, string directoryPath, List<string> imageSaveNameList, ImageFormat image) { var imagePathList = new List<string>(); var pdf = PdfDocument.Load(filePath); var pdfpage = pdf.PageCount; for (int i = 0; i < pdfpage; i++) { var outFilePath = directoryPath + "\\" + imageSaveNameList[i]; if (string.IsNullOrWhiteSpace(Path.GetExtension(outFilePath))) { outFilePath += "." + image.ToString(); } RenderPage(pdf, i, outFilePath, image); imagePathList.Add(outFilePath); } pdf.Dispose(); return imagePathList; } /// <summary> /// 渲染图片 /// </summary> /// <param name="document"></param> /// <param name="pageNumber"></param> /// <param name="size"></param> /// <param name="outputPath"></param> /// <param name="mat"></param> /// <param name="dpi"></param> private static void RenderPage(PdfDocument document, int pageNumber, string outputPath, ImageFormat mat, int dpi = 300) { using (var stream = new FileStream(outputPath, FileMode.Create)) using (var image = GetPageImage(pageNumber, document, dpi)) image.Save(stream, mat); } /// <summary> /// 根据页数转换图片 /// </summary> /// <param name="pageNumber"></param> /// <param name="size"></param> /// <param name="document"></param> /// <param name="dpi"></param> /// <returns></returns> private static System.Drawing.Image GetPageImage(int pageNumber, PdfDocument document, int dpi) { return document.Render(pageNumber, dpi, dpi, PdfRenderFlags.CorrectFromDpi); } } }

  

posted @ 2023-10-25 16:31  元点  阅读(327)  评论(0编辑  收藏  举报