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.
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.
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();
Assembly assembly = this.GetType().Assembly;
foreach (String name in assembly.GetManifestResourceNames())
using (Stream docStream = assembly.GetManifestResourceStream(name))
document = new Document(docStream, FileFormat.Docx);
//Get Section and Paragraph
Section section = document.Sections;
Paragraph paragraph = section.Paragraphs;
//Set Text Color
ParagraphStyle style = new ParagraphStyle(document);
style.Name = “textcolor”;
style.CharacterFormat.TextColor = Color.DarkOrange;
bool? result = this.saveFiledialog.ShowDialog();
if (result.HasValue && result.Value)
using (Stream stream = this.saveFiledialog.OpenFile())
Freely Download Spire.Doc for Silverlight