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这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...
随机推荐
- deeplearning.ai 人工智能行业大师访谈 Pieter Abbeel 听课笔记
1. Pieter Abbeel小时候想做一个篮球运动员 - -!上学的时候喜欢数学和物理,上大学之后很自然的学工程.Pieter定义工程问题是"解决实际中应用物理和数学的问题". ...
- CTF---Web入门第二题 上传绕过
上传绕过分值:10 来源: Justatest 难度:易 参与人数:5847人 Get Flag:2272人 答题人数:2345人 解题通过率:97% bypass the upload 格式:fla ...
- phpcms模板生成原理
phpcms模板生成原理2009-09-25 16:14PHPCMS是功能强大的网站管理系统.以后有时间,俺要将其管理.使用.再开发等等都理顺出来.这一篇短文先介绍PHPCMS的模板机制. 所谓网站模 ...
- MyBatis工作原理
Mybatis工作原理: 我们的应用程序通过mybatis提供的api,增删改查方法来访问数据库,api底层调用了jdbc ,只不过mybatis对jdbc的封装是不完全封装,里面的sql语句需要我们 ...
- 使用setTimeout实现setInterval
setInterval = () =>{ console.log(1) //使用递归 setTimeout(setInterval,1000); }; setInterval()
- node中定时器, process.nextTick(), setImediate()的区别与联系
1.定时器 setTimeout()和setInterval()与浏览器中的API是一致的,定时器的问题在于,他并非精确的(在容忍范围内).尽管事件循环十分快,但是如果某一次循环占用的时间较多,那么下 ...
- Spark_总结七_troubleshooting
转载标明出处 http://www.cnblogs.com/haozhengfei/p/07ef4bda071b1519f404f26503fcba44.html Spark_总结七_troubles ...
- Nginx的启动(start),停止(stop)命令
http://blog.csdn.net/u010739551/article/details/51654859 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速 ...
- Uva 12171 Sculpture - 离散化 + floodfill
题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- VUE之ECMAScript6(es6)
es6:es:EMCAScript 6 (es2015)Emca:国际标准组织 1.常量和变量 const a = "hello" let:定义一个块级作用域的变量 需要先定义再使 ...