在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;
}

代码下载

 

最新文章

  1. BaseAdapter日常的封装
  2. 转 Jmeter之Bean shell使用(一)
  3. ajax常用参数
  4. GlassFish Server is a compliant implementation of the Java EE 7 platform
  5. LINQ to SQL语句
  6. GTD_百度百科
  7. JSP的九个隐式(内置)对象
  8. [java学习笔记]java语言基础概述之运算符&amp;程序流程控制&amp;for循环嵌套
  9. php中body下出现莫名空白字符
  10. 今年的IT大趋势是虚拟现实
  11. UVA 10801 Lift Hopping
  12. python datetime时间差
  13. Web worker 与JS中异步编程的对比
  14. iOS 实现类似QQ分组样式的几种方式
  15. bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)
  16. PowerShell 显示气球提示框 2
  17. IOS 多文件上传 Java web端(后台) 使用List&lt;MultipartFile&gt; 接收出现的问题
  18. flask下载文件中文IE,Edge,Safari文件名乱码
  19. csdn 不登录浏览全文 chrome 浏览器
  20. FMX.Controls单元 中图形矩阵变换

热门文章

  1. Yii2 radioList设置默认值
  2. hive 使用笔记(partition; HDFS乱码)
  3. groovy-脚本和类
  4. abstract 类 构造函数
  5. html标签页图标
  6. js闭包理解
  7. 工具分享——将C#文档注释生成.chm帮助文档
  8. SQL中CONVERT转化函数的用法
  9. Google搜索命令语法大全
  10. linux wget 命令用法详解(附实例说明)