通過Java設(shè)置Word頁(yè)面背景色過程詳解
前言
Word中可以針對(duì)不同文檔排版設(shè)計(jì)要求來設(shè)置背景設(shè)置顏色。常見的可設(shè)置單一顏色、漸變色或加載圖片來設(shè)置成背景。下面通過Java來設(shè)置以上3種Word頁(yè)面背景色。
使用工具:Spire.Doc for Java v2.2.0
Jar文件導(dǎo)入方法
方法1:通過官網(wǎng)下載。在程序下新建一個(gè)directory目錄,并命名(本示例中命名為lib);將控件包中l(wèi)ib文件夾下的Spire.Doc.jar(如下圖1)直接復(fù)制到程序中新建的目錄下。復(fù)制jar文件后,鼠標(biāo)右鍵點(diǎn)擊jar文件,選擇”Add as Library”。完成導(dǎo)入(如下圖2)。
圖1:
圖2:
方法2:通過maven導(dǎo)入。參考導(dǎo)入方法。
Java代碼示例(供參考)
【示例1】添加單一顏色的背景色
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{ //加載測(cè)試文 String input="test.docx"; String output="backgroundcolor.docx"; Document doc = new Document(input); //設(shè)置單色背景 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{ //加載測(cè)試文檔 String input= "test.docx"; String output="GradientBackgound.docx"; Document doc = new Document(input); //設(shè)置漸變色 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】加載圖片設(shè)置成背景
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); //設(shè)置圖片背景 doc.getBackground().setType(BackgroundType.Picture); doc.getBackground().setPicture(img); //保存文檔 doc.saveToFile(output, FileFormat.Docx); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus 自定義 Service Vo接口實(shí)現(xiàn)數(shù)據(jù)庫(kù)實(shí)體與 vo
這篇文章主要介紹了mybatis-plus 自定義 Service Vo接口實(shí)現(xiàn)數(shù)據(jù)庫(kù)實(shí)體與 vo 對(duì)象轉(zhuǎn)換返回功能,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08項(xiàng)目打包成jar后包無法讀取src/main/resources下文件的解決
本文主要介紹了項(xiàng)目打包成jar后包無法讀取src/main/resources下文件的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04從零搭建腳手架之集成Spring?Retry實(shí)現(xiàn)失敗重試和熔斷器模式(實(shí)戰(zhàn)教程)
在我們的大多數(shù)項(xiàng)目中,會(huì)有一些場(chǎng)景需要重試操作,而不是立即失敗,讓系統(tǒng)更加健壯且不易發(fā)生故障,這篇文章主要介紹了從零搭建開發(fā)腳手架之集成Spring?Retry實(shí)現(xiàn)失敗重試和熔斷器模式,需要的朋友可以參考下2022-07-07Java實(shí)現(xiàn)在線考試系統(tǒng)與設(shè)計(jì)(學(xué)生功能)
這篇文章主要介紹了Java實(shí)現(xiàn)在線考試系統(tǒng)與設(shè)計(jì)(學(xué)生功能),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02