Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
3 juillet 2012

With Silverlight-How to Insert TextBox in Word

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.

STEPS

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.

Full InsertTextbox.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
 
namespace WordTextBox
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFiledialog = new SaveFileDialog();
        public MainPage()
        {
            InitializeComponent();
            this.saveFiledialog.Filter = "Word Document (*.docx)|*.docx";
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Create Document
            Document document = new Document();
            Section section = document.AddSection();
 
            //Insert TextBox
            Paragraph paragraph1 = section.AddParagraph();
            Spire.Doc.Fields.TextBox textBox = paragraph1.AppendTextBox(250, 150);
 
            //Set TextBox Format
            textBox.Format.FillColor = Color.LightSeaGreen;
            textBox.Format.LineColor = Color.RosyBrown;
            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", 14F);
            txtRange.CharacterFormat.TextColor = Color.GhostWhite;
 
            //Save Document
            bool? result = this.saveFiledialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFiledialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }
        }
     }

_______________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité