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();
if (firstHeader != null) {
handleHeader(firstHeader, content);
}
HeaderPart headerPart = headerFooterPolicy.getDefaultHeader();
handleHeader(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)不懂请留言