Thursday, 26 April 2012 06:44

Remove Word Section in C#, VB.NET

In Word document, users can add new sections or remove existing sections. This guide demonstrates how to remove Word section in C# and VB.NET.

Section, important part to form a Word document, can be easily added and removed. In MS Word, users can operation section by section break, adding new section by inserting a break and deleting existing section by removing section break. In this guide, a solution will be presented to remove Word section in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET enables developers to access SectionCollection property of Document class to get specified section in Word document and then remove the unnecessary one or more. There are two methods to remove Word section.

Remove one Specified Section

Invoke Remove(Spire.Doc.Interface.IDocumentObject entity) method to remove specified object of document. In this solution, entity is the first section of document which has gotten at first.

[C#]
            Section section = document.Sections[0];
            document.Sections.Remove(section);
[VB.NET]
            Dim section As Section = document.Sections(0)
            document.Sections.Remove(section)

Invoke RemoveAt(int index) method to remove document object at the specified index from the collection.

[C#]
            document.Sections.RemoveAt(0);
[VB.NET]
	    document.Sections.RemoveAt(0)

Remove All Sections

Invoke Clear() method to clear all objects of document.

[C#]
            document.Sections.Clear();
[VB.NET]
            document.Sections.Clear()

Note: Because a Word document must include at least one section, so the document cannot be saved if all the sections are removed.

Spire.Doc, the professional stand-alone component to manipulate MS Word document without automation, enables developers to generate, read, write, modify Word document on their .NET, WPF and Silverlight application.

Published in Page Setup