.Net的PDF转图片
用的是破解版的 O2S.Components.PDFRender4NET.dll 插件, 简单引用即可
public static class PdfToImage
{
public static MemoryStream GetPdfImagePageStream(string pdfInputPath, int pageIndex, ImageFormat format, int width = , int height = , int quality = )
{
try
{
//pdf处理插件
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
int total = pdfFile.PageCount; #region 防止异常参数
if (pageIndex < )
{
pageIndex = ;
}
if (pageIndex > total)
{
pageIndex = total - ;
}
if (quality < )
{
quality = ;
}
if (quality > )
{
quality = ;
}
if (width <= )
{
width = ;
} if (height <= )
{
height = ;
}
#endregion //pdf转换图片
SizeF pageSize = pdfFile.GetPageSize(pageIndex); Bitmap pageImage = pdfFile.GetPageImage(pageIndex, * quality); MemoryStream ms = new MemoryStream(); pageImage.Save(ms, format); //原图
Image img = Image.FromStream(ms, true); double ratio = (double)width / (double)height; double oRatio = (double)img.Width / (double)img.Height; int sbWidth = ; int sbHeight = ; int outX = ;
int outY = ; if (oRatio < ratio)
{
sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));
sbHeight = height; outX = (width - sbWidth) / ;
}
else
{
sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));
sbWidth = width; outY = (height - sbHeight) / ;
} //缩放
Image sbImg = new Bitmap(sbWidth, sbHeight);
Graphics sbGra = Graphics.FromImage(sbImg);
sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
sbGra.Clear(Color.White);
sbGra.DrawImage(img, new System.Drawing.Rectangle(, , sbWidth, sbHeight), new System.Drawing.Rectangle(, , img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel); //补白
Image outImg = new System.Drawing.Bitmap(width, height);
Graphics outGra = System.Drawing.Graphics.FromImage(outImg);
outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
outGra.Clear(Color.White);
outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(, , sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel); MemoryStream outMs = new MemoryStream(); outImg.Save(outMs, format); sbImg.Dispose();
outImg.Dispose();
img.Dispose(); return outMs; }
catch (Exception ex)
{ } return new MemoryStream();
} public static MemoryStream GetPdfImagePageStream(Stream stream, int pageIndex, ImageFormat format, int width = , int height = , int quality = )
{
try
{
//pdf处理插件
PDFFile pdfFile = PDFFile.Open(stream);
int total = pdfFile.PageCount; #region 防止异常参数
if (pageIndex < )
{
pageIndex = ;
}
if (pageIndex > total)
{
pageIndex = total - ;
}
if (quality < )
{
quality = ;
}
if (quality > )
{
quality = ;
}
if (width <= )
{
width = ;
} if (height <= )
{
height = ;
}
#endregion //pdf转换图片
SizeF pageSize = pdfFile.GetPageSize(pageIndex); Bitmap pageImage = pdfFile.GetPageImage(pageIndex, * quality); MemoryStream ms = new MemoryStream(); pageImage.Save(ms, format); //原图
Image img = Image.FromStream(ms, true); double ratio = (double)width / (double)height; double oRatio = (double)img.Width / (double)img.Height; int sbWidth = ; int sbHeight = ; int outX = ;
int outY = ; if (oRatio < ratio)
{
sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));
sbHeight = height; outX = (width - sbWidth) / ;
}
else
{
sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));
sbWidth = width; outY = (height - sbHeight) / ;
} //缩放
Image sbImg = new Bitmap(sbWidth, sbHeight);
Graphics sbGra = Graphics.FromImage(sbImg);
sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
sbGra.Clear(Color.White);
sbGra.DrawImage(img, new System.Drawing.Rectangle(, , sbWidth, sbHeight), new System.Drawing.Rectangle(, , img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel); //补白
Image outImg = new System.Drawing.Bitmap(width, height);
Graphics outGra = System.Drawing.Graphics.FromImage(outImg);
outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
outGra.Clear(Color.White);
outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(, , sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel); MemoryStream outMs = new MemoryStream(); outImg.Save(outMs, format); sbImg.Dispose();
outImg.Dispose();
img.Dispose(); return outMs; }
catch (Exception ex)
{ } return new MemoryStream();
}
}
DLL下载: O2S.Components.PDFRender4NET.dll
.Net的PDF转图片的更多相关文章
- C# 给PDF添加图片背景
C# 给PDF添加图片背景 今天要实现的是给PDF文件添加图片背景这个功能.PDF是近年来最流行的文件之一,无论是办公还是日常生活中都经常会用到,很多时候,PDF文件的背景色都是白色,看多了难免觉得累 ...
- C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统
itextsharp.dll,是一个开源的在C#中用来生成PDF文档的库文件,不少C#爱好者用它制作出了PDF文档生成器.使用时只需在你的C#项目中添加引入此组件即可,使用方法网上有很多,自己查阅一下 ...
- PDF转图片 C# with Adobe API
PDF转图片大概有十几种方式,褒贬不一,我就详细给大家说一下我认为效率最高的方式,使用Adobe官方的SDK 安装acrobat reader 9.0以上即可,勾选如下组件.
- 在线提取PDF中图片和文字
无需下载软件,你就可以在线提取PDF中图片和文字,http://www.extractpdf.com/不仅可以获取本地PDF文档的图片和文字,还能获取远程PDF文档的图片和文字.如下图所示:结果本人测 ...
- 使用pdfbox分页保存pdf为图片
一.背景 pdfbox作为Apache开源的PDF操作工具,允许创建新的PDF文档,操作现有文档,以及从文档中提取内容的能力.Apache PDFBox还包括一些命令行实用工具.本文楼主主要介绍其中的 ...
- JAVA中pdf转图片的方法
JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用. 一.icepdf.有收费版和开源版,几种方法里最推荐的.转换的效果比较好,能识别我手头文件中的中文, ...
- 【JAVA】pdf转图片
最近java项目中使用到了pdf转图片的需求,在此记录一下. 1.基于GhostScript p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px ...
- PDF转图片工具
点击下载( 提取码:1ll1 ) 软件功能基于mupdf,UI使用wxpython开发 功能: 支持pdf转图片,图片格式png 支持批量转换 使用: 第一步,点击按钮添加文档到列表,或直接将待转换文 ...
- Ubuntu下pdf和图片互转
前边文章可以将ppt转换为pdf 查看 使用unoconv将ppt转为pdf,再使用imagemagick将pdf转为图片 这次想将pdf和图片进行互转 当前目录下只有2.ppt 1.ppt转pdf ...
随机推荐
- 使用VS Code开发AngularJS 2 第一个应用程序
使用VS Code开发AngularJS 2 第一个应用程序 目录 运行环境 创建项目 安装依赖包 创建TypeScript应用程序 配置应用程序 运行应用程序 运行环境 运行环境: Windows ...
- Asp.Net 下载文件的几种方式
asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...
- Android -------- 序列化器生成xml文件
- chkconfig(check config)命令
功能说明:chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 语 法:chkconfig ...
- nginx log日志分割
@echo offrem 备份并根据时间重命名错误日志文件set "cmdstr=move E:\nginx\logs\error.log E:\nginx\logs\error%date: ...
- C#高级知识点概要(2) - 线程并发锁
本文目录: 线程的简单使用 并发和异步的区别 并发控制 - 锁 线程的信号机制 线程池中的线程 案例:支持并发的异步日志组件 线程的简单使用 常见的并发和异步大多是基于线程来实现的,所以本文先讲线程的 ...
- UVALive 5792 Diccionário Portuñol
字符串匹配问题 有n个a串个m个b串,讲a的前缀和b的后缀粘在一起有多少个不同的新串. 首先求不同的前缀和后缀肯定好求了,就用字典树分别存一下a个倒过来的b. 那个问题就是解决例如,abcd,和bcd ...
- CF 277E Binary Tree on Plane (拆点 + 费用流) (KM也可做)
题目大意: 平面上有n个点,两两不同.现在给出二叉树的定义,要求树边一定是从上指向下,即从y坐标大的点指向小的点,并且每个结点至多有两个儿子.现在让你求给出的这些点是否能构成一棵二叉树,如果能,使二叉 ...
- C# 汉子增加UTF-8头
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Conv ...
- web标准(复习)--3 二列和三列布局
今天学习二列和三列布局,将涉及到以下内容和知识点 二列自适应宽度 二列固定宽度 二列固定宽度居中 xhtml的块级元素(div)和内联元素(span) float属性 三列自适应宽度 三列固定宽度 三 ...