由於項目需要,經過一些大神的指導以及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;
}
}
}
}
感謝每一位閱讀此篇文章的人,希望可以幫到你。
