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

本文介绍如何使用Spire.Doc库从Word文档中删除指定形状或全部形状的方法,包括初始化文档、获取段落及形状对象等步骤,并提供C#与VB.NET的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spire.Doc还支持通过索引删除单个形状,或清除word文档中的所有形状,本文将介绍如何从C#和VB.NET中的word文档中删除形状。

具有形状的示例单词文档:

图片1

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文档中删除一个形状后的效果截图:

图片2

完整代码:

[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)

慧都控件网

转载于:https://my.oschina.net/u/3006003/blog/1504309

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值