Sometimes, people may insert a textbox in Word document in order to distinguish some additional information or note from body. People can change size of textbox or adjust location to make it to be presented more appropriately.
Textbox includes textbox and vertical textbox. In vertical textbox, text direction is assigned vertically. The vertical textbox is very useful if we want to add quote or sidebar in Word document.
In this post, I will introduce a method about how to insert textbox in Word with Silverlight. I will create a textbox in a blank document and add contents in it.
Also, the component, Spire.Doc for Silverlight is used for realizing this function more quickly and easily. So, if you want to use the following code, please DOWNLOAD and install it at first, and then add its dll file as reference in your project.
Step 1. Design UserControl
Rename MainPage.xaml as InsertTextBox.xaml. Double click it and design UserControl. Add a label and a button in UserControl. Then, change label and button contents. Set format for the contents, including font style, size and color. Finally, set background color for UserControl.
Step 2. Declare SaveFileDialog
Declare a SaveFileDialog to save documents. And set a filter for it. The filter is used for users to choose format for documents. In this example, I set the format in filter as .docx only.
Step 3. Insert TextBox
Create a new Word document and add a section in this document. Then, add a paragraph in this section. Insert textbox in paragraph by using paragraph.AppendTextBox() method. Two parameters passed to this method, box width and height.
Step 4. Set TextBox Format
Firstly, set textbox format, including fill color, line color, line width and line style.
Secondly, add contents in textbox and set format. Add a paragraph in textbox by using textbox.Body.AddParagraph() method. Then, append text in this paragraph. Finally, set font style and color for contents.
Step 5. Save Document
Judge if the SaveFileDialog which I declare in first step can pop up. If the result is true, save document which textbox has been inserted through this SaveFileDialog.
public partial class MainPage : UserControl
private SaveFileDialog saveFiledialog = new SaveFileDialog();
this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;
private void button1_Click(object sender, RoutedEventArgs e)
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph1 = section.AddParagraph();
Spire.Doc.Fields.TextBox textBox = paragraph1.AppendTextBox(250, 150);
//Set TextBox Format
textBox.Format.FillColor = Color.DeepSkyBlue;
textBox.Format.LineColor = Color.Gray;
textBox.Format.LineWidth = 3.5F;
textBox.Format.LineStyle = TextBoxLineStyle.ThickThin;
//Add Contents in TextBox
Paragraph paragraph2 = textBox.Body.AddParagraph();
TextRange txtRange = paragraph2.AppendText(“Insert TextBoxt in Word”);
txtRange.CharacterFormat.Font = new Font(“Impact”, 18F);
txtRange.CharacterFormat.TextColor = Color.White;
bool? result = this.saveFiledialog.ShowDialog();
if (result.HasValue && result.Value)
using (Stream stream = this.saveFiledialog.OpenFile())
Click Here to LEARN MORE about Spire.Doc for Silverlight
Spire.Office also can be used to realize this function