Spire.Doc for Java v2.2.0
方法1:通过官网下载。在程序下新建一个directory目录,并命名(本示例中命名为lib);将控件包lib文件夹下的jar(如下图1)直接复制到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”Add as Library”。完成导入(如下图2)。
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); }}
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); }}
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); }}