C# Asp.Net 实现PPT/PDF转成图片(不依赖office)
/// <summary>
/// 将PPT转换为图片
/// </summary>
/// <param name="pptPath"></param>
/// <param name="imgPath"></param>
public static List<string> UploadPptImage(HttpRequestBase request)
{
var imgUrls = new List<string>();
var file = request.Files["ppt_file"];
if (string.IsNullOrEmpty(file.FileName))
{
return new List<string>();
}
var path = AppDomain.CurrentDomain.BaseDirectory + "imagesfromppt/" + file.FileName;
var savepath = AppDomain.CurrentDomain.BaseDirectory + "ppt/"; if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
if (!System.IO.Directory.Exists(savepath))
{
System.IO.Directory.CreateDirectory(savepath);
}
var filepath = Path.Combine(savepath, file.FileName);
file.SaveAs(filepath);
Microsoft.Office.Interop.PowerPoint.Application application = null;
Presentation persentation = null;
var imagPathList = new List<string>();
try
{
application = new Microsoft.Office.Interop.PowerPoint.Application();
persentation = application.Presentations.Open(filepath);
//persentation.Slides[1].Export(path + "\\page" + 1 + ".jpg", "JPG", 800, 600);
for (var k = 1; k <= persentation.Slides.Count; k++)
{
var imgPath = path + "\\page" + k + ".jpg";
imagPathList.Add(imgPath);
persentation.Slides[k].Export(imgPath, "JPG", 800, 600);
}
imagPathList.ForEach(p =>
{
using (MemoryStream ms = new MemoryStream())
{
using (var fs = new FileStream(p, FileMode.Open))
{
byte[] buffer = new byte[1024];
int result = 0;
while ((result = fs.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, result);
}
}
ms.Seek(0, SeekOrigin.Begin);
var fileData = ms.ToArray();
string url = ;//此处为云存储,不重要,上传后获得url
imgUrls.Add(url);
}
});
}
catch (Exception ex)
{
//记录异常
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
imagPathList.ForEach(p =>
{
if (File.Exists(p))
{
File.Delete(p);
}
});
if (File.Exists(filepath))
{
File.Delete(filepath);
}
if (Directory.Exists(path))
{
Directory.Delete(path);
}
}
return imgUrls;
}
/// <summary>
/// 将PPT转换为图片
/// </summary>
/// <param name="pptPath"></param>
/// <param name="imgPath"></param>
public static List<string> UploadPptImage(HttpRequestBase request)
{
var imgUrls = new List<string>();
var file = request.Files["ppt_file"];
if (string.IsNullOrEmpty(file.FileName))
{
return new List<string>();
}
Presentation ppt = new Presentation();
ppt.LoadFromStream(file.InputStream, FileFormat.Auto);
var slidescount = ppt.Slides.Count;
try
{
if (slidescount > 0)
{
for (int i = 0; i < slidescount; i++)
{
ppt.Slides[i].SaveAsImage();//把ppt转换成emf格式图片
Image image = ppt.Slides[i].SaveAsImage();
using (var ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
var fileData = ms.ToArray();
string url = ;//云存储,返回url
imgUrls.Add(url);
}
}
}
}
catch (Exception ex)
{
//记录异常
}
return imgUrls;
}
1、O2S.Components.PDFRender4NET.dll
2、Acrobat.dll
3. PDFLibNet.dll
4. Ghostscript
/// <summary>
/// 将PPT转换为图片
/// </summary>
/// <param name="pptPath"></param>
/// <param name="imgPath"></param>
public static List<string> UploadPptImage(HttpRequestBase request)
{
var imgUrls = new List<string>();
var file = request.Files["ppt_file"];
if (string.IsNullOrEmpty(file.FileName))
{
return new List<string>();
}
Presentation ppt = new Presentation(file.InputStream); using (var mspdf = new MemoryStream())
{
var pageCount = ppt.Slides.Count;
ppt.Save(mspdf, Aspose.Slides.Export.SaveFormat.Pdf); using (var pdf = PdfDocument.Load(mspdf))
{
try
{
var pagesizes = pdf.PageSizes;
for (int i = 0; i < pdf.PageCount; i++)
{
Size size = new Size();
size.Height = (int)pagesizes[(i)].Height;
size.Width = (int)pagesizes[(i)].Width;
using (var image = pdf.Render(i, size.Width, size.Height, PdfRenderFlags.Annotations))
{
using (var ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
var fileData = ms.ToArray();
string url = ;//云存储,返回url
imgUrls.Add(url);
}
}
}
}
catch (Exception ex)
{
//异常记录
}
}
}
return imgUrls;
}
速度还可以,没有水印,不需要存储到服务器,基本都是流操作,完美!!!
C# Asp.Net 实现PPT/PDF转成图片(不依赖office)的更多相关文章
- C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)
原文:C#技术分享[PDF转换成图片--13种方案](2013-07-25重新整理) 重要说明:本博已迁移到 石佳劼的博客,有疑问请到 文章新地址 留言!!! 写在最前面:为了节约大家时间,撸主把最常 ...
- 【译】在Asp.Net中操作PDF – iTextSharp - 操作图片
原文 [译]在Asp.Net中操作PDF – iTextSharp - 操作图片 作为我的iTextSharp系列的文章的第七篇,开始探索使用iTextSharp在PDF中操作图片,理解本篇文章需要看 ...
- C#技术分享【PDF转换成图片——11种方案】
1.[iTextSharp.dll],C# 开源PDF处理工具,可以任意操作PDF,并可以提取PDF中的文字和图片,但不能直接将PDF转换成图片. DLL和源码 下载地址:http://downloa ...
- 在linux环境下使用icepdf或pdfbox将pdf转化成图片是乱码解决
在linux环境下使用icepdf或pdfbox将pdf转化成图片是出现乱码,网上查发下是itextpdf生成pdf引用"STSong-Light"字体而linux环境下没有这个字 ...
- 【PyMuPDF和pdf2image】Python将PDF转成图片
前言: 在最近的测试中遇到一个与PDF相关的测试需求,其中有一个过程是将PDF转换成图片,然后对图片进行测试. 粗略的试了好几种方式,其中语言尝试了Python和Java,总体而言所找到的Python ...
- WPF 将PPT,Word转成图片
在Office下,PowerPoint可以直接把每张幻灯片转成图片,而Word不能直接保存图片.所以只能通过先转换成xps文件,然后再转成图片. 一.PPT 保存为图片 /// <summary ...
- php 将pdf转成图片且将图片拼接
说明: 1.pdf转图片通过安装php扩展imagick实现. 2.由于windows扩展安装的一系列问题,建议在linux环境开发,windows大伙可以尝试安装. 3.为Centos 安装Imag ...
- Java PDF转换成图片并输出给前台展示
首先需要导入所需工具类 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fo ...
- pdf转成图片
http://blog.csdn.net/wydhao123/article/details/51596028 jar commons-logging.jar. pdfbox-2.0.1.jar.fo ...
随机推荐
- HashMap、ConcurrentHashMap 1.7和1.8对比
本篇内容是学习的记录,可能会有所不足. 一:JDK1.7中的HashMap JDK1.7的hashMap是由数组 + 链表组成 /** 1 << 4,表示1,左移4位,变成10000,即1 ...
- kubernetes 的API 介绍
在API conventions doc中描述了API的全部协议. 在API Reference文档中描述了API的端点.资源类型和示例. 在Controlling API Access doc中讨论 ...
- PHP程序员要掌握哪些技术
PHP程序员要掌握哪些技术 第一阶段: (PHP+MySQL核心编程)面向对象编程MySQL数据库,MySQL的优化细节.HTTP协议,http也是我们web开发的基石.对我们了解PHP底层机制有很大 ...
- SpringBoot + Dubbo + Zookper 整合
经过2个小时的调试终于弄完了,过程如下, 环境: JDK1.8 .Springboot2.2.6. Windows10系统 如果不看Dubbo 管理页面的话就不用下载 Dubbo-domain了,这个 ...
- 【Scrapy(二)】Scrapy 中的 Pipline,Item,Shell组件
Pipline: 1.爬虫项目与爬虫的区别与关联: 一个爬虫项目可以包含多个爬虫,如下图中爬虫项目firstspider 包含多个爬虫itcst 和爬虫itcast1 2.多个爬虫是公用一套Pipli ...
- 【原创】ansible常用模块整理
一.Ansible模块帮助 ansible-doc -l # 列出 Ansible 支持的模块 ansible-doc ping # 查看该模块帮助信息 二.远程命令模块 2.1command com ...
- Day003 注释、标识符和关键字
注释.标志符.关键字 注释 当项目逐渐复杂,注释就很重要了. 注释不会被执行,是给我们写代码的人看的. 书写注释是一个非常好的习惯. Java中的注释有三种: 当行注释 // 多行注释 /* 注释 * ...
- android之Frame Animation
一.在xml文件中设置帧动画 1.首先得在drawable资源文件夹下创建一个animation_list文件 <?xml version="1.0" encoding=&q ...
- 使用DevExpress的GridControl实现多层级或无穷级的嵌套列表展示
在我早期的随笔<在GridControl表格控件中实现多层级主从表数据的展示>中介绍过GridControl实现二级.三级的层级列表展示,主要的逻辑就是构建GridLevelNode并添加 ...
- XCTF.MISC 新手篇
目录 泪目 1.this_is_flag 2.pdf 3.如来十三掌 4.give_you_flag 5.坚持60s 6.gif 7.掀桌子 8.ext3 9.stegano 10.SimpleRAR ...