您的位置:首页 > 编程语言 > VB

Spire.Doc 教程:从C#和VB.NET的word文档中删除形状

2017-08-08 00:00 561 查看
摘要: Spire.Doc 是一个MS Word 组件,使用户可以直接执行各种Word文档处理任务,本文介绍了如何从C#和VB.NET的word文档中删除形状。

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)

慧都控件网
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