zoukankan      html  css  js  c++  java
  • .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: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();
     
     
     
    Shape 对象代表文档中的图形对象,
    InlineShape 代表文档中的嵌入式图形对象,
    但是我又遇到了另外一种问题,word 中的图片除了InlineShape  的图片外还有  Shape 
     如果将 Shape  转化为  InlineShape  就会报错
     对于shape 还是没法做到完全分离,希望大神指点一二
     
    学习永无止境
  • 相关阅读:
    最近在项目中使用ibatis小结
    35 岁前程序员要规划好的四件事
    C# webbrowser小结
    高并发网站架构
    高斯混合模型(GMM)
    EM算法学习(Expectation Maximization Algorithm)
    如何用CSS3美化菜单
    Intellij IDEA配置自动同步到FTP服务器
    Mac 快速休眠关机重启锁屏
    JavaScript并行运算新机遇——Web Workers的神奇魔法
  • 原文地址:https://www.cnblogs.com/6922276zfj/p/4076861.html
Copyright © 2011-2022 走看看