.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 ...
随机推荐
- (转)js 中{},[]中括号,大括号使用详解
一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数. 如:var LangShen = {"Name":"Langshen",&qu ...
- ORACLE基本SQL语句-添加更新数据函数篇
一.添加数据 /*添加数据*/insert into STU values('stu0004','赵一',18,1,"kc0004");insert into STU(STU_ID ...
- java泛型中? super T和? extends T的区别
<? super T>表示包括T在内的任何T的父类,<? extends T>表示包括T在内的任何T的子类;请记住PECS原则:生产者(Producer)使用extends,消 ...
- [C#] 后端post的请求方法
C# 模拟post请求方法 方法1: /// <summary> /// 模拟Post请求 /// </summary> /// <param name="ur ...
- DOM4J 解析 XML
1.在项目根目录下新建lib文件夹 2.把dom4j文件拷贝到lib文件夹 3.dom4j,右键Build Path---->Add To Build Path 这样就添加dom4j到项目成功 ...
- MySQL 聚簇索引
聚簇索引并不是一种单独的索引类型,而是一种数据存储方式.具体的细节依赖于其实现方式,但innoddb 的聚簇索引实际上在同一个结构中保存了B-Tree索引和数据行. 当表有聚簇索引时,它的数据实际上存 ...
- amchart
amchart能够根据提供的数据便捷的生成好看的图标,曾在项目中遇到使用falsh版以支持对js支持不好的低版本浏览器,但是现在官网上都是js版本的,flash版的文档都没有,搜索结果一般都是链接到博 ...
- python django学习资料网站
python module 模块 https://docs.python.org/2.7/py-modindex.html django框架例子 https://docs.djangoproject. ...
- 漫谈项目设计&重构&性能优化
重构的好处:重构能够改进软件设计,随着项目需求的变更,项目体积的变大早已与最初的设计大相径庭,代码结构变得凌乱.复杂,如果不进行重构,则很难添加新的功能. 1.使项目代码更容易理解很多情况下是由于项目 ...
- JUnit使用Eclipse建立Test Case - 就是爱Java
传统的测试方式,是以main(),作为代码的起点,而这次Mix将利用JUnit 进行测试,Eclipse有提供完整的整合环境,可以方便地使用JUnit,让Mix快速地进行单元测试,首先,开始撰写第1个 ...