最近遇到一個項目就是要從一份word中提取出所有的圖片信息,功能看起來不是很難,只要使用office自帶的Microsoft.Office.Interop.Word就可以解決問題。網上也有不少的文章來說明如何去實現。不過總體來說網上的內容分為兩派一個是使用剪貼板來實現,一個是通過將圖片轉為byte數組來完成。個人傾向於后者,但是在實踐過程中遇到了問題。
問題一:通過byte的方式來實現圖片的提取會導致提取出來的圖片的質量嚴重下降,這樣的圖片質量下降是無法通過修改圖片質量的代碼來優化和提高的。目前為止我還沒有想到什么辦法來很好的解決,只能是換方法來實現, 不過個人猜想是因為word中圖片的dpi問題導致,因為原本很小的圖片導出后就會變的很大(尺寸)。所以不得不使用剪貼板的方法來實現,但是用第一種方法也會有局限性。
下面分別的貼出實現的代碼
第一種,通過byte的方式,關鍵語句為(byte[])shape.Range.EnhMetaFileBits;
foreach(InlineShape shape in item.Range.InlineShapes)
{
if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
{
//獲取Word中的圖片
byte[] img = (byte[])shape.Range.EnhMetaFileBits;
Bitmap bmp = new Bitmap(new MemoryStream(img));
}
}
{
if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
{
//獲取Word中的圖片
byte[] img = (byte[])shape.Range.EnhMetaFileBits;
Bitmap bmp = new Bitmap(new MemoryStream(img));
}
}
第二種,通過剪貼板,如下
foreach (InlineShape shape in item.Range.InlineShapes)
{
//判斷類型
if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
{
//利用剪貼板保存數據
shape.Select(); //選定當前圖片
WordApp.Selection.Copy();//copy當前圖片
if (Clipboard.ContainsImage())
{
Bitmap bmp = new Bitmap(Clipboard.GetImage());
fileName = System.Guid.NewGuid() + defaultPicExtension;
bmp.Save(savePath + fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
{
//判斷類型
if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
{
//利用剪貼板保存數據
shape.Select(); //選定當前圖片
WordApp.Selection.Copy();//copy當前圖片
if (Clipboard.ContainsImage())
{
Bitmap bmp = new Bitmap(Clipboard.GetImage());
fileName = System.Guid.NewGuid() + defaultPicExtension;
bmp.Save(savePath + fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
問題二:通過控制台的方式編寫程序可能會遇到剪貼板無法使用的事情,需要引用System.Window.Form來解決問題。
希望有經驗的朋友可以幫忙解釋一下第一種方法的問題所在,最后貼上實現的全部代碼
private void bt_readreport_Click(object sender, EventArgs e)
{
//初始化控件值
ClearControl();
StringBuilder reportContent = new StringBuilder();
object Nothing = System.Reflection.Missing.Value;
object filename = "文件完整路徑和名稱";
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//循環文章中的各個章節
foreach (Paragraph item in WordDoc.Paragraphs)
{
if (item != null)
{
if (item.Range.Text.Trim() != "")
{
//判斷該范圍內是否存在圖片
if (item.Range.InlineShapes.Count != 0)
{
foreach (InlineShape shape in item.Range.InlineShapes)
{
//判斷類型
if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
{
//利用剪貼板保存數據
shape.Select(); //選定當前圖片
WordApp.Selection.Copy();//copy當前圖片
string fileName = "";
if (Clipboard.ContainsImage())
{
Bitmap bmp = new Bitmap(Clipboard.GetImage());
fileName = System.Guid.NewGuid() + ".png";
bmp.Save(savePath + fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}
//在總目錄中添加相應信息
reportContent.AppendLine(item.Range.Text.Trim());
}
}
}
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
{
//初始化控件值
ClearControl();
StringBuilder reportContent = new StringBuilder();
object Nothing = System.Reflection.Missing.Value;
object filename = "文件完整路徑和名稱";
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//循環文章中的各個章節
foreach (Paragraph item in WordDoc.Paragraphs)
{
if (item != null)
{
if (item.Range.Text.Trim() != "")
{
//判斷該范圍內是否存在圖片
if (item.Range.InlineShapes.Count != 0)
{
foreach (InlineShape shape in item.Range.InlineShapes)
{
//判斷類型
if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
{
//利用剪貼板保存數據
shape.Select(); //選定當前圖片
WordApp.Selection.Copy();//copy當前圖片
string fileName = "";
if (Clipboard.ContainsImage())
{
Bitmap bmp = new Bitmap(Clipboard.GetImage());
fileName = System.Guid.NewGuid() + ".png";
bmp.Save(savePath + fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}
//在總目錄中添加相應信息
reportContent.AppendLine(item.Range.Text.Trim());
}
}
}
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
對了,Png的效果要比JPG好,而且文件也不大,推薦使用