Word控件Spire.Doc 【图像形状】教程(9): 如何在 C# 和 VB.NET 中从 word 文档中删除形状

如何在 C# 和 VB.NET 中从 word 文档中删除形状

第 1 步初始化 Document 类的新实例并从文件加载文档。

Document doc = new Document();doc.LoadFromFile("Shapes.docx",FileFormat.Docx2010);

第 2 步从文档中获取第一节和从该节中获取第一段。

Section section = doc.Sections[0];Paragraph para = section.Paragraphs[0];

第 3 步从第一段中获取形状。

ShapeObject shape = para.ChildObjects[0] as ShapeObject;

第 4 步删除形状或所有形状。

//remove the third shape.para.ChildObjects.RemoveAt(2);////clear all the shapes.//para.ChildObjects.Clear();

第 5 步将文档保存到文件中。

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

从word文档中删除一个形状后的有效截图:

如何在 C# 和 VB.NET 中从 word 文档中删除形状

完整代码

[C#]

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace RemoveShape{class Program{static void Main(string[] args){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]
Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace RemoveShapeClass ProgramPrivate Shared Sub Main(args As String())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)End SubEnd ClassEnd Namespace

以上便是如何在 C# 和 VB.NET 中从 word 文档中删除形状,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年9月18日
下一篇 2022年9月18日

相关推荐

发表回复

登录后才能评论