创建项目
创建一个.net framework模板
下载依赖
- 安装
PdfiumViewer
NuGet 包 - 安装
PdfiumViewer.Native.x86.v8-xfa
NuGet 包 - 安装
PdfiumViewer.Native.x86_64.v8-xfa
NuGet 包
目录结构
核心方法
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 { ///
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();
}
}
}
```
测试
``` 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 ); } } }
```