翻譯|使用教程|編輯:胡濤|2022-09-26 16:37:25.397|閱讀 165 次
概述:本指南中的解決方案演示了如何從現有 Word 文檔中提取圖像,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。在 C#、VB.NET 中從 Word 中提取圖像。
本指南中的解決方案演示了如何從現有 Word 文檔中提取圖像,并通過Spire.Doc for .NET將它們保存到 C# 和 VB.NET 中的指定路徑。
圖像是屬于段落項的一種文檔對象。Spire.Doc for .NET 提供了一個DocumentObject類來將圖像存儲在 Document 中。并且還提供了一個DocPicture類來獲取和設置文檔的圖像。下載并安裝 Spire.Doc for .NET。按照步驟從 Word 中提取圖像。
【C#】
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; namespace ExtractImage { class Program { static void Main(string[] args) { //Load document Document document = new Document(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx"); int index = 0; //Get Each Section of Document foreach (Section section in document.Sections) { //Get Each Paragraph of Section foreach (Paragraph paragraph in section.Paragraphs) { //Get Each Document Object of Paragraph Items foreach (DocumentObject docObject in paragraph.ChildObjects) { //If Type of Document Object is Picture, Extract. if (docObject.DocumentObjectType == DocumentObjectType.Picture) { DocPicture picture = docObject as DocPicture; //Name Image String imageName = String.Format(@"images\Image-{0}.png", index); //Save Image picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png); index++; } } } } } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace ExtractImage Class Program Private Shared Sub Main(args As String()) 'Load document Dim document As New Document("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx") Dim index As Integer = 0 'Get Each Section of Document For Each section As Section In document.Sections 'Get Each Paragraph of Section For Each paragraph As Paragraph In section.Paragraphs 'Get Each Document Object of Paragraph Items For Each docObject As DocumentObject In paragraph.ChildObjects 'If Type of Document Object is Picture, Extract. If docObject.DocumentObjectType = DocumentObjectType.Picture Then Dim picture As DocPicture = TryCast(docObject, DocPicture) 'Name Image Dim imageName As [String] = [String].Format("images\Image-{0}.png", index) 'Save Image picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png) index += 1 End If Next Next Next End Sub End Class End Namespace
調試后,所有提取的圖像都保存在指定路徑中。打開目錄,就會找到圖片。
Spire.Doc 是一個易于使用的 Word 文檔操作組件,允許開發人員在 C# 和 VB.NET for .NET 中快速生成、編寫、編輯和保存 Word(Word 97-2003、Word 2007、Word 2010), Silverlight 和 WPF。
以上便是如何在 Word 中插入圖像(C#/VB.NET),如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn