C# PDF转图片(JPG,Png)的项目实践
作者:三岁Funny 时间:2021-11-10 17:59:24
背景:最近需要做一个任务:C# PDF文件需要传输为JPG图片。一开始没有头绪,最后去github找到了现在我用的PdfiumViewer组件(免费)实现了功能。
现在分享一下:
【项目框架是:.NET FrameWork 4.6.1 版本】
第一步:用nuget安装两个包:
PdfiumViewer
PdfiumViewer.Native.x86.v8-xfa(注意不是64位版的)
注:该2个包没有版本要求,本次测试使用包版本如下图供参考:
注:编译代码后需检测项目bin目录必须包含pdfium.dll,入下图:
第二步:附上如下代码:
方法一:
/// <summary>
/// 根据传入参数直接转换
/// </summary>
/// <param name="inFilePath">入参示例:E:\\download\\1111.pdf</param>
/// <param name="outFilePath">入参示例:E:\\download</param>
/// <param name="format">入参示例:Jpeg (转换后文件格式,如:Jpeg,Png等)</param>
public static void PDFConvertTo(string inFilePath, string outFilePath, ImageFormat format)
{
outFilePath = $"{outFilePath}\\{DateTime.Now.ToString("yyyyMMdd-HHmmss")}";
using (var document = PdfDocument.Load(inFilePath))
{
var pageCount = document.PageCount;
for (int i = 0; i < pageCount; i++)
{
string outFile = $"{outFilePath}({i+1}).{format}";
var dpi = 300;
using (var image = document.Render(i, dpi, dpi, PdfRenderFlags.CorrectFromDpi))
{
var encoder = ImageCodecInfo.GetImageEncoders()
.First(c => c.FormatID == format.Guid);
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 10L);
image.Save(outFile, encoder, encParams);
}
}
}
}
调用方法入参示例:
{
"inFilePath":"E:\\download\\search.pdf",
"outFilePath":"E:\\download",
"OutFileFormat":"Png"
}
转换pdf有11页,转换后文件如下图:
方法二:
/// <summary>
/// <param name="filePath">pdf文件路径</param>
/// <param name="outFilePath">picture输出文件路径</param>
/// <param name="image">文件格式:Jpeg (转换后文件格式,如:Jpeg,Png等)</param>
/// </summary>
public void PdfToPic(string filePath, string outFilePath, ImageFormat image)
{
var pdf = PdfDocument.Load(filePath);
var pdfpage = pdf.PageCount;
var pagesizes = pdf.PageSizes;
outFilePath = $"{outFilePath}\\{DateTime.Now.ToString("yyyyMMdd-HHmmss")}";
for (int i = 1; i <= pdfpage; i++)
{
Size size = new Size();
size.Height = (int)pagesizes[(i - 1)].Height;
size.Width = (int)pagesizes[(i - 1)].Width;
//可以把".jpg"写成其他形式 RenderPage(filePath, i, size, picPath);
string outFile = $"{outFilePath}({i}).{image}";
RenderPage(filePath, i, size, outFile, image);
}
}
private void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, ImageFormat mat, int dpi = 300)
{
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, mat);
}
private static System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfDocument document, int dpi)
{
return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations);
}
方法二和方法一区别:转换后文件像素大小不同。这个就影响了图片的质量。
原因分析:对比两个方法,dpi(分辨率值)都是设置为300,为啥转换后picture 像素不同呢。
原因:参数设置不同。
注释:
PdfRenderFlags.Annotations:设置是否要渲染注释
PdfRenderFlags.CorrectFromDpi:DPI的正确高度/宽度。更正DPI=8192
参数都设置为:PdfRenderFlags.CorrectFromDpi
转换picture文件像素就是最合适的了。
PS:需要转其他格式的或者调整图片质量的 稍微改造一下就好了。
来源:https://blog.csdn.net/tnb_ml/article/details/124915260
标签:C#,PDF,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot 通过代码自动生成pid的方法
2023-11-27 02:54:57
![](https://img.aspxhome.com/file/2023/6/60366_0s.png)
struts2中使用注解配置Action方法详解
2023-08-30 00:01:25
C语言编程C++动态内存分配示例讲解
2023-11-02 18:00:12
![](https://img.aspxhome.com/file/2023/5/105185_0s.png)
SpringBoot重写addResourceHandlers映射文件路径方式
2022-05-26 19:02:40
![](https://img.aspxhome.com/file/2023/3/66183_0s.jpg)
MyBatis拦截器原理探究
2021-07-18 23:24:19
java多线程-读写锁原理
2021-07-20 17:28:52
java设计模式学习之工厂方法模式
2023-10-12 17:19:04
![](https://img.aspxhome.com/file/2023/7/58607_0s.jpg)
IDEA简单实现登录注册页面
2023-08-06 19:10:37
Java中的泛型详解
2023-11-02 17:51:56
Java基本数据类型族谱与易错点梳理解析
2021-08-18 10:20:27
![](https://img.aspxhome.com/file/2023/3/85533_0s.png)
DataBinding onClick的七种点击方式
2021-12-08 01:23:22
Java中静态代码块、构造代码块、构造函数和普通代码块的区别
2023-11-25 10:09:06
![](https://img.aspxhome.com/file/2023/8/60158_0s.png)
idea搭建SSM框架遇踩的坑(附完整过程)
2023-06-23 09:53:07
![](https://img.aspxhome.com/file/2023/6/66506_0s.png)
谈谈你可能并不了解的java枚举
2023-11-09 21:08:55
![](https://img.aspxhome.com/file/2023/1/59021_0s.png)
Java reservedcodecachesize虚拟机参数案例详解
2022-03-15 12:18:48
![](https://img.aspxhome.com/file/2023/9/62029_0s.png)
Java项目实现寻找迷宫出路
2022-10-05 14:08:46
![](https://img.aspxhome.com/file/2023/0/62980_0s.jpg)
Spring5中的WebClient使用方法详解
2023-08-05 14:50:24
Eclipse添加xml文件提示及Hibernate配置学习
2023-08-17 05:01:47
spring多数据源配置实现方法实例分析
2023-06-06 22:15:56
详解ThreadLocal为什么会内存溢出原理
2023-11-09 18:45:26
![](https://img.aspxhome.com/file/2023/3/59183_0s.png)