文字色は、ドキュメントのデザインにとって重要です。文字色を設定することで、タイトルを等級分けしたり、内容を強調したりすることができます。うまくデザインされた一連の文字色は、ドキュメントの美観を大きく向上させ、読者のドキュメントを読むことへの興味を高めます。この記事では、無料のFree Spire.Doc for .NETを使って、Wordドキュメントのテキストの色を変更する方法を紹介します。
【依存関係の追加】
まず、Free Spire.Doc for.NET パッケージに含まれるDLLファイルを.NETプロジェクトにリファレンスとして追加する必要があります。DLLファイルは、このリンクからダウンロードするか、NuGet経由でインストールすることができます。
PM> Install-Package FreeSpire.Doc
段落のテキストの色を変更する
Wordドキュメントで段落のテキストの色を変更する手順は次のとおりです。
- Document のインスタンスを作成します。
- Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
- Document.Sections[sectionIndex] プロパティで目的のセクションを取得します。
- Section.Paragraphs[paragraphIndex] プロパティで、テキストの色を変更したい段落を取得します。
- ParagraphStyle のインスタンスを作成します。
- ParagraphStyle.Name プロパティと ParagraphStyle.CharacterFormat.TextColor プロパティでスタイル名とテキストの色を設定します。
- Document.Styles.Add() メソッドを使用して、ドキュメントにスタイルを追加します。
- Paragraph.ApplyStyle() メソッドを使用して、段落にスタイルを適用します。
- Document.SaveToFile() メソッドを使用して、ドキュメントを保存します。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForParagraph
{
class Program
{
static void Main(string[] args)
{
//Documentのインスタンスを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.LoadFromFile("C:/宇宙の視野.docx");
//最初のセクションを取得する
Section section = document.Sections[0];
//2段落目のテキスト色を変更する
Paragraph p1 = section.Paragraphs[1];
ParagraphStyle s1 = new ParagraphStyle(document);
s1.Name = "Color1";
s1.CharacterFormat.TextColor = Color.RosyBrown;
document.Styles.Add(s1);
p1.ApplyStyle(s1.Name);
//3段落目のテキスト色を変更する
Paragraph p2 = section.Paragraphs[2];
ParagraphStyle s2 = new ParagraphStyle(document);
s2.Name = "Color2";
s2.CharacterFormat.TextColor = Color.DarkBlue;
document.Styles.Add(s2);
p2.ApplyStyle(s2.Name);
//ドキュメントを保存する
document.SaveToFile("段落のテキスト色の変更.docx", FileFormat.Docx);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace ChangeFontColorForParagraph
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのインスタンスを作成する
Dim document As Document = New Document()
'Wordドキュメントを読み込む
document.LoadFromFile("C:/宇宙の視野.docx")
'最初のセクションを取得する
Dim section As Section = document.Sections(0)
'2段落目のテキスト色を変更する
Dim p1 As Paragraph = section.Paragraphs(1)
Dim s1 As ParagraphStyle = New ParagraphStyle(document)
s1.Name = "Color1"
s1.CharacterFormat.TextColor = Color.RosyBrown
document.Styles.Add(s1)
p1.ApplyStyle(s1.Name)
'3段落目のテキスト色を変更する
Dim p2 As Paragraph = section.Paragraphs(2)
Dim s2 As ParagraphStyle = New ParagraphStyle(document)
s2.Name = "Color2"
s2.CharacterFormat.TextColor = Color.DarkBlue
document.Styles.Add(s2)
p2.ApplyStyle(s2.Name)
'ドキュメントを保存する
document.SaveToFile("段落のテキスト色の変更.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
特定のテキストの色を変更する
Wordドキュメント内の特定のテキストの色を変更する手順を説明します。
- Document のインスタンスを作成します。
- Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
- Document.FindAllString() メソッドを使用して、色を変更したいテキストを検索します。
- 検索したテキストのすべての出現箇所をループし、TextSelection.GetAsOneRange().CharacterFormat.TextColor プロパティを使用して、出現箇所ごとにテキスト色を変更します。
- Document.SaveToFile() メソッドを使用して、ドキュメントを保存します。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForText
{
class Program
{
static void Main(string[] args)
{
//Documentのインスタンスを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.LoadFromFile("C:/宇宙の視野.docx");
//色を変えたいテキストを探す
TextSelection[] text = document.FindAllString("啓蒙運動", false, true);
//検索されたテキストの色を変更する
foreach (TextSelection seletion in text)
{
seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red;
}
//ドキュメントを保存する
document.SaveToFile("特定のテキスト色の変更.docx", FileFormat.Docx);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace ChangeFontColorForText
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのインスタンスを作成する
Dim document As Document = New Document()
'Wordドキュメントを読み込む
document.LoadFromFile("C:/宇宙の視野.docx")
'色を変えたいテキストを探す
Dim text() As TextSelection = document.FindAllString("啓蒙運動", False, True)
'検索されたテキストの色を変更する
Dim seletion As TextSelection
For Each seletion In text
seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red
Next
'ドキュメントを保存する
document.SaveToFile("特定のテキスト色の変更.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
文字色以外にも、段落のインデント、文字揃え、文字サイズなども文書デザインには重要です。これらの設定を変更する方法について学ぶには、リンクをクリックしてください。