Insert Textbox in Word with C#, VB.NET

Word Textbox, a container to save texts or images, can be moved and its size can be adjusted. Users can insert multiple textboxes and set text direction for different functions, for example, to form as quote or sidebar. Solution in this guide demonstrates how to insert and format a Word textbox with texts in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET, specializing in manipulating Word document for .NET, provides the method: Paragraph.AppendTextBox to enable users to insert textbox in specified paragraph. The overloads passed to this method are float width and float height to set textbox size. Also, it provides a TextBox class to allow users to format textbox by setting its properties, such as Format, Body etc.

In this example, the Word textbox with text is inserted a new added paragraph. And the textbox, including text in it, is well formatted. The following screenshot presents the result after inserting textbox in Word.

Insert Word TextBox

Firstly, download and install Spire.Doc for .NET and follow the steps to insert Word textbox.

Step 1: Declare a new TextBox instance and assign the result after invoking Paragraph.AppendTextBox as value for this instance.

Step 2: Set Format properties, including HorizontalAlignment, LineColor, LineStyle and FillColor. Also, if users would like to insert textbox in an existing paragraph, it is better to set TextWrappingStyle properties of format to have a good location and avoid covering the original contents.

Step 3: Declare a new Paragraph instance and assign result of invoking TextBox.Body.AddParagraph() method as its value. Invoke Paragraph.AppendText method to add text in textbox. The overload passed to this method is string text. Then, assign the result as value for a TextRange instance. Next, set CharacterFormat properties for this TextRange instance to set text format, including FontName, FontSize and TextColor.

Here comes to the full code:

using System.Drawing;
static void Main(string[] args)
  //load the existing Word document
  Document document = new Document();
  document.LoadFromFile(@"..\Microsoft Word 2013 Preview.docx");

  //insert and format textbox
  TextBox TB =document.Sections[0].AddParagraph().AppendTextBox(200, 30);
  TB.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left;
  TB.Format.LineColor = Color.DarkBlue;
  TB.Format.LineStyle = TextBoxLineStyle.Simple;
  TB.Format.FillColor = Color.MediumPurple;

  //add and format text in textbox
  Paragraph para = TB.Body.AddParagraph();
  TextRange TR=para.AppendText("Textbox in Word Document");
  TR.CharacterFormat.FontName = "Century Gothic";
  TR.CharacterFormat.FontSize = 20;
  TR.CharacterFormat.TextColor = Color.White;
  para.Format.HorizontalAlignment = HorizontalAlignment.Center;

  //save and launch the file
  document.SaveToFile("TextBox.docx", FileFormat.Docx);

 Shared Sub Main(ByVal args() As String)
  'load the existing Word document
  Dim document As New Document()
  document.LoadFromFile("..\Microsoft Word 2013 Preview.docx")

  'insert and format textbox
  Dim TB As TextBox = document.Sections(0).AddParagraph().AppendTextBox(200, 30)
  TB.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left
  TB.Format.LineColor = Color.DarkBlue
  TB.Format.LineStyle = TextBoxLineStyle.Simple
   TB.Format.FillColor = Color.MediumPurple

  'add and format text in textbox
  Dim para As Paragraph = TB.Body.AddParagraph()
  Dim TR As TextRange = para.AppendText("Textbox in Word Document")
  TR.CharacterFormat.FontName = "Century Gothic"
  TR.CharacterFormat.FontSize = 20
  TR.CharacterFormat.TextColor = Color.White
  para.Format.HorizontalAlignment = HorizontalAlignment.Center

  'save and launch the file
  document.SaveToFile("TextBox.docx", FileFormat.Docx)
 End Sub

Spire.Doc, professional Word component, is specially designed for developers to fast generate, write, modify and save Word documents in .NET, Silverlight and WPF with C# and VB.NET. Also, it supports conversion between Word and other popular formats, such as PDF, HTML, Image, Text and so on, in .NET and WPF platform.