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)