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
情況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(); } }
情況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(); } }
總結
對Word中的不同頁面設置不同背景,需要幾個重要步驟:
1. 設置文檔分節
2. 設置頁眉圖片,並調整圖片格式以鋪滿整個頁面
3. 運行程序生成文檔
同理,在設置Word水印時,默認的方法也只能生成一個水印文字效果,要實現水印平鋪的效果,也可以通過在頁眉中添加文字的方法來實現,需要的可以參考這篇文章,裏面介紹瞭如何來實現,這裏不作贅述了。