0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javaを使用してWordの各ページに異なる背景を設定する方法

Posted at

Word文書では、「デザイン」-「ページの色」から直接ページの背景を設定できます。そしてJavaコードを介して設定する方法は、以下の内容を参照してください。

1.単色の背景を設定します

doc.getBackground().setType(BackgroundType.Color);
doc.getBackground().setColor(Color.PINK);

2.グラデーションの背景を設定します

doc.getBackground().setType(BackgroundType.Gradient);
doc.getBackground().getGradient().setColor1(Color.white);
doc.getBackground().getGradient().setColor2(Color.green);

3.画像の背景を設定します

String img= "画像.png";
Document doc = new Document(input);
doc.getBackground().setType(BackgroundType.Picture);
doc.getBackground().setPicture(img);

ただし、これらの方法で追加されたページの背景は、ドキュメントページ全体にのみ適用できます。一部のページのみを対象として背景を他のページと異なるように設定する必要がある場合、この方法は機能しません。したがって、この記事では、背景の異なる複数のページを作成する方法を簡単に紹介します。

ホームページの背景を設定するだけで十分だと、または複数のページの背景が異なることに設定することという二つのケースを考えると、紹介する内容は、単純に2つのケースに分けられていますが、方法は本質的に似ています。

プログラム開発環境:

  1. IDEA

  2. jdk1.8.0

  3. Spire.Doc.jar

ケース1:ホームページの背景のみを異なるものに設定する

Java

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.documents.VerticalOrigin;
import com.spire.doc.fields.DocPicture;


public class DifferentPageBackground1 {
    public static void main(String[] args) {
        //Wordテストドキュメントをロードする
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.docx");

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

        //最初のページのヘッダーとフッターを別々に設定する
        section.getPageSetup().setDifferentFirstPageHeaderFooter(true);

        //ホームページのヘッダーを取得する
        HeaderFooter firstpageheader = section.getHeadersFooters().getFirstPageHeader();
        firstpageheader.getParagraphs().clear();//最初のページヘッダーのデフォルトの段落形式をクリアします(元の段落の形式がクリアされていない場合、生成されたドキュメント効果のヘッダーに水平線が表示されます)

        //段落を再追加する
        Paragraph firstpara= firstpageheader.addParagraph();

        //段落に画像を追加し、画像をフォーマットする
        DocPicture pic0 = firstpara.appendPicture("C:\\Users\\Administrator\\Desktop\\1.png");
        pic0.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic0.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic0.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);

        //ページの幅と高さを取得する
        int width = (int) section.getPageSetup().getPageSize().getWidth();
        int height = (int) section.getPageSetup().getPageSize().getHeight();

        //ページ全体に表示される画像サイズを設定する
        pic0.setWidth(width);
        pic0.setHeight(height);

        //他のページのヘッダーも同じように設定する
        HeaderFooter otherheader = section.getHeadersFooters().getHeader();
        otherheader.getParagraphs().clear();
        Paragraph otherpara = otherheader.addParagraph();
        DocPicture pic1 = otherpara.appendPicture("C:\\Users\\Administrator\\Desktop\\2.png");
        pic1.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic1.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic1.setWidth(width);
        pic1.setHeight(height);

        //ドキュメントを保存する
        doc.saveToFile("result.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

01.png

ケース2:背景の異なる複数のページを設定する

複数のページに異なる背景を設定することは、異なるセクションの設定に基づいているため、ドキュメントにセクションを設定する(セクション区切りを挿入する)必要があることに注意してください。ここでは、テストドキュメントに複数のセクションが設定されています。必要に応じてコードセクションを設定するには、セクション区切りを挿入する方法を参照できます。

Document doc = new Document();
doc.loadFromFile("test.docx");
//指定した段落の後にセクション区切りを追加する
Paragraph paragraph = doc.getSections().get(0).getParagraphs().get(5);
paragraph.insertSectionBreak(SectionBreakType.No_Break);

Java

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.documents.VerticalOrigin;
import com.spire.doc.fields.DocPicture;

public class DifferentPageBackground2 {
    public static void main(String[] args) {
        //Wordテストドキュメントをロードする
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.docx");

        //最初のセクションのヘッダーを取得し、画像を追加し、画像形式を調整して、ページをカバーする
        Section section1 = doc.getSections().get(0);
        HeaderFooter header1 = section1.getHeadersFooters().getHeader();
        header1.getParagraphs().clear();
        Paragraph para1= header1.addParagraph();
        DocPicture pic1 = para1.appendPicture("C:\\Users\\Administrator\\Desktop\\1.png");
        pic1.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic1.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        int width = (int) section1.getPageSetup().getPageSize().getWidth();
        int height = (int) section1.getPageSetup().getPageSize().getHeight();
        pic1.setWidth(width);
        pic1.setHeight(height);

        //同様に、2番目のセクションのヘッダーに画像を設定する
        Section section2 = doc.getSections().get(1);
        HeaderFooter header2 = section2.getHeadersFooters().getHeader();
        header2.getParagraphs().clear();
        Paragraph para2= header2.addParagraph();
        DocPicture pic2 = para2.appendPicture("C:\\Users\\Administrator\\Desktop\\2.png");
        pic2.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic2.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic2.setWidth(width);
        pic2.setHeight(height);

        //同様に、3番目のセクションのヘッダーに画像を設定する
        Section section3 = doc.getSections().get(2);
        HeaderFooter header3 = section3.getHeadersFooters().getHeader();
        header3.getParagraphs().clear();
        Paragraph para3= header3.addParagraph();
        DocPicture pic3 = para3.appendPicture("C:\\Users\\Administrator\\Desktop\\3.png");
        pic3.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic3.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic3.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic3.setWidth(width);
        pic3.setHeight(height);


        //ドキュメント保存する
        doc.saveToFile("result2.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

02.png

結語

今回のWordの各ページに異なる背景を設定する方法は以上でした、最後まで読んでいただきありがとうございました。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?