Word文档中,可间接设置页面背景,但通过这种形式增加的页面背景只能利用于整个文档页面,如果须要只对某些页面设置不同于其余页面的背景,这种办法并不见效。因而,本文总结了可实现多个页面设置不同背景的办法。
思考到只需设置首页背景不同,或者多个页面不同背景的状况,简略分为了两种状况来介绍,然而办法都是相似的。
注:程序开发环境中须要导入jar包工具。
状况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中的不同页面设置不同背景,须要几个重要步骤:
- 设置文档分节
- 设置页眉图片,并调整图片格式以铺满整个页面
- 运行程序生成文档
同理,在设置Word水印时,默认的办法也只能生成一个水印文字效果,要实现水印平铺的成果,也能够通过在页眉中增加文字的办法来实现。
参考:《2020最新Java根底精讲视频教程和学习路线!》
链接:https://juejin.cn/post/692225...