Spire.Doc 教程:從C#和VB.NET的word文檔中刪除形狀

Spire.Doc還支持經過索引刪除單個形狀,或清除word文檔中的全部形狀,本文將介紹如何從C#和VB.NET中的word文檔中刪除形狀。html

具備形狀的示例單詞文檔:orm

圖片1

Step 1:初始化Document類的新實例,並從文件加載文檔。htm

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:去除形狀或全部形狀。rem

////clear all the shapes.
//para.ChildObjects.Clear();

Step 5:將文檔保存到文件。文檔

doc.SaveToFile("Removeshape.docx",FileFormat.Docx2010);

從Word文檔中刪除一個形狀後的效果截圖:get

圖片2

完整代碼:io

[C#]ast

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)

慧都控件網

相關文章
相關標籤/搜索