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这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...
随机推荐
- Hadoop(十五)MapReduce程序实例
一.统计好友对数(去重) 1.1.数据准备 joe, jon joe , kia joe, bob joe ,ali kia, joe kia ,jim kia, dee dee ,kia dee, ...
- zoj 3494:BCD Code
Description Binary-coded decimal (BCD) is an encoding for decimal numbers in which each digit is rep ...
- 【新版】Android技术博客精华汇总
[新版]Android技术博客精华汇总(原文链接内持续更新) http://www.apkbus.com/thread-313856-1-1.html Kotlin Kotlin学习资料汇总 http ...
- 将项目(代码)从GitHub上克隆(下载)到本地仓库
要将项目从GitHub上克隆到本地,首先你得下载并安装好git for window. 下载地址:http://www.xp510.com/xiazai/Application/other/30988 ...
- 一篇文章让你深透理解cookie和session,附带分布式WEB系统redis共享session方案
cookie和session有什么区别?这是一个很基础的知识点,大家可能都知道一个大概:cookie是存在客户端的,session是存储在服务端,cookie和session用来验证识别用户的登录状态 ...
- h5学习笔记:vuethink 配置
vuethink 是一款基于PHP TP5和Vuejs 结合的后台框架,设计起来是使用较为前沿.在使用的过程,需要对这款开源的后台做一些调整和面对一些细节的坑.前段时间也因为有项目需求,所以下载了玩了 ...
- Cookies的实际存储位置
检查下注册表中: HKEY_CRURRENT_USER\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVESION\EXPLORER\USER SHELL FOLDERSCoo ...
- Asp.net mvc 中Action 方法的执行(一)
[toc] 在 Aps.net mvc 应用中对请求的处理最终都是转换为对某个 Controller 中的某个 Action 方法的调用,因此,要对一个请求进行处理,第一步,需要根据请求解析出对应的 ...
- PL/SQL FAQ in installation "make sure you have the 32 bits Oracle client installed" and "Database character set(AL32UTF8) and Client character set (GBK) are different"
requirement : connecting to remote oracle server . now I know the connectionURL :connectionUrl :jdb ...
- JS-输入金额校验
function clearNoNum(obj){ obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字&qu ...