Java如何实现给Word文件添加文字水印
这篇文章主要为大家展示了“Java如何实现给Word文件添加文字水印”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java如何实现给Word文件添加文字水印”这篇文章吧。
方法思路在给Word每一页添加水印前,首先需要在Word文档每一页正文的最后一个字符后面插入“连续”分节符,然后在每一节的页眉段落里添加艺术字类型的形状对象,并设置艺术字的坐标位置、样式、对齐方式等。最后保存文档。
Jar引入在程序中引入Free Spire.Doc for Java中的Spire.Doc.jar文件(该文件在lib文件夹下);如果需要通过Maven下载导入,可进行如下配置pom.xml:
<repositories><repository><id>com.e-iceblue</id><url>https://repo.e-iceblue.cn/repository/maven-public/</url></repository></repositories><dependencies><dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>5.1.0</version></dependency></dependencies>Java代码
给每页添加图片水印时,可参考如下步骤:
创建Document类的对象,并通过Document.loadFromFile(String fileName)方法加载Word文档。
通过Document.getSections().get(int index)方法获取指定节。
通过Section.getHeadersFooters().getHeader()方法获取页眉,HeaderFooter.addParagraph()方法添加段落到页眉。
创建ShapeObject类的对象,并传入参数设置形状类型为Text_Plain_Text类型的艺术字。并调用方法设置艺术字样式,如艺术字高度、宽度、旋转、颜色、对齐方式等。
通过Paragraph.getChildObjects().add(IdocumentObject entity)方法添加艺术字到段落。
最后,通过Document.saveToFile(String fileName, FileFormat fileFormat)方法保存文档。
不同页面中设置不一样的文字水印效果,只需要获取该页面对应节的页眉段落,然后参考上述用到的方法步骤逐一添加即可。
下面是完整的Java代码示例:
importcom.spire.doc.*;importcom.spire.doc.documents.*;importcom.spire.doc.fields.ShapeObject;importjava.awt.*;publicclassDifferentTextWatermark{publicstaticvoidmain(String[]args){//加载Word测试文档Documentdoc=newDocument();doc.loadFromFile("test.docx");//获取文档第一节Sectionsection1=doc.getSections().get(0);//定义水印文字的纵向坐标位置floaty=(float)(section1.getPageSetup().getPageSize().getHeight()/3);//添加文字水印1HeaderFooterheader1=section1.getHeadersFooters().getHeader();//获取页眉header1.getParagraphs().clear();//删除原有页眉格式的段落Paragraphpara1=header1.addParagraph();//重新添加段落//添加艺术字并设置大小ShapeObjectshape1=newShapeObject(doc,ShapeType.Text_Plain_Text);shape1.setWidth(362);shape1.setHeight(118);//设置艺术字文本内容、位置及样式(即文本水印字样)shape1.setRotation(315);shape1.getWordArt().setText("内部使用");shape1.setFillColor(newColor(128,128,128));shape1.setLineStyle(ShapeLineStyle.Single);shape1.setStrokeColor(newColor(128,128,128));shape1.setStrokeWeight(0.5);shape1.setVerticalPosition(y);shape1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);para1.getChildObjects().add(shape1);//同理设置第二节页眉中的文字水印2Sectionsection2=doc.getSections().get(1);HeaderFooterheader2=section2.getHeadersFooters().getHeader();header2.getParagraphs().clear();Paragraphpara2=header2.addParagraph();ShapeObjectshape2=newShapeObject(doc,ShapeType.Text_Plain_Text);shape2.setWidth(362);shape2.setHeight(118);shape2.setRotation(315);shape2.getWordArt().setText("绝密资料");shape2.setFillColor(newColor(221,160,221));shape2.setLineStyle(ShapeLineStyle.Single);shape2.setStrokeColor(newColor(221,160,221));shape2.setStrokeWeight(0.5);shape2.setVerticalPosition(y);shape2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);para2.getChildObjects().add(shape2);//同理设置第三节中的页眉中的文字水印3Sectionsection3=doc.getSections().get(2);HeaderFooterheader3=section3.getHeadersFooters().getHeader();header3.getParagraphs().clear();Paragraphpara3=header3.addParagraph();ShapeObjectshape3=newShapeObject(doc,ShapeType.Text_Plain_Text);shape3.setWidth(362);shape3.setHeight(118);shape3.setRotation(315);shape3.getWordArt().setText("禁止传阅");shape3.setFillColor(newColor(70,130,180));shape3.setLineStyle(ShapeLineStyle.Single);shape3.setStrokeColor(newColor(70,130,180));shape3.setStrokeWeight(0.5);shape3.setVerticalPosition(y);shape3.setHorizontalAlignment(ShapeHorizontalAlignment.Center);para3.getChildObjects().add(shape3);//保存文档doc.saveToFile("DifferentTextWatermark.docx",FileFormat.Docx_2013);doc.dispose();}}
如图,每一页均可显示不同的文字水印效果:
以上是“Java如何实现给Word文件添加文字水印”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。