.net 下word 中的图片与文字分离

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

 

c#实现word中的图文分离

 

part 1: class define

 

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();
}
}
for (int i =0; 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();
}
}
for (int i =0; 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();
}
}
for (int i =0; 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:

 

WordSeparator w =new WordSeparator();
w.SeparateImageText();
 
 
 
Shape 对象代表文档中的图形对象,
InlineShape 代表文档中的嵌入式图形对象,
但是我又遇到了另外一种问题,word 中的图片除了InlineShape  的图片外还有  Shape 
 如果将 Shape  转化为  InlineShape  就会报错
 对于shape 还是没法做到完全分离,希望大神指点一二
 
posted @   zhao69222  阅读(571)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2012-11-05 IE下设置网页为 首页,收藏
点击右上角即可分享
微信分享提示