Spire.Doc for .NET (337)
Children categories
By default, the font color of a word document is black. To achieve a more distinctive visual effect, we can change it to red, green or any other colors we like.
This article describes how to change word font color with Spire.Doc for WPF in C#, VB.NET.
At first, please download Spire.Doc and install it correctly, then add Spire.Doc. Wpf.dll and Spire.License.dll from the installation folder as reference.
Below is the screenshot of the original word document:

Detail steps:
Use namespace:
using System.Drawing; using System.Windows; using Spire.Doc; using Spire.Doc.Documents;
Step 1: Initialize a new instance of Document class and load the sample document from file.
Document document = new Document();
document.LoadFromFile("Story.docx");
Step 2: Get its first section and first paragraph (the Title), then set text color for paragraph 1.
Section section = document.Sections[0]; Paragraph p1 = section.Paragraphs[0]; ParagraphStyle s1 = new ParagraphStyle(document); s1.Name = "TitleTextColor"; s1.CharacterFormat.TextColor = Color.RosyBrown; document.Styles.Add(s1); p1.ApplyStyle(s1.Name);
Step 3: Get the second paragraph and set text color for paragraph 2.
Paragraph p2 = section.Paragraphs[1]; ParagraphStyle s2 = new ParagraphStyle(document); s2.Name = "BodyTextColor"; s2.CharacterFormat.TextColor = Color.DarkBlue; document.Styles.Add(s2); p2.ApplyStyle(s2.Name);
Step 4: Save and launch the file.
document.SaveToFile("FontColor.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("FontColor.docx");
Output:

Full codes:
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Load Document
Document document = new Document();
document.LoadFromFile("Story.docx");
//Set Text Color for Paragraph 1(the Title)
Section section = document.Sections[0];
Paragraph p1 = section.Paragraphs[0];
ParagraphStyle s1 = new ParagraphStyle(document);
s1.Name = "TitleTextColor";
s1.CharacterFormat.TextColor = Color.RosyBrown;
document.Styles.Add(s1);
p1.ApplyStyle(s1.Name);
//Set Text Color for Paragraph 2
Paragraph p2 = section.Paragraphs[1];
ParagraphStyle s2 = new ParagraphStyle(document);
s2.Name = "BodyTextColor";
s2.CharacterFormat.TextColor = Color.DarkBlue;
document.Styles.Add(s2);
p2.ApplyStyle(s2.Name);
//Save and Launch
document.SaveToFile("FontColor.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("FontColor.docx");
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Imports System.Windows
Namespace WpfApplication1
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
'Load Document
Dim document As New Document()
document.LoadFromFile("Story.docx")
'Set Text Color for Paragraph 1(the Title)
Dim section As Section = document.Sections(0)
Dim p1 As Paragraph = section.Paragraphs(0)
Dim s1 As New ParagraphStyle(document)
s1.Name = "TitleTextColor"
s1.CharacterFormat.TextColor = Color.RosyBrown
document.Styles.Add(s1)
p1.ApplyStyle(s1.Name)
'Set Text Color for Paragraph 2
Dim p2 As Paragraph = section.Paragraphs(1)
Dim s2 As New ParagraphStyle(document)
s2.Name = "BodyTextColor"
s2.CharacterFormat.TextColor = Color.DarkBlue
document.Styles.Add(s2)
p2.ApplyStyle(s2.Name)
'Save and Launch
document.SaveToFile("FontColor.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("FontColor.docx")
End Sub
End Class
End Namespace
Using Spire.Doc for WPF, programmers can easily create, open, modify and save Word documents in WPF applications. In this article, we’ll introduce how to insert a paragraph to desired position in an existing Word document.
To begin with, you need to download Spire.Doc for WPF and add the Spire.Doc.Wpf.dll and Spire.License.dll as the references to your WPF project. Then add a button in MainWindow and double click the button to write code.
Here are code snippets in the button click event:
Step 1: Initialize a new instance of Document class and load the sample Word document.
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
Step 2: Initialize a new instance of Paragraph class and append some text to it.
Paragraph paraInserted = new Paragraph(document);
TextRange textRange1 = paraInserted.AppendText("Hello, this is a new paragraph.");
Step 3: Set the text formatting of the paragraph.
textRange1.CharacterFormat.TextColor = System.Drawing.Color.Purple; textRange1.CharacterFormat.FontSize = 11; textRange1.CharacterFormat.FontName = "Calibri Light";
Step 4: Insert the paragraph at the first section at index 1, which indicates the position of the second paragraph in the section.
document.Sections[0].Paragraphs.Insert(1, paraInserted);
Step 5: Save the file.
document.SaveToFile("result.docx", FileFormat.Docx);
Output:

Full Code:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
Paragraph paraInserted = new Paragraph(document);
TextRange textRange1 = paraInserted.AppendText("Hello, this is a new paragraph.");
textRange1.CharacterFormat.TextColor = System.Drawing.Color.Purple;
textRange1.CharacterFormat.FontSize = 11;
textRange1.CharacterFormat.FontName = "Calibri Light";
document.Sections[0].Paragraphs.Insert(1, paraInserted);
document.SaveToFile("result.docx", FileFormat.Docx);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Windows
Namespace WpfApplication1
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
Dim document As New Document()
document.LoadFromFile("sample.docx", FileFormat.Docx)
Dim paraInserted As New Paragraph(document)
Dim textRange1 As TextRange = paraInserted.AppendText("Hello, this is a new paragraph.")
textRange1.CharacterFormat.TextColor = System.Drawing.Color.Purple
textRange1.CharacterFormat.FontSize = 11
textRange1.CharacterFormat.FontName = "Calibri Light"
document.Sections(0).Paragraphs.Insert(1, paraInserted)
document.SaveToFile("result.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
This article is going to introduce how to create, write and save word document in WPF via Spire.Doc for WPF.
Spire.Doc for WPF enables users to do a large range of manipulations (such as create, write, open, edit, convert and save, etc.) on word with high performance and efficiency. In addition, as a powerful and independent library, it doesn’t require Microsoft Office or any other 3rd party tools to be installed on system.
Note: please download and install Spire.Doc correctly and add the dll file from the installation folder as reference.
First, let’s begin to create a word document in WPF.
Use namespace:
using System.Windows; using Spire.Doc; using Spire.Doc.Documents;
Step 1: Create a new word document instance, next add a section and a paragraph to it.
//Create New Word Document doc = new Document(); //Add Section Section section = doc.AddSection(); //Add Paragraph Paragraph Para = section.AddParagraph();
Second, we’re going to write something into the document.
Step 2: Append some text to it.
//Append Text
Para.AppendText("Hello! "
+ "I was created by Spire.Doc for WPF, it's a professional .NET Word component "
+ "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert "
+ "Word document) without installing Microsoft Office and any other third-party tools on system.");
Third, save the generated document.
Step 3: Save and launch the document.
//Save and launch
doc.SaveToFile("MyWord.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("MyWord.docx");
Output:

Full codes:
using Spire.Doc;
using Spire.Doc.Documents;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Create New Word
Document doc = new Document();
//Add Section
Section section = doc.AddSection();
//Add Paragraph
Paragraph Para = section.AddParagraph();
//Append Text
Para.AppendText("Hello! "
+ "I was created by Spire.Doc for WPF, it's a professional .NET Word component "
+ "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert "
+ "Word document) without installing Microsoft Office and any other third-party tools on system.");
//Save and launch
doc.SaveToFile("MyWord.docx", Spire.Doc.FileFormat.Docx);
System.Diagnostics.Process.Start("MyWord.docx");
}
}
}