LoginSignup
0
0

More than 3 years have passed since last update.

Java Wordページの背景に色と画像を設定

Posted at

今回はSpire.Doc for Javaというライブラリを使ってWordページの背景に色と画像を設定する方法を紹介していきます。

 下準備

1.E-iceblueの公式サイトからFree Spire.doc for Java無料版をダウンロードしてください。

f:id:lendoris:20210421110028p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.doc.jarを参照に追加してください。

f:id:lendoris:20210421110044p:plain

背景に画像

import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.*;

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

        String inputFile="Sample.docx";
        String backgroundImg="background.png";
        String outputFile="out/result.docx";

        //ファイルを読み込みます。
        Document document= new Document(inputFile);
 
        //背景のタイプを画像にします。
        document.getBackground().setType(BackgroundType.Picture);

        //背景に画像を設定します。
        document.getBackground().setPicture(backgroundImg);

        //保存します。
        document.saveToFile(outputFile, FileFormat.Docx);
    }
}

実行結果

f:id:lendoris:20210421110125p:plain

背景に色

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 WordBackground {
    public static void main(String[] args) throws IOException {

        String inputFile="Sample.docx";
        String outputFile="out/result3.docx";

        //ファイルを設定します。
        Document document= new Document(inputFile);
    
//色を設定します。
        document.getBackground().setType(BackgroundType.Gradient);
        document.getBackground().getGradient().setColor1(Color.white);
        document.getBackground().getGradient().setColor2(Color.red);
        document.getBackground().getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);
        document.getBackground().getGradient().setShadingStyle(GradientShadingStyle.Horizontal);

        //保存します。
        document.saveToFile(outputFile, FileFormat.Docx_2013);
    }
}

実行結果

f:id:lendoris:20210421110213p:plain

 

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