How to remove Text Box from word document in C#

2015-05-28 08:39:44 Written by  support iceblue
Rate this item
(0 votes)

Text Box has been widely used in word documents to present additional information to readers. As a powerful and easy to use .NET word component, Spire.Doc has powerful function to work with text box. We have already showed you how to insert text box to word document and extract Text from Text Boxes in Word document. In this article let's see how to remove text box from word document in C#.

Spire.Doc supports to remove the particular text box or clear all the text boxes from the word documents. We will show you the details as below. Firstly, check the original word document contains three text boxes on it.

How to remove Text Box from word document in C#

Here comes to the steps of how to remove the text box from word document by using the class of TextBoxes.

Step 1: Create a new document and load from file.

Document Doc = new Document();
Doc.LoadFromFile("Sample.docx");

Step 2: Remove the first text box by using the class of TextBoxes.

Doc.TextBoxes.RemoveAt(0);

Step 3: Save and launch the file.

Doc.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");

Effective screenshot:

How to remove Text Box from word document in C#

If you want to clear all the text boxes at one time, the following code snippet will fulfill it.

Screenshot after clear all the text boxes:

Doc.TextBoxes.Clear();

How to remove Text Box from word document in C#

Full codes:

namespace Removetextbox
{
    class Program
    {
        static void Main(string[] args)
        {
            Document Doc = new Document();
            Doc.LoadFromFile("Sample.docx");
            
            Doc.TextBoxes.RemoveAt(0);
            //Doc.TextBoxes.Clear();

            Doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");

        }
    }
}

Additional Info

  • tutorial_title: Remove Text Box from word document
Last modified on Friday, 03 September 2021 04:00