由于项目需要,经过一些大神的指导以及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图片的更多相关文章

  1. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  2. WPF 将PPT,Word转成图片

    在Office下,PowerPoint可以直接把每张幻灯片转成图片,而Word不能直接保存图片.所以只能通过先转换成xps文件,然后再转成图片. 一.PPT 保存为图片 /// <summary ...

  3. VSTO中Word转换Range为Image的方法

    VSTO中Word转换Range为Image的方法 前言 VSTO是一套用于创建自定义Office应用程序的Visual Studio工具包,通过Interop提供的增强Office对象,可以对Wor ...

  4. 如何将word中的图片和文字导入自己的博客中

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  5. word转pdf图片问题

    经过整理总结出两类问题:1,pdf文件下载文档中某些图片显示红叉.     问题现象:pdf是通过word转换成,发现源文件doc和docx文档均出现上述问题:只是某些图片显示红叉.通过这两点确定和文 ...

  6. 怎样将word中的图片插入到CSDN博客中

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  7. 批量将网页转换成图片或PDF文档技巧分享

    工作中我们有时要将一些批量的网页转换成图片或者PDF文档格式,尽管多数浏览器具有滚动截屏或者打印输出PDF文档功能.可是假设有几十上百张网页须要处理,那也是要人命的.所以我一直想找一款可以批量处理该工 ...

  8. word如何选择图片粘贴

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  9. ckeditor粘贴word文档图片的思路

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

随机推荐

  1. 学习web前端技术的笔记,仅供自己查阅备忘,移动对font-size的控制(并非原创)

    假设根字体font-size的值是40px, 640/40=16,16就是px换算rem的值 function initHtmlFontSize(){ //获取可可视屏幕的宽度 var _width= ...

  2. TSP(个人模版)

    O(n^2)TSP: #include<stdio.h> #include<string.h> #include<algorithm> #include<io ...

  3. Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】

    J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...

  4. hdu_2669 Romantic(扩展欧几里得)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 Romantic Time Limit: 2000/1000 MS (Java/Others)  ...

  5. HDU 2119 Matrix

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2119 解题思路: 处理数据,使用公式最小点覆盖数=最大匹配数,使用匈牙利算法求二分图最大匹配即可. ...

  6. 主题:Windows系统服务器磁盘挂载

    可能很多人发现VPS重装之后D盘.E盘不见了,其实并没有不见只是磁盘未挂载,下面由小编来为大家讲讲如何挂载磁盘 远程登录服务器后,桌面上只有一个回收站的,我们在桌面空白处右键属性-->桌面--& ...

  7. Redis 数据结构与内存管理策略(下)

    Redis 数据结构与内存管理策略(下) 标签: Redis Redis数据结构 Redis内存管理策略 Redis数据类型 Redis类型映射 Redis 数据类型特点与使用场景 String.Li ...

  8. MYSQL主从库同步配置过程

    MYSQL主从库同步配置过程 为了实现网站数据库的异地备份,采用了MySQL数据库主从同步配置,需要两台服务器分别作为主从库,当主库发生增删改等操作,会实时反映到从库,我的个人服务器配置如下: 主库为 ...

  9. ROM、SDRAM、RAM、DRAM、SRAM、FLASH区别

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  10. maven项目使用jacoco插件检测代码覆盖率详细配置

    使用maven构建项目(java项目或者web项目都可以) jacoco插件的配置参考官方网址:http://www.eclemma.org/jacoco/trunk/doc/maven.html ( ...