最近在做一个项目要求word 中的图片与文字分离 ,找了好久终于找到一个完美的方法

c#实现word中的图文分离

 

part 1: class define



Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->publicclass WordSeparator:IDisposable
{

#region Constructor

public WordSeparator()

{

WordApp =new Microsoft.Office.Interop.Word.Application();

}

#endregion

#region Fields

private Microsoft.Office.Interop.Word.Application WordApp;

privateobject missing = System.Reflection.Missing.Value;

privateobject yes =true;

privateobject no =false;

private Microsoft.Office.Interop.Word.Document d;

privateobject filename =@"C:\example.rtf";

#endregion

#region Methods

publicvoid UpdateDoc()

{

d = WordApp.Documents.Open(ref filename, ref missing, ref no, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing);

List<Microsoft.Office.Interop.Word.Range> ranges =

new List<Microsoft.Office.Interop.Word.Range>();

foreach (Microsoft.Office.Interop.Word.InlineShape s in d.InlineShapes)

{

if (s.Type ==

Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)

{

ranges.Add(s.Range);

s.Delete();

}

}

foreach (Microsoft.Office.Interop.Word.Range r in ranges)

{

r.InlineShapes.AddPicture(

@"c:\PathToNewImage\Image.jpg", ref missing, ref missing, ref missing);

}

WordApp.Quit(ref yes, ref missing, ref missing);

}

publicvoid SeparateImageText()

{

//初始化程序

d = WordApp.Documents.Open(ref filename, ref missing, ref no, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing);

List<Microsoft.Office.Interop.Word.Range> ranges =

new List<Microsoft.Office.Interop.Word.Range>();

List<string> files =new List<string>();

foreach (Microsoft.Office.Interop.Word.InlineShape s in d.InlineShapes)

{

if (s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture

|| s.Type ==

Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeEmbeddedOLEObject)

{

//获取图片数据

byte[] imgData = (byte[])s.Range.EnhMetaFileBits;

string file =string.Concat(Guid.NewGuid().ToString(), ".gif");

files.Add(file);

//构造图形

MemoryStream mStream =new MemoryStream(imgData);

Bitmap bmp =new Bitmap(mStream);

//保存到磁盘

bmp.Save(file);

mStream.Dispose();

bmp.Dispose();

ranges.Add(s.Range);

s.Delete();

}

}

; i < ranges.Count; i ++ )

{

Microsoft.Office.Interop.Word.Range r = ranges[i];

//替换图片

r.InsertBefore("<img src='"+ files[i] +"'>");

r.InsertAfter("</img>");

}

//退出程序

WordApp.Quit(ref yes, ref missing, ref missing);

}

///<summary>

/// 替换word中的图片

///</summary>

///<param name="serverPath">图片文件的存储物理路径</param>

///<param name="virtualPath">图片文件的标签虚拟路径</param>

publicvoid SeparateImageText(string serverPath, string virtualPath)

{

//初始化程序

d = WordApp.Documents.Open(ref filename, ref missing, ref no, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing);

List<Microsoft.Office.Interop.Word.Range> ranges =new List<Microsoft.Office.Interop.Word.Range>();

List<string> files =new List<string>();

foreach (Microsoft.Office.Interop.Word.InlineShape s in d.InlineShapes)

{

if (s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture

|| s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeEmbeddedOLEObject)

{

//获取图片数据

byte[] imgData = (byte[])s.Range.EnhMetaFileBits;

string file =string.Concat(Guid.NewGuid().ToString(), ".gif");

files.Add(file);

//构造图形

MemoryStream mStream =new MemoryStream(imgData);

Bitmap bmp =new Bitmap(mStream);

//保存到磁盘

bmp.Save(string.Concat(serverPath, "\\", file));

mStream.Dispose();

bmp.Dispose();

ranges.Add(s.Range);

s.Delete();

}

}

; i < ranges.Count; i++)

{

Microsoft.Office.Interop.Word.Range r = ranges[i];

//替换图片

r.InsertBefore("<img src='"+string.Concat(virtualPath,"//",files[i]) +"'>");

r.InsertAfter("</img>");

}

//退出程序

WordApp.Quit(ref yes, ref missing, ref missing);

}

///<summary>

/// 替换word中的图片

///</summary>

///<param name="targetFile">目标文件</param>

///<param name="serverPath">图片文件的存储物理路径</param>

///<param name="virtualPath">图片文件的标签虚拟路径</param>

publicvoid SeparateImageText(string targetFile,string serverPath, string virtualPath)

{

filename = targetFile;

//初始化程序

d = WordApp.Documents.Open(ref filename, ref missing, ref no, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing);

List<Microsoft.Office.Interop.Word.Range> ranges =new List<Microsoft.Office.Interop.Word.Range>();

List<string> files =new List<string>();

foreach (Microsoft.Office.Interop.Word.InlineShape s in d.InlineShapes)

{

if (s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture

|| s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeEmbeddedOLEObject)

{

//获取图片数据

byte[] imgData = (byte[])s.Range.EnhMetaFileBits;

string file =string.Concat(Guid.NewGuid().ToString(), ".gif");

files.Add(file);

//构造图形

MemoryStream mStream =new MemoryStream(imgData);

Bitmap bmp =new Bitmap(mStream);

//保存到磁盘

bmp.Save(string.Concat(serverPath, "\\", file));

mStream.Dispose();

bmp.Dispose();

ranges.Add(s.Range);

s.Delete();

}

}

; i < ranges.Count; i++)

