チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»docjavapagebackground

Displaying items by tag: docjavapagebackground

Word ドキュメントの透かしは、半透明のテキストまたは画像の背景です。透かしは、そのドキュメントが機密文書であること、または単なる草稿であることを読者に思い出させるためにテキストの透かしを使用したり、会社の写真で著作権を宣言したりするなど、ドキュメントに関する重要な何かを強調するために使用されます。時には透かしがドキュメントの読みやすさに影響することもあり、透かしを除去したい場合、Spire.Doc for Java が大いに役立ちます。この記事では、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.9.8</version>
    </dependency>
</dependencies>

Java で Word ドキュメントから透かしを除去する

Spire.Doc for Java が提供する Document.setWatermark() メソッドを使用して透かしを null に設定することで、Word ドキュメントから透かしを削除することができます。 テキスト透かしも画像透かしもこの方法で削除できます。詳しい手順は以下の通りです。

  • Document のオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.setWatermark() メソッドを使用して、透かしを削除するために透かしを null に設定しま す。
  • Document.saveToFile() メソッドを使用して Word ドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class removeWordWatermark {
    public static void main(String[] args) {
        //Documentのオブジェクトを作成する
        Document doc = new Document();

        //Wordドキュメントを読み込む
        doc.loadFromFile("サンプル.docx");

        //透かしをnullに設定する
        doc.setWatermark(null);

        //ドキュメントを保存する
        doc.saveToFile("透かしの削除.docx", FileFormat.Auto);
        doc.dispose();
    }
}

Java:Word ドキュメントから透かしを削除する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページの背景
Tagged under

繰り返し透かしは、複数行の透かしとも呼ばれ、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>11.6.0</version>
    </dependency>
</dependencies>

Word ドキュメントに繰り返しテキスト透かしを追加する

Word ドキュメントのヘッダーに指定した間隔で繰り返しワードアートを追加することで、Word ドキュメントに繰り返しテキスト透かしを挿入することができます。詳しい手順は次のとおりです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • ShapeObject クラスのオブジェクトを作成し、ShapeObject.getWordArt().setText() メソッドを使用してワードアートのテキストを設定します。
  • 回転角度と垂直方向の繰り返し回数、水平方向の繰り返し回数を指定し ます。
  • ShapeObject クラスのメソッドを使用して、図形の書式を設定します。
  • Paragraph.getChildObjects().add(ShapeObject) メソッドを使用して、各セクションのヘッダーにワードアート図形を指定された間隔で複数回追加することにより、各セクションに繰り返し透かしを挿入するために、ドキュメント内のセクションをループします。
  • Document.saveToFile() メソッドを使用してドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

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

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

        //Wordドキュメントを読み込む
        doc.loadFromFile("サンプル.docx");

        //ShapeObjectクラスのオブジェクトを作成し、アートフォントのテキストを設定する
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.getWordArt().setText("オリジナル");

        //テキスト透かしの回転角度と、垂直方向と水平方向の繰り返し回数を設定する
        double rotation = 315;
        int ver = 5;
        int hor = 3;

        //ワードアートの書式を設定する
        shape.setWidth(60);
        shape.setHeight(20);
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(rotation);
        shape.getWordArt().setFontFamily("HarmonyOS Sans SC");
        shape.setFillColor(Color.BLUE);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(Color.CYAN);
        shape.setStrokeWeight(1);

        //ドキュメントのセクションをループする
        for (Section section : (Iterable) doc.getSections()
        ) {
            //セクションのヘッダーを取得する
            HeaderFooter header = section.getHeadersFooters().getHeader();
            //ヘッダーに段落を追加する
            Paragraph paragraph = header.addParagraph();
            for (int i = 0; i < ver; i++) {
                for (int j = 0; j < hor; j++) {
                    //ヘッダーに段落を追加する
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition((float) (section.getPageSetup().getPageSize().getHeight()/ver * i  + Math.sin(rotation) * shape.getWidth()/2));
                    shape.setHorizontalPosition((float) ((section.getPageSetup().getPageSize().getWidth()/hor - shape.getWidth()/2) * j));
                    paragraph.getChildObjects().add(shape);
                }
            }
        }

        //ドキュメントを保存する
        doc.saveToFile("繰り返しテキスト透かし.docx", FileFormat.Auto);
        doc.dispose();
    }
}

Java:Word で繰り返し透かしを挿入する方法

Word ドキュメントに繰り返し画像透かしを追加する

