WPF 将PPT,Word转成图片
在Office下,PowerPoint可以直接把每张幻灯片转成图片,而Word不能直接保存图片。所以只能通过先转换成xps文件,然后再转成图片。
一、PPT 保存为图片
/// <summary>
/// 将ppt转成图片
/// </summary>
/// <param name="fileName"></param>
private void SaveToImages(string fileName)
{
var presentation = _application.Presentations.Open(fileName, MsoTriState.msoFalse, MsoTriState.msoFalse,
MsoTriState.msoFalse); presentation.SaveAs(_path, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoTrue);
}
二、Word转成图片
/// <summary>
/// Word转xps
/// </summary>
public string ConvertDocToXps(string filePath)
{
var application = new Microsoft.Office.Interop.Word.Application();
application.Documents.Add(filePath);
var name = System.IO.Path.GetFileNameWithoutExtension(filePath) + ".xps";
var path = System.IO.Path.Combine(_path, name);
try
{
application.ActiveDocument.ExportAsFixedFormat(path, WdExportFormat.wdExportFormatXPS);
}
catch (Exception)
{
return string.Empty;
}
finally
{
application.Documents.Close();
application.Quit();
}
return filePath;
}
/// <summary>
/// xps 转jpg图片
/// </summary>
/// <param name="path"></param>
/// <param name="dirPath"></param>
/// <returns></returns>
public bool XpsToImages(string path, string dirPath)
{
if (string.IsNullOrEmpty(dirPath)) return false; if (dirPath[dirPath.Length - 1] != '\\')
dirPath += "\\"; if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath); var xpsDocument = new XpsDocument(path, FileAccess.Read);
FixedDocumentSequence document = xpsDocument.GetFixedDocumentSequence(); MemoryStream[] streams = null;
try
{ int pageCount = document.DocumentPaginator.PageCount;
DocumentPage[] pages = new DocumentPage[pageCount];
for (int i = 0; i < pageCount; i++)
pages[i] = document.DocumentPaginator.GetPage(i); streams = new MemoryStream[pages.Count()]; for (int i = 0; i < pages.Count(); i++)
{
DocumentPage source = pages[i];
streams[i] = new MemoryStream(); RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)source.Size.Width,
(int)source.Size.Height,
96d,
96d,
PixelFormats.Default); renderTarget.Render(source.Visual); JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 100;
encoder.Frames.Add(BitmapFrame.Create(renderTarget)); encoder.Save(streams[i]); FileStream file = new FileStream(dirPath + "Page_" + (i + 1) + ".jpg", FileMode.Create);
file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length);
file.Close(); streams[i].Position = 0;
}
}
catch (Exception e1)
{
return false;
}
finally
{
if (streams != null)
{
foreach (MemoryStream stream in streams)
{
stream.Close();
stream.Dispose();
}
}
} return true;
}
WPF 将PPT,Word转成图片的更多相关文章
- C# Asp.Net 实现PPT/PDF转成图片(不依赖office)
最近公司有个需求,将PPT课件转成图片列表,然后在前端展示成轮播图,于是一开始通过Microsoft.Office.Interop.PowerPoint包实现了这个需求具体代码如下: /// < ...
- pdf ppt word office转图片 教学白板
https://zh-cn.libreoffice.org/ http://www.imagemagick.org/script/ 首先用libreoffice将ppt转换为pdf格式,然后再用con ...
- 使用Aspose.Words把 word转成图片
Document doc = new Document("f:\\333.doc"); ImageSaveOptions iso = new ImageSaveOptions(Sa ...
- Java中Office(word/ppt/excel)转换成HTML实现
运行条件:JDK + jacob.jar + jacob.dll 1) 把jacob.dll在 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin\ 以及C:\WINDOWS\sys ...
- PPT文件流转为图片,并压缩成ZIP文件输出到指定目录
实现流程: 接收InputStream流->复制流->InputStream流转为PPT->PPT转为图片->所有图片压缩到一个压缩文件下 注意: 1.PPT文件分为2003和 ...
- WPF GDI+字符串绘制成图片(二)
原文:WPF GDI+字符串绘制成图片(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...
- WPF GDI+字符串绘制成图片(一)
原文:WPF GDI+字符串绘制成图片(一) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...
- WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片
原文:WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片 1.图片文件转换成Visual对象 private Visual CreateVisual(string imag ...
- Java 将 PPT 形状(表格、文本框、心形、图表等)保存成图片
MS PowerPoint中的表格.文本框.心形.图表.图片等均可以称为形状,将这些形状保存成图片,便可分类储存,方便日后查找,再利用. 本文将介绍如何使用 Spire.Presentation fo ...
随机推荐
- 【POJ 2250】Compromise(最长公共子序列LCS)
题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...
- python 变量命名规范
python源码和其他一些书籍,命名各种个性,没有一个比较统一的命名规范.于是总结了一些,供参考. 模块名: 模块应该使用尽可能短的.全小写命名,可以在模块命名时使用下划线以增强可读性.同样包的命名也 ...
- javascript显示实时时间
<html> <script language=Javascript> function time(){ //获得显示时间的div t_div = document.getEl ...
- php中图片文件的导入,上传与下载
---------------------------------------------图片的导入-------------------------------------------------- ...
- Oracle 11g ORA-00845: MEMORY_TARGET not supported on this system
启动Oracle 11gR2后报错:ORA-00845 rac1:/home/oracle> sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3.0 ...
- C# 三种实现抖屏的方式
//int a = -2; //this.BringToFront(); //for (int i = 0; i < 20; i++) //{ // a = -a; // this.Locati ...
- java ssl https 连接详解 生成证书
我们先来了解一下什么理HTTPS 1. HTTPS概念 1)简介 HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全 ...
- vsftpd的安装
好像linux下的服务的配置和文件分布都差不多, 如httpd, vsftpd, named. 都是: 在/etc/???下面进行配置???.conf, 然后在/var/???放置实际要处理的文件/目 ...
- Ajax技术详解
Ajax技术:Ajax描述了一种主要使用脚本(JS)操纵HTTP的web应用架构,它的主要特点是使用脚本操纵HTTP和web服务器进行数据交换,不会导致页面重载.Ajax的核心是JS的XMLHttpR ...
- javascript单体模式
单体模式的思想在于保证一个特定类仅有一个实例.这意味着当第二次使用同一个类创建的新对象的时候,应该得到与第一个所创建的对象完全相同. javacript中并没有类,因此对单体咬文嚼字的定义严格来说并没 ...