如何详细解析使用Java调整Word文档页面背景色的步骤?
- 内容介绍
- 文章标签
- 相关推荐
本文共计758个文字,预计阅读时间需要4分钟。
前言:Word中可以针对不同文档排版设计要求,来设置背景和颜色。常见的可设置单一颜色、渐变颜色或加载图片来作为背景。下面将通过Java来设置以上三种Word页面背景色。
工具:SPI
1. 设置单一颜色背景:javaimport org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;
public void setSingleColorBackground(XWPFDocument document, String color) { XWPFParagraph paragraph=document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText(Hello World!); run.getCTR().addPPr().addBkgColor(color);}
2. 设置渐变颜色背景:javaimport org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;
public void setGradientColorBackground(XWPFDocument document, String startColor, String endColor) { XWPFParagraph paragraph=document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText(Hello World!); run.getCTR().addPPr().addBkgColor(startColor, endColor);}
3. 设置图片背景:javaimport org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;
public void setImageBackground(XWPFDocument document, String imagePath) { XWPFParagraph paragraph=document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText(Hello World!); run.getCTR().addPPr().addBkgImage(imagePath);}
前言
Word中可以针对不同文档排版设计要求来设置背景设置颜色。常见的可设置单一颜色、渐变色或加载图片来设置成背景。下面通过Java来设置以上3种Word页面背景色。
使用工具:Spire.Doc for Java v2.2.0
Jar文件导入方法
方法1:通过官网下载。在程序下新建一个directory目录,并命名(本示例中命名为lib);将控件包中lib文件夹下的Spire.Doc.jar(如下图1)直接复制到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”Add as Library”。完成导入(如下图2)。
图1:
图2:
方法2:通过maven导入。参考导入方法。
Java代码示例(供参考)
添加单一颜色的背景色
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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计758个文字,预计阅读时间需要4分钟。
前言:Word中可以针对不同文档排版设计要求,来设置背景和颜色。常见的可设置单一颜色、渐变颜色或加载图片来作为背景。下面将通过Java来设置以上三种Word页面背景色。
工具:SPI
1. 设置单一颜色背景:javaimport org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;
public void setSingleColorBackground(XWPFDocument document, String color) { XWPFParagraph paragraph=document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText(Hello World!); run.getCTR().addPPr().addBkgColor(color);}
2. 设置渐变颜色背景:javaimport org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;
public void setGradientColorBackground(XWPFDocument document, String startColor, String endColor) { XWPFParagraph paragraph=document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText(Hello World!); run.getCTR().addPPr().addBkgColor(startColor, endColor);}
3. 设置图片背景:javaimport org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;
public void setImageBackground(XWPFDocument document, String imagePath) { XWPFParagraph paragraph=document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText(Hello World!); run.getCTR().addPPr().addBkgImage(imagePath);}
前言
Word中可以针对不同文档排版设计要求来设置背景设置颜色。常见的可设置单一颜色、渐变色或加载图片来设置成背景。下面通过Java来设置以上3种Word页面背景色。
使用工具:Spire.Doc for Java v2.2.0
Jar文件导入方法
方法1:通过官网下载。在程序下新建一个directory目录,并命名(本示例中命名为lib);将控件包中lib文件夹下的Spire.Doc.jar(如下图1)直接复制到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”Add as Library”。完成导入(如下图2)。
图1:
图2:
方法2:通过maven导入。参考导入方法。
Java代码示例(供参考)
添加单一颜色的背景色
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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

