特定の段落やテキストのフォントの色を変更することで、Word ドキュメント内でその段落やテキストを目立たせることができます。今回は、Spire.Doc for Java を使用して Word ドキュメントのフォントの色を変更する方法を説明します。 段落のフォントの色を変更する 指定したテキストのフォントの色を変更する Spire.Doc for Java をインストールします まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>10.7.10</version> </dependency> </dependencies> 段落のフォントの色を変更する 以下は、Word ドキュメントで段落のフォントの色を変更する手順です。 Document クラスのインスタンスを作成します。 Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込む。 Document.getSections().get(sectionIndex) メソッドを使用して、目的のセクションを取得します。 Section.getParagraphs().get(paragraphIndex) メソッドを使用して、フォントの色を変更したい段落を取得します。 ParagraphStyle クラスのインスタンスを作成します。 ParagraphStyle.setName() メソッドと ParagraphStyle.getCharacterFormat().setTextColor() メソッドを使用してスタイル名とフォント色を設定する。 Document.getStyles().add() メソッドを使用して、ドキュメントにスタイルを追加します。 Paragraph.applyStyle() メソッドを使用して、段落にスタイルを適用します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.ParagraphStyle; import java.awt.*; public class ChangeFontColorOfParagraph { public static void main(String[] args) { //Document クラスのインスタンスを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/例.docx"); //最初のセクションを取得する Section section = document.getSections().get(0); //このセクションで最初の段落のフォント色を変更する Paragraph p1 = section.getParagraphs().get(0); ParagraphStyle s1 = new ParagraphStyle(document); s1.setName("色1"); s1.getCharacterFormat().setTextColor(new Color(6, 84, 148)); document.getStyles().add(s1); p1.applyStyle(s1.getName()); //このセクションで2番目の段落のフォント色を変更する Paragraph p2 = section.getParagraphs().get(1); ParagraphStyle s2 = new ParagraphStyle(document); s2.setName("色2"); s2.getCharacterFormat().setTextColor(new Color(128, 128, 128));; document.getStyles().add(s2); p2.applyStyle(s2.getName()); //ドキュメントを保存する document.saveToFile("段落のフォント色の変更.docx", FileFormat.Docx); } } 指定したテキストのフォントの色を変更する 以下は、Word ドキュメントに含まれる指定した文字のフォントの色を変更する手順です。 Document クラスのインスタンスを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.findAllString() メソッドを使用して、フォントの色を変更したいテキストを検索します。 検索されたすべてのテキストをループし、TextSelection.getAsOneRange().getCharacterFormat().setTextColor() メソッドを使用して各テキストのフォント色を変更します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.TextSelection; import java.awt.*; public class ChangeFontColorOfText { public static void main(String[] args) { //Documentクラスのインスタンスを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/例.docx"); //フォントの色を変更したいテキストを探す TextSelection[] text = document.findAllString("暗黒物質", false, true); //検索されたテキストのフォントの色を変更する for (TextSelection selection : text) { selection.getAsOneRange().getCharacterFormat().setTextColor(Color.red); } //ドキュメントを保存する document.saveToFile("テキストのフォント色の変更.docx", FileFormat.Docx); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。