.NET 以PdfiumViewer方式PDF转为图片

255 阅读1分钟

创建项目

创建一个.net framework模板

image.png

下载依赖

  1. 安装 PdfiumViewer NuGet 包
  2. 安装 PdfiumViewer.Native.x86.v8-xfa NuGet 包
  3. 安装 PdfiumViewer.Native.x86_64.v8-xfa NuGet 包

image.png

目录结构

image.png

核心方法

PDFHelper

using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp9
{
        public static class PDFHelper
        {
            /// <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,
                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();
            }
        }
        
    }

测试

image.png

image.png

using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp9
{
    internal class Program
    {
        static void Main(string[] args)
        {
            PDFHelper.PdfToImage(
                "Z:\\ocr\\tessdata\\01.pdf",
                "Z:\\ocr\\tessdata\\",
                "transed",
                "png",
                ImageFormat.Png,
                1,
                1
                );
        }
    }
}