文檔金喜正規買球>>Aspose中文文檔>>從 NPOI 中的 Word 文檔中提取圖像
從 NPOI 中的 Word 文檔中提取圖像
Aspose.Words是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
Aspose.Words
要從文檔中提取所有圖像或具有特定類型的圖像,請按照下列步驟操作:
-
使用 Document.GetChildNodes 方法選擇所有 Shape 節點。
- 迭代結果節點集合。
- 檢查 Shape.HasImage 布爾屬性。
- 使用Shape.ImageData屬性提取圖像數據 。
- 將圖像數據保存到文件中。
Document wordDocument = new Document("Extract Images from Word Document.doc"); NodeCollection pictures = wordDocument.GetChildNodes(NodeType.Shape, true); int imageindex = 0; foreach (Shape shape in pictures) { if (shape.HasImage) { string imageFileName = "data/Aspose_" + (imageindex++).ToString() + "_" + shape.Name; shape.ImageData.Save(imageFileName); } }
點擊復制
NPOI
XWPFDocument doc = new XWPFDocument(new FileStream("data/Extract Images from Word Document.doc",FileMode.Open)); IList<XWPFPictureData> pics = doc.AllPictures; foreach (XWPFPictureData pic in pics) { FileStream outputStream = new FileStream("data/NPOI_" + pic.FileName,FileMode.OpenOrCreate); byte[] picData= pic.Data; outputStream.Write(picData, 0, picData.Length); outputStream.Close(); }
點擊復制
下載運行代碼
下載示例代碼