段落と文字の背景色は、Word ドキュメントにおけるドキュメント設計の重要な要素であります。適切な段落と文字の背景色は、特定の段落と文字を強調したり、文字のコントラストを高めて読みやすくしたり、レイアウトの隙間を埋めて組版を支援したりする役割を果たします。そのため、Word ドキュメントを作成する際には、段落やテキストの背景色を合理的に使用することが非常に重要です。この記事では、Spire.Doc for Java を使用して、Java で段落とテキストの背景色を設定する方法を説明します。 Word ドキュメントで段落の背景色を設定する 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>11.4.2</version> </dependency> </dependencies> Word ドキュメントで段落の背景色を設定する 段落の背景色を設定するには、段落を取得し、Paragraph.getFormat().setBackColor() メソッドを使用して背景色を変更する必要があります。詳しい手順は以下の通りです。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getSections().get() メソッドを使用して、最初のセクションを取得します。 Section.getParagraphs().get() メソッドを使用して、2つ目の段落を取得します。 Paragraph.getFormat().setBackColor() を使用して、段落の背景色を設定します。 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 java.awt.*; public class SetParagraphBackgroundColor { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/サンプル.docx"); //最初のセクションを取得する Section section = document.getSections().get(0); //2段落目を取得する Paragraph paragraph = section.getParagraphs().get(1); //この段落の背景色を淡灰色に設定する paragraph.getFormat().setBackColor(Color.LIGHT_GRAY); //ドキュメントを保存する document.saveToFile("段落の背景色.docx", FileFormat.Docx_2013); document.dispose(); } } Word ドキュメントで既存の文字の背景色を設定する Spire.Doc for Java は、Word ドキュメント内の特定のテキストの出現箇所をすべて検索する Document.findAllString() メソッドと、特定のテキストの背景色を設定する TextRange.getCharacterFormat ().setTextBackgroundColor() メソッドを提供します。既存のテキストの背景色を設定する詳しい手順は以下の通りです。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して、Word ドキュメントを読み込みます。 Document.findAllString() メソッドを使用して、"時のない宇宙" の出現箇所をすべて検索します。 出現した文字列をループします。 TextSelection.getAsOneRange() メソッドを使用して、各出現箇所をテキスト範囲として取得します。 TextRange.getCharacterFormat().setTextBackgroundColor() メソッドを使用して、テキスト範囲の背景色を設定します。 また、出現箇所をコレクションからインデックスで選択し、テキスト範囲として取得し、その出現箇所に対してのみ背景色を設定することも可能です。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.TextRange; import java.awt.*; public class SetTextBackgroundColor { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/サンプル.docx"); //背景色を設定するテキストを探す TextSelection[] textSelections = document.findAllString("時間を超越した宇宙", false, true); //出現するすべてのテキストに背景色を設定する for (TextSelection selection : textSelections){ //出現箇所をテキスト範囲として取得する TextRange textRange = selection.getAsOneRange(); //出現するテキストの背景色を設定する textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN); } //テキストの最初の出現箇所の背景色を設定する //TextRange textRange = textSelections[0].getAsOneRange(); //textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN); //ドキュメントを保存する document.saveToFile("文字の背景色.docx", FileFormat.Docx_2013); document.dispose(); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。