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)

慧都控件网

相关文章
相关标签/搜索