原創(chuàng)|行業(yè)資訊|編輯:郝浩|2018-08-06 15:05:11.000|閱讀 3476 次
概述:圖片是Word文檔的基本要素之一,常見(jiàn)的對(duì)Word圖片的操作有插入、刪除、替換和提取。本文將介紹如何使通過(guò)編程的方式添加圖片到指定位置,以及如何獲取Word文檔中的圖片并保存到本地路徑。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
圖片是Word文檔的基本要素之一,常見(jiàn)的對(duì)Word圖片的操作有插入、刪除、替換和提取。本文將介紹如何使通過(guò)編程的方式添加圖片到指定位置,以及如何獲取Word文檔中的圖片并保存到本地路徑。
//實(shí)例化一個(gè)Document對(duì)象 Document doc = new Document(); //添加section和段落 Section section = doc.AddSection(); Paragraph para = section.AddParagraph(); //加載圖片到System.Drawing.Image對(duì)象, 使用AppendPicture方法將圖片插入到段落 Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png"); DocPicture picture = doc.Sections[0].Paragraphs[0].AppendPicture(image); //設(shè)置文字環(huán)繞方式 picture.TextWrappingStyle = TextWrappingStyle.Square; //指定圖片位置 picture.HorizontalPosition = 50.0f; picture.VerticalPosition = 50.0f; //設(shè)置圖片大小 picture.Width = 100; picture.Height = 100; //保存到文檔 doc.SaveToFile("Image.doc", FileFormat.Doc);
//初始化一個(gè)Document實(shí)例并加載Word文檔 Document doc = new Document(); doc.LoadFromFile(@"Image.doc"); int index = 0; //遍歷Word文檔中每一個(gè)section foreach (Section section in doc.Sections) { //遍歷section中的每個(gè)段落 foreach (Paragraph paragraph in section.Paragraphs) { //遍歷段落中的每個(gè)DocumentObject foreach (DocumentObject docObject in paragraph.ChildObjects) { //判斷DocumentObject是否為圖片 if (docObject.DocumentObjectType == DocumentObjectType.Picture) { //保存圖片到指定路徑并設(shè)置圖片格式 DocPicture picture = docObject as DocPicture; String imageName = String.Format(@"images\Image-{0}.png", index); picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png); index++; } } } }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn