展开全部
用第三方吧,比较方便,参考下Free Spire.Doc for Java的方法,这个是免费版,直接用可以了。62616964757a686964616fe58685e5aeb931333431373261纯色背景色import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.IOException;
public class BackgroundColor_Doc {
public static void main (String[] args) throws IOException{
//加载测试文
String input="test.docx";
String output="backgroundcolor.docx";
Document doc = new Document(input);
//设置单色背景
doc.getBackground().setType(BackgroundType.Color);
doc.getBackground().setColor(Color.PINK);
//保存文档
doc.saveToFile(output,FileFormat.Docx_2013);
}
}
2. 渐变背景色import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import com.spire.doc.documents.GradientShadingStyle;
import com.spire.doc.documents.GradientShadingVariant;
import java.awt.*;
import java.io.IOException;
public class GradientBackground_Doc {
public static void main(String[] arg) throws IOException{
//加载测试文档
String input= "test.docx";
String output="GradientBackgound.docx";
Document doc = new Document(input);
//设置渐变色
doc.getBackground().setType(BackgroundType.Gradient);
doc.getBackground().getGradient().setColor1(Color.white);
doc.getBackground().getGradient().setColor2(Color.green);
doc.getBackground().getGradient().setShadingVariant(GradientShadingVariant.Shading_Middle);
doc.getBackground().getGradient().setShadingStyle(GradientShadingStyle.Horizontal);
//保存文档
doc.saveToFile(output, FileFormat.Docx_2010);
}
}
3. 图片背景import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.io.IOException;
public class ImgBackground_Doc {
public static void main(String[] arg) throws IOException {
//加载文件
String input= "test.docx";
String output="ImgBackgound.docx";
String img= "lye.png";
Document doc = new Document(input);
//设置图片背景
doc.getBackground().setType(BackgroundType.Picture);
doc.getBackground().setPicture(img);
//保存文档
doc.saveToFile(output, FileFormat.Docx);
}
}