using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using MSWord = Microsoft.Office.Interop.Word;

namespace ConvertEquationToPng
{
    public partial class MainForm : Form
    {
        private MSWord.Application m_word;
        private MSWord.Document m_doc;

public MainForm()
        {
            InitializeComponent();
        }

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var processes = Process.GetProcessesByName("winword");
                foreach (var process in processes)
                {
                    process.Kill();
                }
            }
            catch (Exception)
            {

}
            m_word = new MSWord.Application();
            object filefullname =Application.StartupPath+@"\\845AD2AE-23C0-4AC4-AFE7-F832BD088673_ONLY_QUESTION.doc";
            var obj = Type.Missing;
            try
            {
                m_word.Documents.Open(ref filefullname,ref obj, ref obj, ref obj,ref obj, ref obj, ref obj,ref obj, ref obj,ref obj, ref obj, ref obj, ref obj,ref obj, ref obj, ref obj);
                m_word.Visible = false;

var document = m_word.Documents[1];

var count = 0;
                foreach (var c in document.Fields)
                {
                    count++;
                    var d = (MSWord.Field) c;
                    d.Copy();
                    d.Select();
                    m_word.Selection.PasteSpecial(Link: false, DisplayAsIcon: false,DataType: MSWord.WdPasteDataType.wdPasteEnhancedMetafile,Placement:MSWord.WdOLEPlacement.wdInLine);
                }
                var picPath =new DirectoryInfo("c:\\Pic");
                if (!picPath.Exists)
                {
                    picPath.Create();
                }
                var i = 0;
                for (var j=1;j<= document.InlineShapes.Count;j++)
                {
                    var ish = document.InlineShapes[j];

if (ish.Type == MSWord.WdInlineShapeType.wdInlineShapePicture)
                    {
                        ish.Select();
                        m_word.Selection.Copy();
                        var image = Clipboard.GetImage();
                        if (image != null)
                        {
                            var bitmap = new Bitmap(image);
                            var fi = new FileInfo(picPath +"\\"+ i + ".jpg");
                            var fiNew= new FileInfo(picPath + "\\"+ i + "_new.jpg");
                            bitmap.Save(fi.FullName);
                            Trim(fi.FullName, fiNew.FullName);
                            var img = Image.FromFile(fiNew.FullName);
                            Clipboard.SetDataObject(img);
                            ish.Delete();
                            m_word.Selection.Paste();
                        }
                        i++;
                    }
                }
                
                #region 关闭
                //避免弹出normal.dotm被使用的对话框,自动保存模板  
                m_word.NormalTemplate.Saved = true;

document.SaveAs("c:\\result.doc");
                //先关闭打开的文档(注意saveChanges选项)  
                Object saveChanges = MSWord.WdSaveOptions.wdDoNotSaveChanges;
                Object originalFormat = Type.Missing;
                Object routeDocument = Type.Missing;
                m_word.Documents.Close(ref saveChanges, ref originalFormat, ref routeDocument);

//若已经没有文档存在,则关闭应用程序  
                if (m_word.Documents.Count == 0)
                {
                    m_word.Quit(Type.Missing, Type.Missing, Type.Missing);
                }
                var r=MessageBox.Show("成功完成,请查看效果!","恭喜",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                if (r == DialogResult.OK)
                {
                    var fileFullName = "c:\\result.doc";
                    var psi = new ProcessStartInfo("Explorer.exe")
                    {
                        Arguments = "/e,/select," + fileFullName
                    };
                    Process.Start(psi);
                }

#endregion
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("打开Word文档出错:"+ex);
            }
        }

public static void Trim(string sourceFileName, string targetFileName)
        {
            CommonUtil.ExecCommand(@"C:\Program Files\ImageMagick-6.9.3-Q16\convert.exe", sourceFileName + " -transparent white -trim " + targetFileName);
        }
    }
}