{

Microsoft.Office.Interop.Word.Range r = ranges[i];

//替换图片

r.InsertBefore("<img src='"+string.Concat(virtualPath, "//", files[i]) +"'>");

r.InsertAfter("</img>");

}

//退出程序

WordApp.Quit(ref yes, ref missing, ref missing);

}

#endregion

#region IDisposable 成员

publicvoid Dispose()

{

if (d !=null)

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(d);

d =null;

}

if (WordApp !=null)

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);

WordApp =null;

}

}

#endregion

}

part 2: usage code:



Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->WordSeparator w =new WordSeparator();

w.SeparateImageText();

 
 
 
Shape 对象代表文档中的图形对象,
InlineShape 代表文档中的嵌入式图形对象,
但是我又遇到了另外一种问题,word 中的图片除了InlineShape  的图片外还有  Shape 
 如果将 Shape  转化为  InlineShape  就会报错
 对于shape 还是没法做到完全分离,希望大神指点一二
 

.net 下word 中的图片与文字分离的更多相关文章

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

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

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

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

  3. 在RichTextBox控件中添加图片和文字

    public void SetText(RichTextBox rtb) { rtb.Text = "在RichTextBox控件中添加图片和文字" + Environment.N ...

  4. 如何把word中的图片怎么导出来呢?

    在办公使用word的过程中你可能经常会遇到这个问题:插入到word中的图片找不到导出来的方法,是不是很郁闷呢,别急,今天咱们研究一下把word中的图片导出来的方法(把"我的"变成你 ...

  5. Java 添加、删除、格式化Word中的图片

    本文介绍使用Spire.Cloud.SDK for Java提供的ImagesApi接口来操作Word中的图片.具体可通过addImage()方法添加图片.deleteImage()方法删除图片.up ...

  6. iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片

    Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...

  7. 写带有清晰图片的博客:如何将word中的图片复制到windows live writer保持大小不变--清晰度不变

    写blog的习惯,先在word写了,复制到windows live writer,再发布到博客园.word中的文章,图片有缩放比例,复制到windows live writer后图片变得不清晰.除了一 ...

  8. word中更改图片和标题之间的垂直距离

    word中插入图片后.往往须要给图片加上标题. 你插入图片和给图片插入标题时,word用的是默认的格式给你插入的图片和标题. 假如原来的paragraph是2倍行距.你的图片和标题之间的距离也是2倍行 ...

  9. 利用POI抽取word中的图片并保存在文件中

    利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...

随机推荐

  1. 笨办法学Python(二十六)

    习题 26: 恭喜你,现在可以考试了! 你已经差不多完成这本书的前半部分了,不过后半部分才是更有趣的.你将学到逻辑,并通过条件判断实现有用的功能. 在你继续学习之前,你有一道试题要做.这道试题很难,因 ...

  2. POJ-2395 Out of Hay---MST最大边

    题目链接: https://vjudge.net/problem/POJ-2395 题目大意: 求MST中的最大边,和POJ-2495类似 思路: 模板直接过 #include<iostream ...

  3. 使用shc加密bash脚本程序

    摘要以前写看到别人写的脚本用shc加密的,我也有就了解了下. SHC代表shell script compiler,即shell脚本编译器.通过SHC编译过的脚本程序对普通用户而言是不读的,因此如果你 ...

  4. 在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>

    <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'> Time Elapsed: 0:00:21.3163 ...

  5. python_2_变量的使用2

    ''' 多行注释(三个单引号,或者双引号) gf_of_oldboy="Chen rong hua"#变量的表示办法1,用下划线(老男孩的女朋友) GfOfOldboy=" ...

  6. Search in Rotated Sorted Array——LeetCode

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. ABP学习 解决:Update-Database : 无法将“Update-Database”项识别为 cmdlet、函数、脚本文件或可运行程序的名称的问题

    原因: 没有引用EntityFramework命令 解决: 在程序包管理器控制台执行如下命令:Import-Module 项目路径\packages\EntityFramework.6.1.3(EF版 ...

  8. pooling

    转自:http://www.gageet.com/2014/09182.php 本文部分参考了:http://www.zhihu.com/question/23437871 卷积层是对图像的一个邻域进 ...

  9. 一篇SSM框架整合友好的文章(一)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/53730333 本文出自方志朋的博客 最近实在太忙,之前写的<rxjava系列文章&g ...

  10. 在xampp修改密码

    1.选择 服务器--账号--修改密码 2.在密码 一栏输入新密码 3.刷新页面会得到如下页面 此时,该页面提醒我们检查配置文件中的主机.用户名和密码 4.打开配置文件 路径为 xampp -> ...