由于项目需要,经过一些大神的指导以及github,stackOverflow找资料,写了个这么个程序。
主要是因为word文档有特殊字体,特殊字体处理就要用到EnhMetaFileBits,即获取一页内容的增强图元信息。类型属于 EmfPlusDual。
在此,特别感谢github,stackOverflow,csdn这三个网站,能够分享技术的人,乐于帮助别人的人。
虽然大部分代码能够在网上找到,但里面也有一丁点自己的思想,也算是自己对代码业的一丁点贡献吧。
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using InteropWord = Microsoft.Office.Interop.Word; using System.IO; using System.Runtime.InteropServices; using System.Drawing.Imaging; using Microsoft.Office.Interop.Word; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Word2Png w = new Word2Png(); w.SaveToImages("d:\\"); } } class Word2Png { Application wordApp = null; private InteropWord.Document document = null; public void SaveToImages(string directory) { this.wordApp = new Application(); this.document = this.wordApp.Documents.Open("c:\\2.docx"); InteropWord.Windows windows = this.document.Windows; int windowCount = windows.Count; for (var i = 1; i <= windowCount; i++) { InteropWord.Window win = windows[i]; InteropWord.View windowsView = win.View; // Pages can only be retrieved in print layout view. windowsView.Type = InteropWord.WdViewType.wdPrintView; InteropWord.Panes panes = win.Panes; int paneCount = panes.Count; for (var j = 1; j <= paneCount; j++) { InteropWord.Pane pane = panes[j]; var pages = pane.Pages; var pageCount = pages.Count; for (var k = 1; k <= pageCount; ) { InteropWord.Page p = null; try { p = pages[k]; } catch { // pages[k] sometimes throws exception: ‘System.Runtime.InteropServices.COMException: The requested member of the collection does not exist‘. // This is a workaround for this issue. continue; } var bits = p.EnhMetaFileBits; var target =directory+ string.Format(@"\{0}_image.doc", k); using (var ms = new MemoryStream((byte[])(bits))) { var image = System.Drawing.Image.FromStream(ms); var imageTarget = Path.ChangeExtension(target, "png"); image.Save(imageTarget, ImageFormat.Png); } Marshal.ReleaseComObject(p); p = null; k++; } Marshal.ReleaseComObject(pages); pages = null; Marshal.ReleaseComObject(windowsView); windowsView = null; Marshal.ReleaseComObject(pane); pane = null; } Marshal.ReleaseComObject(panes); panes = null; Marshal.ReleaseComObject(win); win = null; } Marshal.ReleaseComObject(windows); windows = null; } public void Close() { if (this.document != null) { ((InteropWord._Document)this.document).Close(); Marshal.ReleaseComObject(this.document); this.document = null; } } } }
声明:转载请注明出处,有问题联系:[email protected]
时间: 2024-10-13 12:39:28