Word Textbox in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to insert a textbox into a document.

private void button1_Click(object sender, EventArgs e)
{
    //Create word document
    Document document = new Document();

    InsertTextbox(document.AddSection());

    //Save doc file.
    document.SaveToFile("Sample.doc",FileFormat.Doc);

    //Launching the MS Word file.
    WordDocViewer("Sample.doc");


}

private void InsertTextbox(Section section)
{
    Paragraph paragraph = section.AddParagraph();
    paragraph.AppendText("The sample demonstrates how to insert a textbox into a document.");
    paragraph.ApplyStyle(BuiltinStyle.Heading2);

    paragraph = section.AddParagraph();
    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
    Spire.Doc.Fields.TextBox textBox = paragraph.AppendTextBox(50,20);

}

private void WordDocViewer(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	'Create word document
	Dim document_Renamed As New Document()

	InsertTextbox(document_Renamed.AddSection())

	'Save doc file.
	document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)

	'Launching the MS Word file.
	WordDocViewer("Sample.doc")


End Sub

Private Sub InsertTextbox(ByVal section_Renamed As Section)
	Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
	paragraph_Renamed.AppendText("The sample demonstrates how to insert a textbox into a document.")
	paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)

	paragraph_Renamed = section_Renamed.AddParagraph()
	paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left
	Dim textBox As Spire.Doc.Fields.TextBox = paragraph_Renamed.AppendTextBox(50,20)

End Sub

Private Sub WordDocViewer(ByVal fileName As String)
	Try
		Process.Start(fileName)
	Catch
	End Try
End Sub