LoginSignup
0
0

JavaでWord文書のテキスト、段落、ページの背景色の設定方法

Last updated at Posted at 2023-05-26

Word文書の背景色は、読みやすさの向上、コンテンツの強調、組版の補助、美観の向上などに役立つ強力なツールです。テキストに背景色を設定して重要な語句を強調したり、段落に背景色を設定して異なる段落を区別しやすくしたり、ページに背景色を設定して読書効率を向上させたりすることができます。この記事では、無料のFree Spire.Doc for Javaを使用して、Word文書のテキスト段落ページの背景色を設定する方法を紹介します。

【依存関係の追加】

この方法は、無料のFree Spire.Doc for Javaが必要ですので、先にjarファイルをインポートしてください。

1. Maven

Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。

<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.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

2. 公式サイトよりJarファイルをダウンロード

まず、Free Spire.Doc for Javaの公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Doc.jarファイルを依存関係としてプロジェクトにインポートしてください。

特定のテキストの背景色を設定

Free Spire.Doc for Javaでは、テキストの背景色を設定するために 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();
    }
}

【結果文書】
特定のテキストの背景色を設定

特定の段落の背景色を設定

また、Free Spire.Doc for Javaでは、段落の背景色を設定する 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文書のページ背景を色に設定するには Document.getBackground().setType() メソッドを使用し、ページ背景として特定の色を設定するには Document.getBackground().setColor() メソッドを使用します。ページの背景色を設定する詳しい手順は、次のとおりです。

  • Document のオブジェクトを作成します。
  • Document.loadFromFile() メソッドで、Wordドキュメントを読み込みます。
  • Document.getBackground().setType() メソッドでページの背景を色で設定します。
  • Document.getBackground().setColor() メソッドでページの背景色を設定します。
  • Document.saveToFile() メソッドでドキュメントを保存します。

Java

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BackgroundType;

import java.awt.*;

public class SetPageBackgroundColor {
    public static void main(String[] args) {

        //Documentのオブジェクトを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("C:/万物は一体である.docx");

        //ページの背景を色で設定する
        document.getBackground().setType(BackgroundType.Color);

        //ページの背景色をピンクに設定する
        document.getBackground().setColor(Color.PINK);

        //ドキュメントを保存する
        document.saveToFile("ページの背景.docx", FileFormat.Docx_2013);
    }
}

【結果文書】
ページの背景色を設定

テキスト、段落、ページの背景設定以外にも、Free Spire.Doc for Javaは多くのWord文書操作の機能を備えています。詳しくは、Spire.Doc for Javaのチュートリアルをご覧ください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0