Word 文書の特定の段落またはテキストを強調したい場合は、そのフォントの色を変更したらできます。この記事では、Spire.Doc for .NET ライブラリーを使用して C# および VB.NET で Word のフォントの色を変更する方法について紹介します。 段落のフォントの色を変更する 特定のテキストのフォントの色を変更する Spire.Doc for .NET をインストールします まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。 PM> Install-Package Spire.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# VB.NET using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace ChangeFontColorForParagraph { class Program { static void Main(string[] args) { //ドキュメントインスタンスを作成する Document document = new Document(); //Word文書をロードする document.LoadFromFile("C:UsersAdministratorDesktopSample.docx"); //最初のセクションを取得する Section section = document.Sections[0]; //最初の段落のテキストの色を変更する Paragraph p1 = section.Paragraphs[0]; ParagraphStyle s1 = new ParagraphStyle(document); s1.Name = "Color1"; s1.CharacterFormat.TextColor = Color.RosyBrown; document.Styles.Add(s1); p1.ApplyStyle(s1.Name); //2番目の段落のテキストの色を変更する Paragraph p2 = section.Paragraphs[1]; ParagraphStyle s2 = new ParagraphStyle(document); s2.Name = "Color2"; s2.CharacterFormat.TextColor = Color.DarkBlue; document.Styles.Add(s2); p2.ApplyStyle(s2.Name); //結果ドキュメントを保存する document.SaveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx); } } } Imports Spire.Doc Imports Spire.Doc.Documents Imports System.Drawing Namespace ChangeFontColorForParagraph Class Program Shared Sub Main(ByVal args() As String) 'ドキュメントインスタンスを作成する Dim document As Document = New Document() 'Word文書をロードする document.LoadFromFile("C: UsersAdministratorDesktopSample.docx") '最初のセクションを取得する Dim section As Section = document.Sections(0) '最初の段落のテキストの色を変更する Dim p1 As Paragraph = section.Paragraphs(0) Dim s1 As ParagraphStyle = New ParagraphStyle(document) s1.Name = "Color1" s1.CharacterFormat.TextColor = Color.RosyBrown document.Styles.Add(s1) p1.ApplyStyle(s1.Name) '2番目の段落のテキストの色を変更する Dim p2 As Paragraph = section.Paragraphs(1) 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("ChangeParagraphTextColor.docx", FileFormat.Docx) End Sub End Class End Namespace 特定のテキストのフォントの色を変更する Word 文書の特定のテキストのフォントの色を変更する手順は次のとおりです。 Document インスタンスを作成します。 Document.LoadFromFile() メソッドを使用して Word 文書をロードします。 Document.FindAllString() メソッドを使用して、フォントの色を変更するテキストを検索します。 検索されたテキストのすべての出現箇所をループし、TextSelection.GetAsOneRange().CharacterFormat.TextColor プロパティを使用して出現箇所ごとにフォントの色を変更します。 Document.SaveToFile() メソッドを使用して結果ドキュメントを保存します。 C# VB.NET using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace ChangeFontColorForText { class Program { static void Main(string[] args) { //ドキュメントインスタンスを作成する Document document = new Document(); //Word文書をロードする document.LoadFromFile("C:UsersAdministratorDesktopSample.docx"); //フォントの色を変更するテキストを見つける TextSelection[] text = document.FindAllString("Spire.Doc for .NET", false, true); //検索したテキストのフォントの色を変更する foreach (TextSelection seletion in text) { seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red; } //結果ドキュメントを保存する document.SaveToFile("ChangeCertainTextColor.docx", FileFormat.Docx); } } } Imports Spire.Doc Imports Spire.Doc.Documents Imports System.Drawing Namespace ChangeFontColorForText Class Program Shared Sub Main(ByVal args() As String) 'ドキュメントインスタンスを作成する Dim document As Document = New Document() 'Word文書をロードする document.LoadFromFile("C:UsersAdministratorDesktopSample.docx") 'フォントの色を変更するテキストを見つける Dim text() As TextSelection = document.FindAllString("Spire.Doc for .NET",False,True) '検索したテキストのフォントの色を変更する Dim seletion As TextSelection For Each seletion In text seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red Next '結果ドキュメントを保存する document.SaveToFile("ChangeCertainTextColor.docx", FileFormat.Docx) End Sub End Class End Namespace 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。