当前位置:网站首页>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);
}
}边栏推荐
猜你喜欢

Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor

BluePrism流程业务对象的组件功能介绍-RPA第三章

UE5编辑器Slate快速入门【开篇】

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

正则表达式

Week 6 Convolutional Neural Networks (CNNs)

【MySQL】 - 索引原理与使用

three.js 给地球加标签和弹窗

An open source web drawing board is really convenient

vs如何读取mysql中的数据(顺便通过代码解决了中文乱码问题)
随机推荐
Decompile jar files (idea environment)
【ffmpeg】给视频文件添加时间戳 汇总
C#将PDF文件转成图片
svn使用碎碎念
会议OA之会议排座&送审
花1200亿修一条“地铁”,连接4个万亿城市,广东在想啥?
SQL injection
一文读懂 .NET 中的高性能队列 Channel
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
超强接口协作平台如何打造:细数Apifox的六把武器
u盘损坏怎么恢复原来数据,u盘损坏数据如何恢复
计算机组成原理常见面试题目总结,含答案
Excel-VBA 快速上手(十二、Like 比较的常见用法)
FastTunnel-开源内网穿透框架
.net GC workflow
How to wait for the return results of multiple asynchronous tasks synchronously?
Excel-VBA 快速上手(十、提示框、可输入的弹出框)
Kingbases SQL language reference manual of Jincang database (16. SQL statement: create sequence to delete)
开源 | AREX-携程无代码侵入的流量回放实践
C # use the default transformation method