c#实现Word转换PNG图片
由于项目需要,经过一些大神的指导以及github,stackOverflow找资料,写了个这么个程序。
主要是因为word文档有特殊字体,特殊字体处理就要用到EnhMetaFileBits,即获取一页内容的增强图元信息。类型属于 EmfPlusDual。
在此,特别感谢github,stackOverflow,csdn这三个网站,能够分享技术的人,乐于帮助别人的人。
虽然大部分代码能够在网上找到,但里面也有一丁点自己的思想,也算是自己对代码业的一丁点贡献吧。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InteropWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using Microsoft.Office.Interop.Word;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Word2Png w = new Word2Png();
w.SaveToImages("d:\\");
}
}
class Word2Png
{
Application wordApp = null;
private InteropWord.Document document = null;
public void SaveToImages(string directory)
{
this.wordApp = new Application();
this.document = this.wordApp.Documents.Open("c:\\2.docx"); InteropWord.Windows windows = this.document.Windows;
int windowCount = windows.Count;
for (var i = 1; i <= windowCount; i++)
{
InteropWord.Window win = windows[i];
InteropWord.View windowsView = win.View; // Pages can only be retrieved in print layout view.
windowsView.Type = InteropWord.WdViewType.wdPrintView; InteropWord.Panes panes = win.Panes;
int paneCount = panes.Count;
for (var j = 1; j <= paneCount; j++)
{
InteropWord.Pane pane = panes[j];
var pages = pane.Pages;
var pageCount = pages.Count;
for (var k = 1; k <= pageCount; )
{
InteropWord.Page p = null; try
{
p = pages[k];
}
catch
{
// pages[k] sometimes throws exception: 'System.Runtime.InteropServices.COMException: The requested member of the collection does not exist'.
// This is a workaround for this issue.
continue;
} var bits = p.EnhMetaFileBits;
var target =directory+ string.Format(@"\{0}_image.doc", k);
using (var ms = new MemoryStream((byte[])(bits)))
{
var image = System.Drawing.Image.FromStream(ms);
var imageTarget = Path.ChangeExtension(target, "png");
image.Save(imageTarget, ImageFormat.Png);
} Marshal.ReleaseComObject(p);
p = null; k++;
} Marshal.ReleaseComObject(pages);
pages = null; Marshal.ReleaseComObject(windowsView);
windowsView = null; Marshal.ReleaseComObject(pane);
pane = null;
} Marshal.ReleaseComObject(panes);
panes = null; Marshal.ReleaseComObject(win);
win = null;
} Marshal.ReleaseComObject(windows);
windows = null;
} public void Close()
{
if (this.document != null)
{
((InteropWord._Document)this.document).Close(); Marshal.ReleaseComObject(this.document);
this.document = null; }
}
}
}
感谢每一位阅读此篇文章的人,希望可以帮到你。
c#实现Word转换PNG图片的更多相关文章
- C#将Word转换成PDF方法总结(基于Office和WPS两种方案)
有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...
- WPF 将PPT,Word转成图片
在Office下,PowerPoint可以直接把每张幻灯片转成图片,而Word不能直接保存图片.所以只能通过先转换成xps文件,然后再转成图片. 一.PPT 保存为图片 /// <summary ...
- VSTO中Word转换Range为Image的方法
VSTO中Word转换Range为Image的方法 前言 VSTO是一套用于创建自定义Office应用程序的Visual Studio工具包,通过Interop提供的增强Office对象,可以对Wor ...
- 如何将word中的图片和文字导入自己的博客中
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- word转pdf图片问题
经过整理总结出两类问题:1,pdf文件下载文档中某些图片显示红叉. 问题现象:pdf是通过word转换成,发现源文件doc和docx文档均出现上述问题:只是某些图片显示红叉.通过这两点确定和文 ...
- 怎样将word中的图片插入到CSDN博客中
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- 批量将网页转换成图片或PDF文档技巧分享
工作中我们有时要将一些批量的网页转换成图片或者PDF文档格式,尽管多数浏览器具有滚动截屏或者打印输出PDF文档功能.可是假设有几十上百张网页须要处理,那也是要人命的.所以我一直想找一款可以批量处理该工 ...
- word如何选择图片粘贴
自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...
- ckeditor粘贴word文档图片的思路
由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...
随机推荐
- CSS中伪类选择器及伪元素
1.伪类选择器 在CSS中,最常用的伪类选择器是使用在a(锚)元素上的几种选择器,它们的使用方法如下: a:link{color:#FF0000;text-decoration:none} a:vis ...
- More is better(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...
- SSM框架原理,作用及使用方法
---恢复内容开始--- 尊重原创:http://m.blog.csdn.net/dennis_wu_/article/details/73437097 作用: SSM框架是spring MVC ,s ...
- HTTP协议----->连接管理
1. TCP连接 1.1 TCP为HTTP提供了一条可靠的比特传输管道. TCP(Transmission Control Protocol)----传输控制协议,是主机对主机层的传输控制协议,提 ...
- php网站在服务器上邮件发送不了,在本地可以
标签: php邮箱 2015-11-27 13:58 879人阅读 评论(0) 收藏 举报 分类: php(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在做phpmailer发送邮 ...
- Centos系统下Lamp环境的快速搭建(超详细)
lamp的搭建对于初学者是一件很头疼的事情,所以借此机会把自己当初快速搭建linux+apche+mysql+php的方法分享大家希望能到你. 工具/原料 虚拟机及Centos操作系统 Linux基本 ...
- Java数据持久层框架 MyBatis之API学习五(Mapper XML 文件)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- spring data jpa 学习笔记
springboot 集成 springData Jpa 1.在pom.xml添加依赖 <!-- SpringData-Jpa依赖--> <dependency <groupI ...
- win7 mysql 数据库轻松实现数据库定时备份
本地备份: 第一步: 安装一个mysql. 第二步: 在命令行中配置mysql 打开环境变量将mysql 安装路径配置到path中 第三部: cmd 中输入:mysqldump -uroot -p12 ...
- python3 第六章 - 条件判断
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 条件语句的执行过程,如下图: 条件语句,又称为if语句,它的完整语法如下: if 条件1: 语句块1 ...