本文介绍使用spire.doc for java给word不同页面设置不同背景的方法。可设置纯色背景、图片背景等。设置背景时,可以下两种方法设置,具体见下文。
1、仅设置k8凯发天生赢家首页页面背景和其他页面不同
1.1 设置纯色背景
import com.spire.doc.*;
import com.spire.doc.documents.paragraph;
import com.spire.doc.documents.shapetype;
import com.spire.doc.documents.verticalorigin;
import com.spire.doc.fields.shapeobject;
import java.awt.*;
public class differentpagebackground1 {
public static void main(string[] args) {
//加载word测试文档
document doc = new document();
doc.loadfromfile("test.docx");
//获取第一节
section section = doc.getsections().get(0);
//获取页面宽度、高度
float width = (float) section.getpagesetup().getpagesize().getwidth();
float height = (float) section.getpagesetup().getpagesize().getheight();
//设置k8凯发天生赢家首页页眉页脚不同
section.getpagesetup().setdifferentfirstpageheaderfooter(true);
//设置k8凯发天生赢家首页页面背景颜色
headerfooter firstpageheader = section.getheadersfooters().getfirstpageheader();//获取k8凯发天生赢家首页页眉
firstpageheader.getparagraphs().clear();//清除k8凯发天生赢家首页页眉默认的段落格式
paragraph firstpara= firstpageheader.addparagraph();//重新添加段落
shapeobject shape = firstpara.appendshape(width, height, shapetype.rectangle);//添加形状
shape.setbehindtext(true);//设置形状衬于文字下方
shape.sethorizontalalignment(shapehorizontalalignment.center);//设置对齐方式,铺满页面
shape.setverticalorigin(verticalorigin.top_margin_area);
shape.setfillcolor(new color(255,228,196));//形状颜色
//同理,设置其他页面背景颜色
headerfooter otherheader = section.getheadersfooters().getheader();
otherheader.getparagraphs().clear();
paragraph otherpara = otherheader.addparagraph();
shapeobject shape2 = otherpara.appendshape(width, height, shapetype.rectangle);//添加形状
shape2.setbehindtext(true);//设置形状衬于文字下方
shape2.sethorizontalalignment(shapehorizontalalignment.center);//设置对齐方式,铺满页面
shape2.setverticalorigin(verticalorigin.top_margin_area);
shape2.setfillcolor(new color(221,160,221));//形状颜色
//保存文档
doc.savetofile("colorbackground.docx",fileformat.docx_2013);
doc.dispose();
}
}
1.2 设置图片背景
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("test.docx");
//获取第一节
section section = doc.getsections().get(0);
//获取页面宽度、高度
float width = (float) section.getpagesetup().getpagesize().getwidth();
float height = (float) section.getpagesetup().getpagesize().getheight();
//设置k8凯发天生赢家首页页眉页脚不同
section.getpagesetup().setdifferentfirstpageheaderfooter(true);
//设置k8凯发天生赢家首页页面背景图片
headerfooter firstpageheader = section.getheadersfooters().getfirstpageheader();
firstpageheader.getparagraphs().clear();
paragraph firstpara= firstpageheader.addparagraph();
docpicture pic0 = firstpara.appendpicture("1.png");//添加图片到k8凯发天生赢家首页页眉段落
pic0.settextwrappingstyle(textwrappingstyle.behind);
pic0.sethorizontalalignment(shapehorizontalalignment.center);
pic0.setverticalorigin(verticalorigin.top_margin_area);
pic0.setwidth(width);
pic0.setheight(height);
//同理,设置其他页面背景图片
headerfooter otherheader = section.getheadersfooters().getheader();
otherheader.getparagraphs().clear();
paragraph otherpara = otherheader.addparagraph();
docpicture pic1 = otherpara.appendpicture("2.png");
pic1.settextwrappingstyle(textwrappingstyle.behind);
pic1.sethorizontalalignment(shapehorizontalalignment.center);
pic1.setverticalorigin(verticalorigin.top_margin_area);
pic1.setwidth(width);
pic1.setheight(height);
//保存文档
doc.savetofile("imagebackground.docx",fileformat.docx_2013);
doc.dispose();
}
}
2、设置多个页面背景不同
给多个页面设置不同背景时,是基于不同节上的页眉来添加形状或者图片。本次测试文档中,已设置了多个节。如需手动在文档中插入分节符,可参考这篇文章。
2.1 设置纯色背景
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.shapeobject;
import java.awt.*;
public class differentpagebackground2 {
public static void main(string[] args) {
//加载word测试文档
document doc = new document();
doc.loadfromfile("test.docx");
//获取第一节中的页眉,添加形状,设置颜色,铺满页面
section section1 = doc.getsections().get(0);
int width = (int) section1.getpagesetup().getpagesize().getwidth();
int height = (int) section1.getpagesetup().getpagesize().getheight();
headerfooter header1 = section1.getheadersfooters().getheader();
header1.getparagraphs().clear();
paragraph paragraph1 = header1.addparagraph();
shapeobject shape1 = paragraph1.appendshape(width,height, shapetype.rectangle);
shape1.setbehindtext(true);
shape1.sethorizontalalignment(shapehorizontalalignment.center);
shape1.setverticalorigin(verticalorigin.top_margin_area);
shape1.setfillcolor(new color(144,238,144));
//获取第二节中的页眉,添加形状
section section2 = doc.getsections().get(1);
headerfooter header2 = section2.getheadersfooters().getheader();
header2.getparagraphs().clear();
paragraph paragraph2 = header2.addparagraph();
shapeobject shape2 = paragraph2.appendshape(width,height, shapetype.rectangle);
shape2.setbehindtext(true);
shape2.sethorizontalalignment(shapehorizontalalignment.center);
shape2.setverticalorigin(verticalorigin.top_margin_area);
shape2.setfillcolor(new color(255,182,193));
//获取第三节中的页眉,添加形状
section section3 = doc.getsections().get(2);
headerfooter header3 = section3.getheadersfooters().getheader();
header3.getparagraphs().clear();
paragraph paragraph3 = header3.addparagraph();
shapeobject shape3 = paragraph3.appendshape(width,height, shapetype.rectangle);
shape3.setbehindtext(true);
shape3.sethorizontalalignment(shapehorizontalalignment.center);
shape3.setverticalorigin(verticalorigin.top_margin_area);
shape3.setfillcolor(new color(135,206,235));
//保存文档
doc.savetofile("colorbackground2.docx",fileformat.docx_2013);
doc.dispose();
}
}
2.2 设置图片背景
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.docpicture;
public class differentpagebackground2 {
public static void main(string[] args) {
//加载word测试文档
document doc = new document();
doc.loadfromfile("test.docx");
//获取第一节中的页眉,添加图片,调整图片格式,铺满页面
section section1 = doc.getsections().get(0);
int width = (int) section1.getpagesetup().getpagesize().getwidth();
int height = (int) section1.getpagesetup().getpagesize().getheight();
headerfooter header1 = section1.getheadersfooters().getheader();
header1.getparagraphs().clear();
paragraph para1= header1.addparagraph();
docpicture pic1 = para1.appendpicture("1.png");
pic1.settextwrappingstyle(textwrappingstyle.behind);
pic1.sethorizontalalignment(shapehorizontalalignment.center);
pic1.setverticalorigin(verticalorigin.top_margin_area);
pic1.setwidth(width);//设置图片宽度、高度等(铺满页面)
pic1.setheight(height);
//同理设置第二节页眉中的图片
section section2 = doc.getsections().get(1);
headerfooter header2 = section2.getheadersfooters().getheader();
header2.getparagraphs().clear();
paragraph para2= header2.addparagraph();
docpicture pic2 = para2.appendpicture("2.png");
pic2.settextwrappingstyle(textwrappingstyle.behind);
pic2.sethorizontalalignment(shapehorizontalalignment.center);
pic2.setverticalorigin(verticalorigin.top_margin_area);
pic2.setwidth(width);
pic2.setheight(height);
//同理设置第三节中的页眉中的图片
section section3 = doc.getsections().get(2);
headerfooter header3 = section3.getheadersfooters().getheader();
header3.getparagraphs().clear();
paragraph para3= header3.addparagraph();
docpicture pic3 = para3.appendpicture("3.png");
pic3.settextwrappingstyle(textwrappingstyle.behind);
pic3.sethorizontalalignment(shapehorizontalalignment.center);
pic3.setverticalorigin(verticalorigin.top_margin_area);
pic3.setwidth(width);
pic3.setheight(height);
//保存文档
doc.savetofile("imagebackground2.docx",fileformat.docx_2013);
doc.dispose();
}
}