700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 使用spire.doc for java为word添加页码 文字水印 书签(去除警告信息)

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

时间:2019-09-10 23:10:02

相关推荐

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

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

spire.doc的依赖

<repositories><repository><id>com.e-iceblue</id><url>http://repo.e-/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>

工具类

导入的包

package mon.util;import com.spire.doc.*;import com.spire.doc.documents.HorizontalAlignment;import com.spire.doc.documents.Paragraph;import com.spire.doc.documents.TextSelection;import com.spire.doc.documents.WatermarkLayout;import com.spire.doc.fields.TextRange;import lombok.extern.slf4j.Slf4j;import org.apache.poi.hwpf.HWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFDocument;import java.awt.*;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.List;

工具类

@Slf4jpublic class WordUtil {

插入水印的方法

private static void insertTextWatermark(String dName,String Str) throws Exception {Document document = new Document();document.loadFromFile(dName);Section section=document.getSections().get(0);TextWatermark txtWatermark = new TextWatermark();txtWatermark.setText(Str);txtWatermark.setFontSize(40);txtWatermark.setColor(Color.RED);txtWatermark.setLayout(WatermarkLayout.Diagonal);section.getDocument().setWatermark(txtWatermark);document.saveToFile(dName, FileFormat.Docx_);clean(dName);}

插入页码的方法

public static void insertHeaderFooter(String dName) throws Exception {com.spire.doc.Document doc = new com.spire.doc.Document(dName);//清除页码doc.getSections().get(1).getHeadersFooters().getFooter().getChildObjects().clear();//添加页码到第一节//获取第一个节中的页脚HeaderFooter footer = doc.getSections().get(1).getHeadersFooters().getFooter();//添加段落到页脚Paragraph footerParagraph = footer.addParagraph();//添加文字、页码域和总页数域到段落footerParagraph.appendField("page number", FieldType.Field_Page);int pageCount = 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_);clean(dName);}

插入书签的方法

public static void insertBookMarks(String dName, List<String> stringList) throws Exception {for (String str : stringList) {insertBookmarkToCharacter(dName, str);}}public static void insertBookmarkToCharacter(String dName, String str) throws Exception {//加载文档com.spire.doc.Document doc = new com.spire.doc.Document();doc.loadFromFile(dName);if (str.equals(REPORT)) {//获取第三节com.spire.doc.Section section = doc.getSections().get(2);//将指定名称的书签插入指定段落section.getParagraphs().get(0).appendBookmarkStart(str);section.getParagraphs().get(2).appendBookmarkEnd(str);} else {//查找指定字符串TextSelection textSelection = doc.findString(str, false, false);TextRange range = textSelection.getAsOneRange();com.spire.doc.documents.Paragraph para = range.getOwnerParagraph();int index = para.getChildObjects().indexOf(range);//添加书签com.spire.doc.BookmarkStart start = new com.spire.doc.BookmarkStart(doc, str);com.spire.doc.BookmarkEnd end = new com.spire.doc.BookmarkEnd(doc, str);para.getChildObjects().insert(index, start);para.getChildObjects().insert(index + 2, end);}//保存文档doc.saveToFile(dName, FileFormat.Docx_);// doc.dispose();clean(dName);}

清除警告的方法

public static void clean(String dName) throws Exception {InputStream inputStream = null;String out_path = dName;//加载Word文档com.spire.doc.Document document = new com.spire.doc.Document(dName);if (dName.endsWith(".doc")) {inputStream = new FileInputStream(out_path);HWPFDocument hwpfDocument = new HWPFDocument(inputStream);//以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告//hwpfDocument.delete() 该方法去掉文档指定长度的内容hwpfDocument.delete(0, 70);//输出word内容文件流,输出路径位置OutputStream os = new FileOutputStream(out_path);try {hwpfDocument.write(os);} catch (Exception e) {e.printStackTrace();} finally {hwpfDocument.close();os.close();inputStream.close();}} else if (dName.endsWith(".docx")) {inputStream = new FileInputStream(dName);XWPFDocument old_document = new XWPFDocument(inputStream);//以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告old_document.removeBodyElement(0);//输出word内容文件流,输出路径位置OutputStream os = new FileOutputStream(dName);try {old_document.write(os);} catch (Exception e) {e.printStackTrace();} finally {document.close();os.close();inputStream.close();}}}

实现

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

}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。