原創(chuàng)|使用教程|編輯:王香|2017-08-07 16:27:40.000|閱讀 554 次
概述:Spire.Doc 是一個MS Word 組件,使用戶可以直接執(zhí)行各種Word文檔處理任務(wù),本文介紹了如何從C#和VB.NET的word文檔中刪除形狀。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc還支持通過索引刪除單個形狀,或清除word文檔中的所有形狀,本文將介紹如何從C#和VB.NET中的word文檔中刪除形狀。
具有形狀的示例單詞文檔:
Step 1:初始化Document類的新實例,并從文件加載文檔。
Document doc = new Document(); doc.LoadFromFile("Shapes.docx",FileFormat.Docx2010);
Step 2:從文檔中獲取第一個部分,并從該部分獲取第一個段落。
Section section = doc.Sections[0]; Paragraph para = section.Paragraphs[0]
Step 3:從第一段獲取形狀。
ShapeObject shape = para.ChildObjects[0] as ShapeObject;
Step 4:去除形狀或所有形狀。
////clear all the shapes. //para.ChildObjects.Clear();
Step 5:將文檔保存到文件。
doc.SaveToFile("Removeshape.docx",FileFormat.Docx2010);
從Word文檔中刪除一個形狀后的效果截圖:
完整代碼:
[C#]
Document doc = new Document(); doc.LoadFromFile("Shapes.docx",FileFormat.Docx2010); Section section = doc.Sections[0]; Paragraph para = section.Paragraphs[0]; ShapeObject shape = para.ChildObjects[0] as ShapeObject; //remove the third shape. para.ChildObjects.RemoveAt(2); ////clear all the shapes. //para.ChildObjects.Clear(); doc.SaveToFile("Removeshape.docx",FileFormat.Docx2010);
[VB.NET]
Dim doc As New Document() doc.LoadFromFile("Shapes.docx", FileFormat.Docx2010) Dim section As Section = doc.Sections(0) Dim para As Paragraph = section.Paragraphs(0) Dim shape As ShapeObject = TryCast(para.ChildObjects(0), ShapeObject) 'remove the third shape. para.ChildObjects.RemoveAt(2) '''/clear all the shapes. 'para.ChildObjects.Clear(); doc.SaveToFile("Removeshape.docx", FileFormat.Docx2010)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn