C#/VB.NET: Insert or Remove a Text Box in Word

A text box is a movable, resizable container for storing text or images. In Word documents, you can insert text boxes as sidebars or to bring focus to some important text, such as headings or quotes. Occasionally, you may also need to delete some mistakenly added text boxes. In this article, you will learn how to programmatically insert or remove a text box in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Insert a Text Box in a Word Document

Spire.Doc for .NET provides the Paragraph.AppendTextBox(float width, float height) method to insert a text box in a specified paragraph. After the text box is inserted, Spire.Doc for .NET also provides the TextBox class for users to format the text box by setting its properties, such as Format, Body etc. The detailed steps are as follows.

  • Create a Document instance, and then load a sample Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Sections[] property, and then add a paragraph to the section using Section.AddParagraph() method.
  • Add a text box to the paragraph using Paragraph.AppendTextBox(float width, float height) method.
  • Get the format of the text box using TextBox.Format property, and then set the text box's wrapping type, position, border color and fill color using the properties of TextBoxFormat Class.
  • Add a paragraph to the text box using TextBox.Body.AddParagraph() method, and then set its alignment.
  • Insert an image to the paragraph using Paragraph.AppendPicture() method, and then set the size of the inserted image.
  • Insert text to the text box using Paragraph.AppendText() method, and then set the text font.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertTextbox
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document
            document.LoadFromFile("Ralph.docx");

            //Insert a textbox and set its wrapping style
            TextBox TB = document.Sections[0].AddParagraph().AppendTextBox(130, 320);
            TB.Format.TextWrappingStyle = TextWrappingStyle.Square;

            //Set the position of the textbox
            TB.Format.HorizontalOrigin = HorizontalOrigin.RightMarginArea;
            TB.Format.HorizontalPosition = -100;
            TB.Format.VerticalOrigin = VerticalOrigin.Page;
            TB.Format.VerticalPosition = 130f;

            //Set the border style and fill color of the textbox
            TB.Format.LineColor = Color.DarkBlue;
            TB.Format.FillColor = Color.LightCyan;

            //Insert an image to textbox as a paragraph
            Paragraph para = TB.Body.AddParagraph();
            DocPicture picture = para.AppendPicture(@"C:\Users\Administrator\Desktop\Ralph.jpg");

            //Set alignment for the paragraph
            para.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //Set the size of the inserted image
            picture.Height = 90;
            picture.Width = 90;


            //Insert text to textbox as the second paragraph
            TextRange TR = para.AppendText("Emerson is truly the center of the American transcendental movement, "
                + "setting out most of its ideas and values in a little book, Nature, published in 1836, "
                + "that represented at least ten years of intense study in philosophy, religion, and literature.");

            //Set alignment for the paragraph
            para.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //Set the font of the text
            TR.CharacterFormat.FontName = "Times New Roman";
            TR.CharacterFormat.FontSize = 12;
            TR.CharacterFormat.Italic = true;

            //Save the result file
            document.SaveToFile("AddTextBox.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Insert or Remove a Text Box in Word

Remove a Text Box from a Word Document

Spire.Doc for .NET provides the Document.TextBoxes.RemoveAt(int index) method to delete a specified text box. If you want to delete all text boxes from a Word document, you can use the Document.TextBoxes.Clear() method. The below example shows how to remove the first text box from a Word document.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Remove the first text box using Document.TextBoxes.RemoveAt(int index) method.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace Removetextbox
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document Doc = new Document();

            //Load a sample Word document 
            Doc.LoadFromFile("TextBox.docx");

            //Remove the first text box
            Doc.TextBoxes.RemoveAt(0);

            //Remove all text boxes
            //Doc.TextBoxes.Clear();

            //Save the result document
            Doc.SaveToFile("RemoveTextbox.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Insert or Remove a Text Box in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.