700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Docx4j替换word文档的页眉

Docx4j替换word文档的页眉

时间:2023-05-15 11:29:09

相关推荐

Docx4j替换word文档的页眉

Docx4j替换word文档的页眉

目前国内关于Docx4j的帖子少的可怜,看来看去都是那几个。本人使用Docx4j也有一段时间,虽然还有许多东西没了解全,但在这边想分享我的学习经验,互相交流,也方便自己以后回顾。

1、若想了解如何为word插入页眉页脚,请百度(一大堆都是)。

2、在看该文章之前需要大致了解下什么是Docx4j,是操作哪一种版本的word。需要对XML有一点了解。

3、废话不说,看代码。

(1)为方便,写一个抽象类

public abstract class Docx4jAbstract {protected WordprocessingMLPackage wordMLPackage;protected MainDocumentPart documentPart;public AbstractOp(WordprocessingMLPackage wordMLPackage) {this.wordMLPackage = wordMLPackage;this.documentPart = wordMLPackage.getMainDocumentPart();}}

(2)主要内容

public class HeaderAndFooter extends Docx4jAbstract {private boolean revision = false;//是否需要修订记录private String author = "";//修订记录的作者public HeaderFooterOp(WordprocessingMLPackage wordMLPackage) {super(wordMLPackage);}public void setRevision (boolean revision ) {this.revision = revision ;}public void setAuthor(String author) {this.author = author;}/*** 替换页眉** @param content 要替换的内容*/public void replaceHeader(String content) throws Exception {/*** .docx文件里面其实包含了三个header(header1、header2、header3)* 其中有一个header为首页不同的header(为什么是其中一个呢?因为不同文档header存放的内容并不是固定的)* 这边主要思路是找到header,将里面的P(段落)内的内容替换掉。*/List<SectionWrapper> sections = this.wordMLPackage.getDocumentModel().getSections();if (null == sections || 0 == sections.size()) return;for (int i = 0; i < sections.size(); i++) {HeaderFooterPolicy headerFooterPolicy = sections.get(i).getHeaderFooterPolicy();HeaderPart firstHeader = headerFooterPolicy.getFirstHeader();//首页不同的页眉的headerif (firstHeader != null) {handleHeader(firstHeader, content);}HeaderPart headerPart = headerFooterPolicy.getDefaultHeader();//普通情况的页眉headerhandleHeader(headerPart, content);}}private void handleHeader(HeaderPart headerPart, String content) throws Exception {List<Object> headList = headerPart.getContent();for (Object headP : headList) {if (headP instanceof P) {P srcP = (P) headP;//获取文档中的页眉deleteHeadRevision(headerPart, srcP);//删除原先的修订记录String srcContent = extractText(srcP);//获取页眉的内容P targetP = revision ? createDelAndInsP(srcContent, content, author, srcP.getPPr()): DocxUtils.createP(content, srcP.getPPr());//生成新的段落int index = headerPart.getContent().indexOf(srcP);//获取索引headerPart.getContent().remove(index);//删除原先的页眉headerPart.getContent().add(index, targetP);//插入新的页眉}}}public void deleteHeadRevision(HeaderPart headerPart, P p) throws Exception {List<Object> objList = headerPart.getContent();deleteDocxRevision(objList,p);}public String extractText(Object object) {StringWriter stringWriter = new StringWriter();try {stringWriter.getBuffer().setLength(0);TextUtils.extractText(object, stringWriter);} catch (Exception e) {e.printStackTrace();}return stringWriter.getBuffer().toString();}public P createDelAndInsP(String srcContent, String replaceContent, String author, PPr ppr) {P pDelAndIns = createDelAndInsP(srcContent, replaceContent, author);pDelAndIns.setPPr(ppr);return pDelAndIns;}public P createDelAndInsP(String srcContent, String replaceContent, String author) {ObjectFactory factory = Context.getWmlObjectFactory();P pDelAndIns = factory.createP();insetIns(pDelAndIns, replaceContent, author);insetDel(pDelAndIns, srcContent, author);return pDelAndIns;}public void insetIns(P pIns, String content, String author) {RunIns Ins = createRunIns(content, author);pIns.getContent().add(Ins);}public RunIns createRunIns(String content, String author) {ObjectFactory factory = Context.getWmlObjectFactory();RunIns ins = factory.createRunIns();R r = createR(factory, content);ins.setAuthor(author);ins.getCustomXmlOrSmartTagOrSdt().add(r);return ins;}public void insetDel(P pDel, String content, String author) {RunDel Del = createRunDel(content, author);pDel.getContent().add(Del);}public RunDel createRunDel(String content, String author) {ObjectFactory factory = Context.getWmlObjectFactory();RunDel del = factory.createRunDel();R rDel = factory.createR();DelText delText = factory.createDelText();delText.setValue(content);rDel.getContent().add(delText);del.setAuthor(author);del.getCustomXmlOrSmartTagOrSdt().add(rDel);return del;}public P createP(String content, PPr ppr) {P p = createP(content);p.setPPr(ppr);return p;}public P createP(String content) {ObjectFactory factory = Context.getWmlObjectFactory();P p = factory.createP();R r = createR(content);p.getContent().add(r);return p;}}

(3)不懂请留言

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