python设置word背景色_Java 给Word不同页面设置不同背景

本文介绍如何使用Java的Spire.Doc库为Word文档的不同页面设置不同的背景颜色、渐变或图片。提供了针对首页背景和多页面背景的示例代码,通过设置页眉和图片铺满页面来实现。
摘要由CSDN通过智能技术生成

Java 给Word不同

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();

}

}

013405aecc998c2f1ddcf052e68a11a5.png

情况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();

}

}

6d0afa436fd7123dfc58937048a8ce65.png

总结

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

1. 设置文档分节

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

3. 运行程序生成文档

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值