处理eq问题的更多相关文章

  1. <c:if test="value ne, eq, lt, gt,...."> 用法

    类别 运算符 算术运算符 + . - . * . / (或 div )和 % (或 mod ) 关系运算符 == (或 eq ). != (或 ne ). < (或 lt ). > (或 ...

  2. jQuery中eq()方法用法实例

    本文实例讲述了jQuery中eq()方法用法.分享给大家供大家参考.具体分析如下: 此方法能够获取匹配元素集上的相应位置索引的元素. 匹配元素集上元素的位置索引是从0开始的. 语法结构: 复制代码 代 ...

  3. jqueyr eq get用法

    相信大部份人都会把这2个的用法搞错.仔细查看下API文档就可以知道.eq返回的是一个jquery对象,get返回的是一个html 对象数组.举个例子: <p style="color: ...

  4. JQ first-child与:first的区别以及nth-child(index)与eq(index)的区别

    1.first-child和:first区别 first-child  是指选取每个父元素的第一个子元素 如$("div:first-child")指每个父级里的第一个div孩子 ...

  5. jQuery.eq() 函数

    eq() 函数 获取当前对象中指定索引所对应的的元素 语法 $selector.eq(index)//index为指定索引,值为数字型 返回值 返回值为一个对象 实例说明 代码 <!DOCTYP ...

  6. jQuery的eq方法

    定义和用法eq() 方法将匹配元素集缩减值指定 index 上的一个. 语法.eq(index) 其中的index :整数,指示元素的位置(最小为 0).如果是负数,则从集合中的最后一个元素往回计数. ...

  7. jQuery原型方法first,last,eq,slice源码分析

    这4个方法中前3个方法很常用大家都见过,但是slice方法可能会以为是数组方法,其实slice也是jQuery的一个原型方法,只不过是底层方法是为其他方法服务的(更具体点是为eq方法服务的),首先还是 ...

  8. yourphp的eq作用

    <eq name="> <span style="color:#090">是</span> <else /> 否 < ...

  9. jQuery : eq() vs get()

    .get(index) and .eq(index) both return a single "element" from a jQuery object array, but ...

  10. JQUERY添加、删除元素、eq()方法;

    一.jQuery - 添加元素 1.append() - 在被选元素内部的结尾插入指定内容 2.prepend() - 在被选元素内部的开头插入指定内容 3.after() - 在被选元素之后插入内容 ...

随机推荐

  1. CodeForces 778D Parquet Re-laying 构造

    题意: 有两个\(n \times m\)的矩阵\(A,B\),都是由\(1 \times 2\)的砖块铺成,代表初始状态和结束状态 有一种操作可以把两个砖块拼成的\(2 \times 2\)的矩形旋 ...

  2. 算法:枚举法---kotlin

    枚举法:效率低,循环所有的情况,找到正确答案 用于解决数学问题,还是很简单的. 比如,奥数里面: 算 法 描 述 题X题=题题题题题题 其中 算法描述题每一个为一个数字,请写出正确的数字. ok,我们 ...

  3. Installation error: INSTALL_FAILED_CANCELLED_BY_USER

    我的手机本来是支持Androidstadio 调试手机的,我手机小米的,后来,系统升级了,我也没在意,第二天上班,已运行就报错: Installation error: INSTALL_FAILED_ ...

  4. luogu2221 [HAOI2012]高速公路

    和sdoi的相关分析很像qwq,推柿子然后线段树搞搞 #include <iostream> #include <cstdio> using namespace std; ty ...

  5. USACO Section2.3 Controlling Companies 解题报告 【icedream61】

    concom解题报告------------------------------------------------------------------------------------------ ...

  6. python学习笔记十六:读取JSON文件

    读取JSON文件可以用JSON库,示例代码: #coding:utf-8 import json with open("msg.json") as jsonfile: json_d ...

  7. 使用node构建一个自己的服务器

    我们做本地服务器,经常会选择Apache.IIS或者Tomcat,当然这些最方便的算是Apache,几乎不需要配置,最多就是配置下端口,亦或者我们想不用localhost,改成其他也是可以的,只要去更 ...

  8. centos6 install cobbler

    cobbler 安装   一:定义yum源 wget -c -O CentOS-Base.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo ...

  9. 2 26requests.py

    """ requests """ # import requests # reponse = requests.get("http ...

  10. Comparable和Comparator的学习笔记

    目录 Comparable和Comparator的实现 Comparable接口 Comparator接口 总结 参考自 今天在项目开发中,遇到要对List中的对象按照对象某一属性进行排序的问题,我发 ...