同様に、ヘッダーに一定の間隔で繰り返し画像を追加することで、Wordドキュメントに繰り返し画像透かしを挿入することができます。詳しい手順は次のとおりです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • DocPicture.loadImage() メソッドを使用して画像を読み込みます。
  • DocPicture.setTextWrappingStyle(TextWrappingStyle.Behind) メソッドを使用して、画像の文字列の折り返しスタイルを背面に設定します。
  • 垂直方向の繰り返し回数と水平方向の繰り返し回数を指定します。
  • Paragraph.getChildObjects().add(DocPicture) メソッドを使用して、各セクションのヘッダーに画像を指定された間隔で複数回追加することにより、各セクションに繰り返し透かしを挿入するために、ドキュメント内のセクションをループします。
  • Document.saveToFile() メソッドを使用してドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

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

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

        //Wordドキュメントを読み込む
        doc.loadFromFile("サンプル.docx");

        //画像を読み込む
        DocPicture pic = new DocPicture(doc);
        pic.loadImage("透かし.png");

        //画像のテキストの折り返しを背面に設定する
        pic.setTextWrappingStyle(TextWrappingStyle.Behind);

        //垂直方向と水平方向の繰り返し回数を設定する
        int ver = 4;
        int hor = 3;

        //ドキュメントのセクションをループする
        for (Section section : (Iterable) doc.getSections()
        ) {
            //セクションのヘッダーを取得する
            HeaderFooter header = section.getHeadersFooters().getHeader();
            //ヘッダーに段落を追加する
            Paragraph paragraph = header.addParagraph();
            for (int i = 0; i < ver; i++) {
                for (int j = 0; j < hor; j++) {
                    //ヘッダーに画像を追加する
                    pic = (DocPicture) pic.deepClone();
                    pic.setVerticalPosition((float) ((section.getPageSetup().getPageSize().getHeight()/ver) * i));
                    pic.setHorizontalPosition((float) (section.getPageSetup().getPageSize().getWidth()/hor - pic.getWidth()/2) * j);
                    paragraph.getChildObjects().add(pic);
                }
            }
        }

        //ドキュメントを保存する
        doc.saveToFile("繰り返し画像透かし.docx", FileFormat.Auto);
        doc.dispose();
    }
}

Java:Word で繰り返し透かしを挿入する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページの背景
Tagged under

段落と文字の背景色は、Word ドキュメントにおけるドキュメント設計の重要な要素であります。適切な段落と文字の背景色は、特定の段落と文字を強調したり、文字のコントラストを高めて読みやすくしたり、レイアウトの隙間を埋めて組版を支援したりする役割を果たします。そのため、Word ドキュメントを作成する際には、段落やテキストの背景色を合理的に使用することが非常に重要です。この記事では、Spire.Doc for Java を使用して、Java で段落とテキストの背景色を設定する方法を説明します。

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();
    }
}

Java:段落と文字の背景色を設定する方法

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();
    }
}

Java:段落と文字の背景色を設定する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページの背景
Tagged under

ウォーターマークは、ドキュメントの背景にある淡い画像またはテキストです。文書を他の人に送信する必要がある場合は、文書にウォータマークを追加することで、他人が文書を不正に使用しないようにしたり、文書が機密、下書き、サンプルなどであることを読者に注意したりすることができます。この記事では、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.9.8</version>
    </dependency>
</dependencies>

Word ドキュメントにテキストウォータマークを追加する

以下に詳細な操作方法を示します。

  • Document インスタンスを作成します。
  • Document.loadFromFile() メソッドを使用してサンプルドキュメントをロードします。
  • Document.getSections().get() メソッドを使用して最初のセクションを取得します。
  • TextWatermark インスタンスを作成します。
  • TextWatermark クラスで提供される方法を使用して、テキストウォータマークのテキスト、フォントサイズ、色、レイアウトを設定します。
  • Section.getDocument().setWatermark() メソッドを使用して、サンプルドキュメントにテキストウォーターマークを追加します。
  • Document.saveToFile() メソッドを使用して、結果文書を保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;

public class WordTextWatermark {
    public static void main(String[] args) {
        //Documentインスタンスを作成する
        Document document = new Document();

        //サンプルドキュメントをロードする
        document.loadFromFile("Sample.docx");

        //最初のセクションを取得する
        Section section = document.getSections().get(0);

        //TextWatermarkインスタンスを作成する
        TextWatermark txtWatermark = new TextWatermark();

        //テキストウォータマークの書式を設定する
        txtWatermark.setText("機密");
        txtWatermark.setFontSize(40);
        txtWatermark.setColor(Color.red);
        txtWatermark.setLayout(WatermarkLayout.Diagonal);

        //ドキュメントにテキストウォーターマークを追加する
        section.getDocument().setWatermark(txtWatermark);

        //結果文書を保存する
        document.saveToFile("out/result.docx", FileFormat.Docx);
    }

}

Java:Word ドキュメントにテキストと画像ウォータマークを追加する方法

Word ドキュメントに画像ウォータマークを追加する

