.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();

}

}

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:

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->WordSeparator w =new WordSeparator();

w.SeparateImageText();

-

转自:http://www.csharpwin.com/csharpspace/11474r5980.shtml

Shape 对象代表文档中的图形对象,

InlineShape 代表文档中的嵌入式图形对象,

但是我又遇到了另外一种问题,word 中的图片除了InlineShape  的图片外还有  Shape

如果将 Shape  转化为  InlineShape  就会报错

对于shape 还是没法做到完全分离,希望大神指点一二

时间: 2024-08-11 19:03:13

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

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

目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写博客需要在第三方博客平台注册帐号,且需要第三方博客平台提供API接口.目前的有的博客平台均已关闭博客接口,所以无法使用Word来发布博客. 2.发布到博客或公众号平台的图片无法转载.由于所有博客平台,公众号平台(如微信)开启了图片防盗链功能,作者发布到这些平台上的图片则无法转载到其它的网站中,这限制

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

word中插入图片后,往往需要给图片加上标题. 你插入图片和给图片插入标题时,word用的是默认的格式给你插入的图片和标题. 假如原来的paragraph是2倍行距,你的图片和标题之间的距离也是2倍行距,这样显的你的图片和标题之间的距离特别大. 插入完成后,你想调节图片和标题的距离,就不可能了.因为行距是前一行和后一行直接的距离,但是图片并没有行距. 解决方法如下: 1.删除你的图片和图片标题. 2. 在你插入图片的上一行之下插入两个回车 3. 选中你插入的回车,邮件点击"段落",把行

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

写blog的习惯,先在word写了,复制到windows live writer,再发布到博客园.word中的文章,图片有缩放比例,复制到windows live writer后图片变得不清晰.除了一张一张 设置图片缩放比例为100%外,再复制到windows live writer.这样做太麻烦.就百度了下面的解决办法. word中图片小,复制出来的就不清楚,你需要设置图片大小为原始大小,复制到windows live writer图片才可能清楚. 还需要设置Windows live writ

Java 添加、替换、删除Word中的图片

Java 添加.替换.删除Word中的图片 文档中,可以通过图文混排的方式来增加内容的可读性,相比纯文本文档,在内容展现方式上也更具美观性.在给文档添加图片时,可设置图片的文本环绕方式.旋转角度.图片高度/宽度等:另外,也可对文档中已有的图片实现替换.删除等操作.本文将从在Word文档中添加.替换.删除图片等三个操作需求来介绍.具体代码可参考以下示例. 工具使用:Free Spire.Doc for Java(免费版) [示例1]添加图片到Word import com.spire.doc.*;

Word中插入图片,嵌入式,图片显示不能全部显示出来。

解决办法:原来,是由于图片所在的段落属性的行距设置成了“固定值”,设置不要为固定值即可. 1.选中你要插入图片的上下文,单击“右键”,选择“段落”修改行距,比如为修改“1.5倍行距””(实际上,只要不是“固定值”均可),然后确认. 2.再插入图片即可OK. 3.或者,有的图片已经插入到word中了.那就直接选中已经插入文中的图片,单击“右键”,选择“段落”修改行距,比如为修改“1.5倍行距”(实际上,只要不是“固定值”均可),然后确认就可以了.

解决在IE下label中IMG图片无法选中radio的几个方法

今天遇到一个问题,在IE浏览器下,使用label FOR radio后,label中的图片无法选中.例如,这样的代码 <input type="radio" name="radio" id="a" /><label for="a"><img src="/Labs/images/logo.gif" /></label> ,这种情况下,点击label的图片将无法选

使用POI替换word中的特定字符/文字改进版

package com.xfzx.test.POI.main; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import jav

在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 }

在PHP中给图片添加文字水印

<?php if (function_exists('imagepng')) { dir('GD库不存在'); } //图片路径 $imagePath = './img/a.jpg'; //获取文件类型 $imageInfo = getimagesize($imagePath); $imageExtension = image_type_to_extension($imageInfo[2], false); //获取图片 $func = 'imagecreatefrom' . $imageExt