Loading

C# Pdf转图片通过(PdfiumViewer或O2S.Components.PDFRender4NET)实现

1、通过PdfiumViewer实现,目前测试结果来看是不收费的,可直接通过Nuget添加引用

        /// <summary>
        /// pdf转图片
        /// </summary>
        /// <param name="pdfPath">pdf路径</param>
        /// <param name="imagePath">输出图片路径</param>
        /// <param name="imageName">输出图片名称</param>
        /// <param name="imagePathFormat">输出图片后缀</param>
        /// <param name="imageFormat">输出图片格式</param>
        /// <param name="startPageNum">开始页码</param>
        /// <param name="endPageNum">结束页码</param>
        public static void PdfToImage(string pdfPath,string imagePath, string imageName, string imagePathFormat, System.Drawing.Imaging.ImageFormat imageFormat,int startPageNum, int endPageNum)
        {
            #region 文件夹及路径处理
            if (!System.IO.Directory.Exists(imagePath))
            {
                System.IO.Directory.CreateDirectory(imagePath);
            }
            if (!imagePath.EndsWith("\\") && !imagePath.EndsWith("/"))
            {
                imagePath = imagePath + "\\";
            }
            if (!imagePathFormat.StartsWith("."))
            {
                imagePathFormat = "." + imagePathFormat;
            }
            #endregion
            var pdf = PdfiumViewer.PdfDocument.Load(pdfPath);//读取pdf
            var pdfPage = pdf.PageCount;//pdf页码
            var pdfSize = pdf.PageSizes;
            #region 开始结束页
            if (startPageNum <= 0) { startPageNum = 1; }
            if (endPageNum > pdf.PageCount) { endPageNum = pdf.PageCount; }
            if (startPageNum > endPageNum)//开始>结束
            {
                int tempPageNum = startPageNum;
                startPageNum = endPageNum;
                endPageNum = startPageNum;
            }
            #endregion

            for (int i = startPageNum; i <= endPageNum; i++)
            {
                System.Drawing.Size size = new System.Drawing.Size();
                //pdfSize为list类型,索引从0,而pdf页码从1开始,所以需要-1
                size.Width = (int)pdfSize[i - 1].Width;
                size.Height = (int)pdfSize[i - 1].Height;
                var stream = new System.IO.FileStream($"{imagePath}{imageName}-{i}{imagePathFormat}", System.IO.FileMode.Create);
                var image = pdf.Render(i - 1, size.Width, size.Height, 300, 300, PdfiumViewer.PdfRenderFlags.Annotations);
                image.Save(stream, imageFormat);
                stream.Close();
                image.Dispose();
                stream.Dispose();
                System.Diagnostics.Process.Start(imagePath);
            }
            pdf.Dispose();
        }

 效果如下:

 

2.通过O2S.Components.PDFRender4NET实现,目前测试结果直接通过Nuget添加引用的话是需要收费的,生成出来的图片左上角会带有PDFView4NET 11.1.0.0 evaluation version样式水印,可以通过O2S.Components.PDFRender4NET下载,提取码:1234,通过此链接下载dll然后添加引用,通过该dll实现的生成图片不会有水印文字

      /// <summary>
        /// 图片清晰度
        /// </summary>
        public enum Definition
        {
            One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
        }
        /// <summary>
        /// 将PDF转为图片
        /// </summary>
        /// <param name="pdfPath">PDF文件路径</param>
        /// <param name="imagePath">图片输出路径</param>
        /// <param name="imageName">图片名称</param>
        /// <param name="imagePathFormat">图片格式</param>
        /// <param name="imageFormat">图片输出格式</param>
        /// <param name="startPageNum">从PDF文档的第几页开始转换</param>
        /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
        /// <param name="definition">图片清晰度,数字越大越清晰</param>
        public static void PdfToImage2(string pdfPath,string imagePath, string imageName, string imagePathFormat, System.Drawing.Imaging.ImageFormat imageFormat,int startPageNum, int endPageNum,Definition definition)
        {
            O2S.Components.PDFRender4NET.PDFFile pdfFile = O2S.Components.PDFRender4NET.PDFFile.Open(pdfPath);
            if (!System.IO.Directory.Exists(imagePath))
            {
                System.IO.Directory.CreateDirectory(imagePath);
            }
            if (startPageNum <= 0) { startPageNum = 1; }
            if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; }
            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum;
                startPageNum = endPageNum;
                endPageNum = startPageNum;
            }
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                System.Drawing.Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
                pageImage.Save($"{imagePath}{imageName}-{i}{imagePathFormat}", imageFormat);
                pageImage.Dispose();
            }
            pdfFile.Dispose();
        }

效果如下所示:

 

posted @ 2023-05-05 15:22  流纹  阅读(1233)  评论(1编辑  收藏  举报