How to Set Text Color in Word with Silverlight

In order to highlight the special text, such as key word, notice, addition information, in Word document, we may set different colors for the text. Because of the colors, readers will pay much more attention on the text to learn some important information.

Beside special text, we can set color for title, subtitle and headings. Although people can know titles or headings at the beginning, with colors, they can be more obviously and the structure will be clearer.

In this post, I want to introduce a method about how to easily set text color in Word with Silverlight.

I prepare a Word document which includes a title and an image. The original color of title is blue. And I will change the title color as dark orange. The following image shows part of contents of the Word document which I will set text color.

Also, the component, Spire.Doc for Silverlight is used in this example. So if you want to use the following code, please download and install it on your system and then add its dll file as reference.

STEPS

Step 1. Declare SaveFileDialog

Declare a SaveFileDialog for saving document. Also, set a filter for this SaveFileDialog. The filter must be document format, for example, .doc or .docx.

Step 2. Load Document

Right click project name and add existing item (the document). After that, click this added document and change its Build Action as Embedded Resource.

At the beginning, I have added a button on UserControl. Double click this button and write code. Declare document and assembly. Use foreach sentence to get name string from assembly. If the name is the same as embedded resource name, load this document.

Step 3. Get Paragraph

Because I want to set text color for the title which is taken as first paragraph, so I need to get the paragraph from the first section in document.

Step 4. Set Text Color

Define a paragraph style. Set text color as dark orange in this style. Then, add this style in document and apply for the paragraph.

Step 5. Save Documents

Judge if the SaveFileDialog can pop up. If the result is true, use document.SaveToStream() method to save document through this SaveFileDilaog.

Full ColorSetting.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Drawing;

using System.IO;

using System.Reflection;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace SLTextColor

{

    public partial class MainPage : UserControl

    {

        //Declare SaveFileDialog

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith(“welcome.docx”))

                {

                    using (Stream docStream = assembly.GetManifestResourceStream(name))

                    {

                        document = new Document(docStream, FileFormat.Docx);

                    }

                }

            }

 

            //Get Section and Paragraph

            Section section = document.Sections[0];

            Paragraph paragraph = section.Paragraphs[0];

 

            //Set Text Color

            ParagraphStyle style = new ParagraphStyle(document);

            style.Name = “textcolor”;

            style.CharacterFormat.TextColor = Color.DarkOrange;

            document.Styles.Add(style);

            paragraph.ApplyStyle(style.Name);

 

            //Save Document

            bool? result = this.saveFiledialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream stream = this.saveFiledialog.OpenFile())

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

        }

    }

}

 

Result

 

Freely Download Spire.Doc for Silverlight

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