從 word 文檔中刪除形狀
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
我們已經(jīng)演示了如何使用 Spire.Doc 從代碼中向 word 文檔添加形狀。Spire.Doc 還支持通過索引刪除單個形狀或清除 word 文檔中的所有形狀。本文將說明如何在 C# 和 VB.NET 中從 word 文檔中刪除形狀。
  
 
第 1 步:初始化 Document 類的新實例并從文件加載文檔。
Document doc = new Document();
doc.LoadFromFile("Shapes.docx",FileFormat.Docx2010);
第 2 步:從文檔中獲取第一節(jié)和從該節(jié)中獲取第一段。
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#]
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.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace RemoveShape
Class Program
Private 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 Sub
End Class
End Namespace
以上便是如何在 C# 和 VB.NET 中從 word 文檔中刪除形狀,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群(767755948)

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線