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);
        }
    }
}

最新文章

  1. list for循环中删除元素
  2. 每天一个percona 工具 --- pt-kill
  3. HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
  4. 5.7 NDK开发
  5. 提高D3js力导向图加载速度(转)
  6. 黄聪:VPS实现自动定时备份网站数据以及Mysql数据库到百度云同步盘
  7. python通过163邮箱发送邮件
  8. 全7 天玩转 ASP.NET MVC — 第 2 天
  9. 【Todo】Kerberos、OpenSSL、OAuth2.0等一系列验证权限安全相关学习
  10. ORA-12162: TNS:net service name is incorrectly specified
  11. POJ1860——Currency Exchange(BellmanFord算法求最短路)
  12. SAR-303 xml validator验证框架
  13. KB奇遇记(4):困难重重的选型
  14. Window下SVN服务器搭建以及客户端使用
  15. 阿里云 virtual memory exhausted: 无法分配内存
  16. Count:858org.apache.jasper.JasperException: Unable to compile class for JSP
  17. HTML5 拖放(Drag 和 Drop)详解与实例(转)
  18. [已解决]python FileNotFoundError: [WinError 3] for getsize(filepath)
  19. MyBatis源码解析(三)——Transaction事务模块
  20. Windows10 64位 Python2.7 Matplotlib安装

热门文章

  1. 10 class封装 ORM
  2. Android getLocationInWindow
  3. USACO Section1.3 Ski Course Design 解题报告
  4. pytorch下对简单的数据进行分类(classification)
  5. js万年历
  6. pyinstaller打包自己的python程序
  7. MVC学习笔记----@Helper标签(HelperMethod方法)和HtmlExtesion扩展
  8. Codeforces Round #364 (Div. 1) 700B(树)
  9. [bzoj] 3343 教主的魔法 || 带修改分块
  10. BZOJ 1043 【bzoj1043】[HAOI2008]下落的圆盘 | 暴力么??