博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
word实现转换成图片的实现
阅读量:7125 次
发布时间:2019-06-28

本文共 3718 字,大约阅读时间需要 12 分钟。

 using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using WORD = Microsoft.Office.Interop.Word;
using System.IO;
using System.Windows.Forms;
using System.Drawing;  

/// <summary>

    /// WORD文档进行图片转化。会用到Office的内置来实现 word实现转换成图片的实现

    /// 【先转换成htm文件,在生成JPG图片。】

    /// </summary>
    public class ConvertWORD
    {
        <summary>
        /// 图片保存路径。System.IO.Path.GetTempPath() + @"cache\{0}.jpg"
        /// </summary>
        private static string SAVEWORDJPG = @"E:\cheng_Text\FormatApplication\测试文件\{0}.bmp";

        /// <summary>

        /// 暂时网页保存文件路径。Path.GetTempPath() + @"cache\{0}.htm";
        /// </summary>
        private static string TEMPHTMLPATH = @"E:\cheng_Text\FormatApplication\测试文件\{0}.htm";

        /// <summary>

        /// WORD文档转换为Bmp图片,
        /// </summary>
        /// <param name="wordPath">Word文件路径</param>
        /// <returns>图片的路径</returns>
        public static string WORDConvertImage(string wordPath)
        {
            WORD.Application App = null;
            object newObj = Type.Missing;
            try
            {

                App = new Microsoft.Office.Interop.Word.Application();

                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;
                object saveHTML = string.Format(TEMPHTMLPATH, Guid.NewGuid().ToString());
                object filePath = wordPath as object;
                object wordOnly = true;
                TEMPHTMLPATH = saveHTML.ToString();
                WORD.Document wordDoc = App.Documents.Open(ref filePath, ref newObj, ref wordOnly, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj, ref newObj);
                App.Visible = false;
                wordDoc.SaveAs(ref saveHTML, ref saveFormat, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj, ref  newObj);
                App.Documents.Close(ref newObj, ref newObj, ref newObj);

                return ConverHTML(saveHTML.ToString());

            }
            catch (Exception WORDEx)
            {
                MessageBox.Show(WORDEx.ToString());
                return null;
            }
            finally
            {
                App.Quit(ref newObj, ref newObj, ref newObj);
            }
        }

        /// <summary>

        /// 通过WebBrowser控件来实现从HTML到Bmp图片的生成。
        /// </summary>
        /// <param name="htmPath">HTML路径</param>
        /// <returns>Bmp图片路径</returns>
        private static string ConverHTML(string htmPath)
        {
            string ImagePath = string.Empty;
            WebBrowser web = new WebBrowser();
            web.Navigate(htmPath);
            web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);

            //DeleteTempFile(TEMPHTMLPATH);

            return ImagePath;
        }

        static void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {
            WebBrowser web = sender as WebBrowser;
            if (web.ReadyState == WebBrowserReadyState.Complete)
            {
                Rectangle rect = web.Document.Body.ScrollRectangle;
                web.Width = rect.Width + 100;
                web.Height = rect.Height + 50;
                while (web.ReadyState != WebBrowserReadyState.Complete)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
                Bitmap bitmap = new Bitmap(web.Width, web.Height);
                web.DrawToBitmap(bitmap, new Rectangle(0, 0, web.Width, web.Height));

                web.Dispose();

                SAVEWORDJPG = string.Format(@"E:\cheng_Text\FormatApplication\测试文件\{0}.bmp", Guid.NewGuid().ToString());
                bitmap.Save(SAVEWORDJPG, System.Drawing.Imaging.ImageFormat.Bmp);
                bitmap.Dispose();
                DeleteTempFile(TEMPHTMLPATH);
            }
        }

        /// <summary>

        /// 删除指定的临时文件。
        /// </summary>
        /// <param name="filePath"></param>
        private static void DeleteTempFile(string filePath)
        {
            //删除临时HTML文件 。
            File.Delete(filePath);
            //删除目录文件内容。
            string dirPath = filePath.Split('.')[0] + ".files";
            foreach (string f in Directory.GetFileSystemEntries(dirPath))
            {
                // 如果是文件存在 
                if (File.Exists(f))
                {
                    FileInfo fi = new FileInfo(f);
                    if (fi.Attributes.ToString().IndexOf("Readonly") != 1)
                    {
                        fi.Attributes = FileAttributes.Normal;
                    }
                    // 直接删除其中的文件 
                    File.Delete(f);
                }
            }
            Directory.Delete(dirPath);
        }
    }

转载地址:http://fjhel.baihongyu.com/

你可能感兴趣的文章
linux 查看进程占用内存
查看>>
数据库系统原理中的基本概念
查看>>
我的友情链接
查看>>
Windows sever 2008 R2 ---虚拟机安装
查看>>
PC 加入AD域的要求
查看>>
B-tree vs B+tree
查看>>
Eclipse中10个最有用的快捷键组合
查看>>
LAMP一键安装脚本
查看>>
vsphere层级架构
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
转载-Linux新人必读,Linux发行版选择和软件安装的一些原则性问题
查看>>
Linux从入门到精通之监控软件Cacti
查看>>
徹底解決 Windows Server 2012 R2 惱人的輸入法問題
查看>>
linux 手动整理内存
查看>>
Android 向右滑动销毁(finish)Activity, 随着手势的滑动而滑动的效果
查看>>
Vue.js 自定义指令
查看>>
打开Nginx的rewrite日志
查看>>
[李景山php]每天laravel-20161121|StatusCommand.php
查看>>
通过Rancher部署并扩容Kubernetes集群基础篇二
查看>>