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实现转换成图片的实现 word实现转换成图片的实现](http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___7486ZH00SIGG.gif)
/// 【先转换成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); } }