当前位置:

使用spire.doc for java为word添加页码、文字水印、书签去掉警告信息

访客 2024-02-18 1377 0

这个java工具类的作用是给word添加页码、文字水印、书签。写的时候用的是spire.docforjava。同时也去掉了组件自动生成的警告。还有组件需要的依赖也全盘奉上。

spire.doc的依赖

<repositories><repository><id>com.e-iceblue</id><url>http://repo.e-iceblue.cn/repository/maven-public/</url></repository></repositories><dependency><groupId>e-iceblue</groupId><artifactId>spire.doc</artifactId><version>5.4.2</version></dependency>

poi的依赖

<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.poi.xwpf.converter.core</artifactId><version>2.0.1</version><exclusions><exclusion><artifactId>poi</artifactId><groupId>org.apache.poi</groupId></exclusion></exclusions></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId><version>2.0.1</version></dependency>

工具类

导入的包

packagecom.ejiaoyi.common.util;importcom.spire.doc.*;importcom.spire.doc.documents.HorizontalAlignment;importcom.spire.doc.documents.Paragraph;importcom.spire.doc.documents.TextSelection;importcom.spire.doc.documents.WatermarkLayout;importcom.spire.doc.fields.TextRange;importlombok.extern.slf4j.Slf4j;importorg.apache.poi.hwpf.HWPFDocument;importorg.apache.poi.xwpf.usermodel.XWPFDocument;importjava.awt.*;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.List;

工具类

@Slf4jpublicclassWordUtil{

插入水印的方法

privatestaticvoidinsertTextWatermark(StringdName,StringStr)throwsException{Documentdocument=newDocument();document.loadFromFile(dName);Sectionsection=document.getSections().get(0);TextWatermarktxtWatermark=newTextWatermark();txtWatermark.setText(Str);txtWatermark.setFontSize(40);txtWatermark.setColor(Color.RED);txtWatermark.setLayout(WatermarkLayout.Diagonal);section.getDocument().setWatermark(txtWatermark);document.saveToFile(dName,FileFormat.Docx_2010);clean(dName);}

插入页码的方法

publicstaticvoidinsertHeaderFooter(StringdName)throwsException{com.spire.doc.Documentdoc=newcom.spire.doc.Document(dName);//清除页码doc.getSections().get(1).getHeadersFooters().getFooter().getChildObjects().clear();//添加页码到第一节//获取第一个节中的页脚HeaderFooterfooter=doc.getSections().get(1).getHeadersFooters().getFooter();//添加段落到页脚ParagraphfooterParagraph=footer.addParagraph();//添加文字、页码域和总页数域到段落footerParagraph.appendField("pagenumber",FieldType.Field_Page);intpageCount=doc.getPageCount()-1;footerParagraph.appendText("/"pageCount);//将段落居中footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);doc.getSections().get(1).getPageSetup().setRestartPageNumbering(true);doc.getSections().get(1).getPageSetup().setPageStartingNumber(1);//保存文档doc.saveToFile(dName,FileFormat.Docx_2010);clean(dName);}

插入书签的方法

publicstaticvoidinsertBookMarks(StringdName,List<String>stringList)throwsException{for(Stringstr:stringList){insertBookmarkToCharacter(dName,str);}}publicstaticvoidinsertBookmarkToCharacter(StringdName,Stringstr)throwsException{//加载文档com.spire.doc.Documentdoc=newcom.spire.doc.Document();doc.loadFromFile(dName);if(str.equals(REPORT)){//获取第三节com.spire.doc.Sectionsection=doc.getSections().get(2);//将指定名称的书签插入指定段落section.getParagraphs().get(0).appendBookmarkStart(str);section.getParagraphs().get(2).appendBookmarkEnd(str);}else{//查找指定字符串TextSelectiontextSelection=doc.findString(str,false,false);TextRangerange=textSelection.getAsOneRange();com.spire.doc.documents.Paragraphpara=range.getOwnerParagraph();intindex=para.getChildObjects().indexOf(range);//添加书签com.spire.doc.BookmarkStartstart=newcom.spire.doc.BookmarkStart(doc,str);com.spire.doc.BookmarkEndend=newcom.spire.doc.BookmarkEnd(doc,str);para.getChildObjects().insert(index,start);para.getChildObjects().insert(index2,end);}//保存文档doc.saveToFile(dName,FileFormat.Docx_2013);//doc.dispose();clean(dName);}

清除警告的方法

publicstaticvoidclean(StringdName)throwsException{InputStreaminputStream=null;Stringout_path=dName;//加载Word文档com.spire.doc.Documentdocument=newcom.spire.doc.Document(dName);if(dName.endsWith(".doc")){inputStream=newFileInputStream(out_path);HWPFDocumenthwpfDocument=newHWPFDocument(inputStream);//hwpfDocument.delete()该方法去掉文档指定长度的内容hwpfDocument.delete(0,70);//输出word内容文件流,输出路径位置OutputStreamos=newFileOutputStream(out_path);try{hwpfDocument.write(os);}catch(Exceptione){e.printStackTrace();}finally{hwpfDocument.close();os.close();inputStream.close();}}elseif(dName.endsWith(".docx")){inputStream=newFileInputStream(dName);XWPFDocumentold_document=newXWPFDocument(inputStream);//以上Spire.Doc生成的文件会自带警告信息,这里来删除Spire.Doc的警告old_document.removeBodyElement(0);//输出word内容文件流,输出路径位置OutputStreamos=newFileOutputStream(dName);try{old_document.write(os);}catch(Exceptione){e.printStackTrace();}finally{document.close();os.close();inputStream.close();}}}

实现

//实现publicstaticvoidmain(String[]args)throwsException{//水印insertTextWatermark("D:\\text.docx","xxx");//页码WordUtil.insertHeaderFooter("D:\\text.docx");//书签WordUtil.insertBookmarkToCharacter("D:\\text.docx","xxx表");ArrayList<String>list=newArrayList<>();list.add("xxxx");list.add("xxxxxx");WordUtil.insertBookMarks("D:\\Report.docx",list);}}

                        
一些小笔记,一起学习,一起交流(•̀ω•́)✧有用记得O(∩_∩)O点赞~

发表评论

  • 评论列表
还没有人评论,快来抢沙发吧~