.net 下word 中的图片与文字分离
最近在做一个项目要求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();
.net 下word 中的图片与文字分离的更多相关文章
- 如何将word中的图片和文字导入自己的博客中
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- 怎样将word中的图片插入到CSDN博客中
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- 在RichTextBox控件中添加图片和文字
public void SetText(RichTextBox rtb) { rtb.Text = "在RichTextBox控件中添加图片和文字" + Environment.N ...
- 如何把word中的图片怎么导出来呢?
在办公使用word的过程中你可能经常会遇到这个问题:插入到word中的图片找不到导出来的方法,是不是很郁闷呢,别急,今天咱们研究一下把word中的图片导出来的方法(把"我的"变成你 ...
- Java 添加、删除、格式化Word中的图片
本文介绍使用Spire.Cloud.SDK for Java提供的ImagesApi接口来操作Word中的图片.具体可通过addImage()方法添加图片.deleteImage()方法删除图片.up ...
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...
- 写带有清晰图片的博客:如何将word中的图片复制到windows live writer保持大小不变--清晰度不变
写blog的习惯,先在word写了,复制到windows live writer,再发布到博客园.word中的文章,图片有缩放比例,复制到windows live writer后图片变得不清晰.除了一 ...
- word中更改图片和标题之间的垂直距离
word中插入图片后.往往须要给图片加上标题. 你插入图片和给图片插入标题时,word用的是默认的格式给你插入的图片和标题. 假如原来的paragraph是2倍行距.你的图片和标题之间的距离也是2倍行 ...
- 利用POI抽取word中的图片并保存在文件中
利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...
随机推荐
- 如何利用PHP语言压缩图片?PHP入门教程
PHP可以控制缩略图清晰度和缩略图之后产生音量的产生.下面我们就来看看如何使用PHP优化我们的压缩图像. PHP应用程序的开发往往涉及生成缩略图,使用PHP生成缩略图的过程本身并不难,但你知道PHP ...
- SAP CRM和C4C数据同步的两种方式概述:SAP PI和HCI
SAP Cloud for Customer(C4C)和SAP其他传统产品进行数据同步的方式,如下图所示,可以使用SAP Netweaver Process Integration或者SAP HANA ...
- framework7对日历的一些效果处理
现在的要求是日历中要区分已打卡和未打卡的显示,并且当月只显示当月的日历状态,其他月份不显示状态,并且打卡的日期不能大于当日 实现代码(精确到天): HTML: <div class=" ...
- LA 2957 最大流,最短时间,输出路径
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=1 ...
- Java 执行系统命令工具类(commons-exec)
依赖jar <!-- 可以在JVM中可靠地执行外部进程的库. --> <dependency> <groupId>org.apache.commons</gr ...
- Shell编程学习之Shell编程基础(一)
这篇随笔将要介绍关于Shell编程的基本知识,这些将会在假设你已经熟悉了Linux系统和命令行的基本知识. 构建基本脚本 你应该了解或熟悉使用Shell命令行了,但是只是使用Shell命令行的命令,有 ...
- 五、@property的参数
格式:@property(参数1,参数2)类型 名字: 参数可有可无 如:@property int age; @property (nonatomic,retain) UIButton* btn; ...
- path、classpath理解
path.classpath最常见的场景:环境变量配置 path环境变量:设置path的作用是让操作系统可以找到JDK命令(指定了JDK命令搜索路径):path环境变量原来Windows里面就有,只需 ...
- System.Threading.Tasks
前言: 我们之前介绍了两种构建多线程软件的编程技术(使用异步委托或通过System.Threading的成员).这两个可以在任何版本的.NET平台工作. 关于System.Threading 的介绍 ...
- ReactiveObjC框架的简单介绍
最近在一直在学习RAC框架的Object-C版本ReactiveObjC(Swift版本为ReactiveSwift),这篇文章简单展示一下学习的成果!!!如果有什么地方理解错误,欢迎大家指正!!!互 ...