本文介绍使用Spire.Doc for Java给Word不同页面设置不同背景的方法。可设置纯色背景、图片背景等。设置背景时,可以下两种方法设置,具体见下文。
1、仅设置首页页面背景和其他页面不同
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();
//设置首页页眉页脚不同
section.getPageSetup().setDifferentFirstPageHeaderFooter(true);
//设置首页页面背景颜色
HeaderFooter firstpageheader = section.getHeadersFooters().getFirstPageHeader();//获取首页页眉
firstpageheader.getParagraphs().clear();//清除首页页眉默认的段落格式
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();
//设置首页页眉页脚不同
section.getPageSetup().setDifferentFirstPageHeaderFooter(true);
//设置首页页面背景图片
HeaderFooter firstpageheader = section.getHeadersFooters().getFirstPageHeader();
firstpageheader.getParagraphs().clear();
Paragraph firstpara= firstpageheader.addParagraph();
DocPicture pic0 = firstpara.appendPicture("1.png");//添加图片到首页页眉段落
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();
}
}