Insert Textbox with Contents in Word by Using C#/VB.NET

Textbox in Word is one kind of graphic container, which can be moved and edited. Through textbox, users can insert some additional information in a document, such as quotation, sidebar and so on. Generally speaking, contents in textbox are independent of other document bod, which means that users can set specified format for textbox, for example, vertical text direction.

In this post, I want to introduce a method to insert textbox in Word by using C#/VB.NET. Also,, the .NET Word component, Spire.Doc for .NET, is used to meet my requirement more easily.

In my example, I will insert a simple textbox in Word document. And I will set line style and color for it. Finally, add some contents in it.

Detailed Step Shown as Following:

  1. Create a Word document. Then, add section and paragraph.
  2. Set paragraph style, including font type, color and line spacing.
  3. Append cue words to tell readers what I want to do and apply style for this paragraph.
  4. Insert textbox. Firstly, add a paragraph and use paragraph.AppendTextBox method to insert textbox. There are two parameters passed to this method, textbox width and height. Secondly, set textbox line color, width and style.
  5. Add text in textbox. Add a paragraph in textbox. Append text in this paragraph and set format for text.
  6. Save and launch file.

Main Coding

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace InsertTextBox
{
    class TextBox
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();

            //Add Section and Paragraph
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Style
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = “Style”;
            style.CharacterFormat.Font = new Font(“Century Gothic”, 14F);
            style.CharacterFormat.TextColor = Color.DarkCyan;
            style.ParagraphFormat.LineSpacing = 2F * 12F;
            style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
            document.Styles.Add(style);

            //Append Text and Apply Style
            paragraph.AppendText(“This Example Demonstrates How to Insert TextBox in Word”);
            paragraph.ApplyStyle(style.Name);

            //Insert TextBox
            Paragraph paragraph1 = section.AddParagraph();
            Spire.Doc.Fields.TextBox textBox = paragraph1.AppendTextBox(200, 100);
            textBox.Format.LineColor = Color.DeepSkyBlue;
            textBox.Format.LineWidth = 3.5F;
            textBox.Format.LineStyle = TextBoxLineStyle.Double;

            //Add Text in TextBox
            Paragraph paragraph2 = textBox.Body.AddParagraph();
            TextRange txtRange = paragraph2.AppendText(“TextBox”);
            txtRange.CharacterFormat.Font = new Font(“Calibri”, 12F);
            txtRange.CharacterFormat.TextColor = Color.DarkBlue;

            //Save and Launch
            document.SaveToFile(“TextBox.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“TextBox.docx”);
        }
    }
}

VB.NET

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace InsertTextBox
    Friend Class TextBox
        Shared Sub Main(ByVal args() As String)
            ‘Create Document
            Dim document As New Document()

            ‘Add Section and Paragraph
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()

            ‘Style
            Dim style As New ParagraphStyle(document)
            style.Name = “Style”
            style.CharacterFormat.Font = New Font(“Century Gothic”, 14.0F)
            style.CharacterFormat.TextColor = Color.DarkCyan
            style.ParagraphFormat.LineSpacing = 2.0F * 12.0F
            style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple
            document.Styles.Add(style)

            ‘Append Text and Apply Style
            paragraph.AppendText(“This Example Demonstrates How to Insert TextBox in Word”)
            paragraph.ApplyStyle(style.Name)

            ‘Insert TextBox
            Dim paragraph1 As Paragraph = section.AddParagraph()
            Dim textBox As Spire.Doc.Fields.TextBox = paragraph1.AppendTextBox(200, 100)
            textBox.Format.LineColor = Color.DeepSkyBlue
            textBox.Format.LineWidth = 3.5F
            textBox.Format.LineStyle = TextBoxLineStyle.Double

            ‘Add Text in TextBox
            Dim paragraph2 As Paragraph = textBox.Body.AddParagraph()
            Dim txtRange As TextRange = paragraph2.AppendText(“TextBox”)
            txtRange.CharacterFormat.Font = New Font(“Calibri”, 12.0F)
            txtRange.CharacterFormat.TextColor = Color.DarkBlue

            ‘Save and Launch
            document.SaveToFile(“TextBox.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“TextBox.docx”)
        End Sub
    End Class
End Namespace

Result Shown as Following:

Download Spire.Doc for .NET Here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s