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= "lye.png";
Document doc = new Document(input);
doc.getBackground().setType(BackgroundType.Picture);
doc.getBackground().setPicture(img);

但是通过这些方式添加的页面背景只能应用于整个文档页面,如果需要只对某些页面设置不同其他页面的背景,这种方法并不奏效。因此,本文总结了可实现多个页面设置不同背景的方法。

考虑到只需设置首页背景不同,或者多个页面不同背景的情况,简单分为了两种情况来介绍,但是方法都是类似的。

程序开发环境:

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("测试.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("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("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();
}
}

情况2:设置多个页面背景不同

需要说明的是,给多个页面设置不同页面是基于不同节上设置的,因此需要在文档中设置分节(插入分节符),这里测试文档中已经设置了多个分节,如果需要代码设置分节可以参考插入分节符的方法:

Document doc = new Document();
doc.loadFromFile("测试.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("测试.docx"); //获取第一节中的页眉,添加图片,调整图片格式,铺满页面
Section section1 = doc.getSections().get(0);
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);
int width = (int) section1.getPageSetup().getPageSize().getWidth();
int height = (int) section1.getPageSetup().getPageSize().getHeight();
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("result2.docx",FileFormat.Docx_2013);
doc.dispose();
}
}

总结

对Word中的不同页面设置不同背景,需要几个重要步骤:

1. 设置文档分节

2. 设置页眉图片,并调整图片格式以铺满整个页面

3. 运行程序生成文档

同理,在设置Word水印时,默认的方法也只能生成一个水印文字效果,要实现水印平铺的效果,也可以通过在页眉中添加文字的方法来实现,需要的可以参考这篇文章,里面介绍了如何来实现,这里不作赘述了。

Java 给Word不同页面设置不同背景的更多相关文章

  1. C# 给Word不同页面设置不同背景

    给Word文档设置背景时,通常只能针对整篇文档设置统一的背景,如果需要对某些页面单独设置背景,则需要通过另外的方式来实现.本文通过C# 程序代码演示如何来实现.并附VB.NET代码作参考. 思路:通过 ...

  2. Java解析word文档

    背景 在互联网教育行业,做内容相关的项目经常碰到的一个问题就是如何解析word文档. 因为系统如果无法智能的解析word,那么就只能通过其他方式手动录入word内容,效率低下,而且人工成本和录入出错率 ...

  3. Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法

    本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...

  4. Java 给Word每一页设置不同文字水印效果

    Word中设置水印时,可预设的文字或自定义文字设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法.下面,将以Ja ...

  5. java 读写word java 动态写入 模板文件

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...

  6. java导出word的6种方式(复制来的文章)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  7. Java 实现word 中写入文字图片的解决方案

    JAVA生成WORD文件的方法目前有以下两种方式: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案; 一种是poi但是他的excel处理很程序 ...

  8. java操作word,excel,pdf

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  9. [Java] Java读取Word文档

    前言 最近需要做一些NLP 方面的工作,使用的是Java,在此总结一下使用Java读取Word(.doc)格式文件的方法. Apache基金会非常厉害,开源工具包POI就可以处理微软家的文档,甚至包括 ...

随机推荐

  1. [GXYCTF2019]simple CPP

    [GXYCTF2019]simple CPP 一.查壳 无壳,64位程序 二.IDA分析 找到主函数后动态调试,看的更清楚 经过调试后我们可以找到len就是储存字符串长度的变量,之后判断长度是不是大于 ...

  2. kubeadm部署k8s

      Kubernetes技术已经成为了原生云技术的事实标准,它是目前基础软件领域最为热门的分布式调度和管理平台.于是,Kubernetes也几乎成了时下开发工程师和运维工程师必备的技能之一. 官方文档 ...

  3. Java学习_面向对象编程

    抽象类 一个class定义了方法,但没有具体执行代码,这个方法就是抽象方法,抽象方法用abstract修饰.因为抽象类本身被设计成只能用于被继承,因此,抽象类可以强迫子类实现其定义的抽象方法,否则编译 ...

  4. wildfly 21中应用程序的部署

    目录 简介 Managed Domain中的部署 管理展开的部署文件 standalone模式下的部署 standalone模式下的自动部署 Marker Files 受管理的和不受管理的部署 部署覆 ...

  5. springMVC搭建分布式框架

    https://www.cnblogs.com/lr393993507/p/7652717.html https://www.cnblogs.com/Tpf386/p/10987931.html

  6. [leetcode]21Merge Sorted ListNode递归合并顺序链表

    /** * Merge two sorted linked lists and return it as a new list. * The new list should be made by sp ...

  7. 关于vuex的数据不直接给data而要通过computed

    # 为什么vuex的数据不直接给data而要通过computed计算 ## 疑惑 其实一直以来使用vue的状态管理vuex都有一个疑惑,文档中介绍,vue的状态数据`$store.state.xx`的 ...

  8. 实现strStr

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. Nginx Consul nginx-upsync-module

    nginx consul nginx-upsync-module 依赖包: yum -y install libpcre3 libpcre3-dev ruby zlib1g-dev patch 下载n ...

  10. python基础语法1-变量

    l Python基础语法1-变量