最近在做一个项目要求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:PathToNewImageImage.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
}
{
#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:PathToNewImageImage.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();
w.SeparateImageText();
Shape 对象代表文档中的图形对象,
InlineShape 代表文档中的嵌入式图形对象,
但是我又遇到了另外一种问题,word 中的图片除了InlineShape 的图片外还有 Shape
如果将 Shape 转化为 InlineShape 就会报错
对于shape 还是没法做到完全分离,希望大神指点一二