当前位置:网站首页>C # convert PDF files into pictures
C # convert PDF files into pictures
2022-07-26 20:13:00 【biyusr】

Preface
Today, a colleague told me what you got pdf The file is a little inconsistent with our existing software process , Can you take us pdf Turn the file into a picture ! Do as you say , Because I have studied for some time before pdf Related components of the file , So I am github Before you found me on the star The warehouse of ,PdfiumViewer Open source address : https://github.com/pvginkel/PdfiumViewer:

First let's open up Nuget install PdfiumViewer and ImageResizer.Plugins.PdfiumRenderer.Pdfium.Dll.

For a demand , We should know what properties the object we operate on has ?
So we get pdf The file should know ,pdf It has attributes such as the number of pages and the height and width of the file .
For pictures , Picture has height , Width ( The resolution of the ) And horizontal resolution and vertical resolution (dpi) And other attributes are more important !
After we understand these properties, we can start to work , The first step is to load our pdf file , Get the number of pages and size of the file .
var pdf = PdfiumViewer.PdfDocument.Load(strpdfPath);
var pdfpage = pdf.PageCount;
var pagesizes = pdf.PageSizes;
Then assemble the height, width, horizontal resolution, vertical resolution and other attributes of the picture
document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations);
Finally, save the picture
image.Save(stream, ImageFormat.Jpeg);
The complete code is as follows
public class PdfToImage
{
/// <summary>
///
/// </summary>
/// <param name="filePath">pdf File path </param>
/// <param name="picPath">picture File path </param>
public void PdfToPic(string filePath, string picPath)
{
var pdf = PdfiumViewer.PdfDocument.Load(filePath);
var pdfpage = pdf.PageCount;
var pagesizes = pdf.PageSizes;
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;
// You can put ".jpg" Write in other forms
RenderPage(filePath, i, size, picPath);
}
}
private void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, int dpi = 300)
{
using (var document = PdfiumViewer.PdfDocument.Load(pdfPath))
using (var stream = new FileStream(outputPath, FileMode.Create))
using (var image = GetPageImage(pageNumber, size, document, dpi))
{
image.Save(stream, ImageFormat.Jpeg);
}
}
private static System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfiumViewer.PdfDocument document, int dpi)
{
return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations);
}
}边栏推荐
- 猎聘问卷星,成为微信「寄生虫」
- UE5编辑器Slate快速入门【开篇】
- Kingbases SQL language reference manual of Jincang database (13. SQL statement: alter synonym to comment)
- 金仓数据库 KingbaseES SQL 语言参考手册 (12. SQL语句:ALTER LANGUAGE 到 ALTER SUBSCRIPTION)
- 网络与VPC动手实验
- Strengthen supervision on secret room escape and script killing, and focus on strengthening fire safety and juvenile protection
- Canvas graphics
- An open source web drawing board is really convenient
- 有点酷,使用 .NET MAUI 探索太空
- 福建争抢VC/PE
猜你喜欢

Decompile jar files (idea environment)

There is an Oolong incident in open source. Maybe you don't know these five open source protocols

如何实现一个能处理海量数据的异步任务队列系统(至尊典藏版)

Ten sorting details

.NET GC工作流程

DevSecOps,让速度和安全兼顾

N圆最密堆积、最小外接正方形的matlab求解(二维、三维等圆Packing 问题)

C# 使用默认转型方法

What should we do about the fragmentation of internal information? Try this

【Android】Kotlin 快速编译背后的黑科技,了解一下~
随机推荐
负载均衡的使用
事务回滚,同时记录异常信息
网络与VPC动手实验
使用请求头认证来测试需要授权的 API 接口
金仓数据库 KingbaseES SQL 语言参考手册 (20. SQL语句: MERGE 到 VALUES)
[Android] the black technology behind kotlin's rapid compilation. Learn about it~
MySQL之InnoDB引擎(五)
DevOps 实践多年,最痛的居然是?
一年卖7亿,德州扒鸡赶考IPO
Strengthen supervision on secret room escape and script killing, and focus on strengthening fire safety and juvenile protection
.net GC workflow
猎聘问卷星,成为微信「寄生虫」
Kingbases SQL language reference manual of Jincang database (15. SQL statement: create materialized view to create schema)
同花顺靠谱吗?我刚开始学习理财,开证券账户安全吗?
cv2.resize()
模拟身份验证
Kingbasees SQL language reference manual of Jincang database (20. SQL statements: merge to values)
Canvas graphics
Excel-VBA 快速上手(十、提示框、可输入的弹出框)
There is an Oolong incident in open source. Maybe you don't know these five open source protocols