以下に詳細な操作方法を示します。

  • Document インスタンスを作成します。
  • Document.loadFromFile() メソッドを使用してサンプルドキュメントをロードします。
  • PictureWatermark インスタンスを作成します。
  • PictureWatermark.setPicture() メソッドを使用して画像を画像ウォータマークとしてロードします。PictureWatermark.setScaling() メソッドと PictureWatermark.isWashout() メソッドを使用して、画像ウォータマークのスケーリングとウォッシュアウトプロパティを設定します。
  • Document.setWatermark() メソッドを使用して、サンプルドキュメントに画像ウォーターマークを追加します。
  • Document.saveToFile() メソッドを使用して、結果文書を保存します。
  • Java
import com.spire.doc.*;


public class WordImageWatermark {
    public static void main(String[] args)  throws Exception{
        //Documentインスタンスを作成する
        Document document = new Document();

        //サンプルドキュメントをロードする
        document.loadFromFile("Sample.docx");

        //PictureWatermarkインスタンスを作成する
        PictureWatermark picture = new PictureWatermark();

        //画像ウォータマークの書式を設定する
        picture.setPicture("logo.png");
        picture.setScaling(100);
        picture.isWashout(false);

        //サンプルドキュメントにイメージウォーターマークを追加する
        document.setWatermark(picture);

        //結果文書を保存する
        document.saveToFile("out/result2.docx",FileFormat.Docx );
    }
}

Java:Word ドキュメントにテキストと画像ウォータマークを追加する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページの背景
Tagged under

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>
        <url>http://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 ドキュメントに背景色を追加する

Word ドキュメントに背景色を追加するのはとても簡単です。背景の種類を「Color」に設定し、背景となる色を選択するだけです。詳しい手順は、以下の通りです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getBackground().setType() メソッドを使用して、背景の種類を色に設定します。
  • Document.getBackground().setColor() メソッドを使用して、背景色を設定します。
  • Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.*;

public class addBackgroundColor {
    public static void main(String[] args) throws IOException {

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

        //Wordドキュメントを読み込む
        document.loadFromFile("C:/実例.docx");

        //背景の種類を「Color」に設定する
        document.getBackground().setType(BackgroundType.Color);

        //背景色を設定する
        document.getBackground().setColor(Color.orange);

        //ドキュメントを保存する
        document.saveToFile("背景色の追加.docx", FileFormat.Docx);
    }
}

Java:Word ドキュメントに背景色や背景画像を追加する方法

Word ドキュメントにグラデーションの背景を追加する

グラデーションの背景を追加するには、より多くの手順が必要です。背景の種類を「グラデーション」に設定し、2色を選択し、バリエーションとグラデーションの種類を設定する必要があります。詳しい手順は、以下の通りです。

  • Documen クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getBackground().setType() メソッドを使用して、背景の種類を「Gradient」に設定します。
  • Background.getGradient().setColor1()Background.getGradient().setColor2() メソッドを使用して2色を選択します。
  • Background.getGradient().setShadingVariant() メソッドを使用して、バリエーションを設定します。
  • Background.getGradient().setShadingStyle() メソッドを使用して、シェーディングの種類を設定します。
  • Document.saveToFile() を使用して、ドキュメントを保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import com.spire.doc.documents.GradientShadingStyle;
import com.spire.doc.documents.GradientShadingVariant;
import java.awt.*;
import java.io.*;

public class addBackgroundColor {
    public static void main(String[] args) throws IOException {

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

        //Wordドキュメントを読み込む
        document.loadFromFile("C:/実例.docx");

        //背景の種類を「Gradient」に設定する
        Background background = document.getBackground();
        background.setType(BackgroundType.Gradient);

        //2つの色を選択する
        background.getGradient().setColor1(Color.white);
        background.getGradient().setColor2(Color.orange);

        //バリエーションを選択する
        background.getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);

        //シェーディングの種類を選択する
        background.getGradient().setShadingStyle(GradientShadingStyle.Horizontal);

        //ドキュメントを保存する
        document.saveToFile("グラデーション背景の追加.docx", FileFormat.Docx);
    }
}

Java:Word ドキュメントに背景色や背景画像を追加する方法

Word ドキュメントに背景画像を挿入する

Word ドキュメントに背景画像を挿入するには、背景の種類を「Picture」に設定し、背景として画像を挿入する必要があります。詳しい手順は、以下の通りです。

  • Document クラスのオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getBackground().setType() メソッドを使用して、背景の種類を「Picture」に変更します。
  • Document.getBackground().setPicture() メソッドを使用して、背景として画像を挿入します。
  • Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.*;

public class addBackgroundColor {
    public static void main(String[] args) throws IOException {

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

        //Wordドキュメントを読み込む
        document.loadFromFile("C:/実例.docx");

        //背景の種類を「Picture」に設定する
        document.getBackground().setType(BackgroundType.Picture);

        //背景画像を挿入する
        document.getBackground().setPicture("C:/背景.jpg");

        //ドキュメントを保存する
        document.saveToFile("背景画像の挿入.docx", FileFormat.Docx);
    }
}

Java:Word ドキュメントに背景色や背景画像を追加する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページの背景
Tagged under