Office 2007 之后使用 XML 格式来存储文档数据,这个变化其实是非常不错的。如何操作这些XML生成文件呢?或许你还在使用 POI,也有可能你在使用已经放弃更新的 iText (只是doc已经放弃更新,PDF持续更新),但是我觉得,你可以选择使用 Docx4j,这是个不错的开源软件,国内很少使用而已。
下面就简单的使用 docx4j添加页码的代码做一个简单的介绍。
/**
* 创建页脚(分页信息)
*/
public static void createFooter(WordprocessingMLPackage wordMLPackage) throws Exception {
Relationship relationship = DocxUtil.createFootPart(wordMLPackage);
DocxUtil.createFootReference(wordMLPackage, relationship);
}
/**
* 创建页脚的组件
*/
public static Relationship createFootPart(
WordprocessingMLPackage wordprocessingMLPackage)
throws Exception {
FooterPart footerPart = new FooterPart();
Relationship rel = wordprocessingMLPackage.getMainDocumentPart()
.addTargetPart(footerPart);
// After addTargetPart, so image can be added properly
footerPart.setJaxbElement(createFooterPageNr());
return rel;
}
/**
* 创建页脚的组件
*/
public static void createFootReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship)
throws InvalidFormatException {
ObjectFactory factory = Context.getWmlObjectFactory();
List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = factory.createSectPr();
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
// footer references
FooterReference footerReference = factory.createFooterReference();
footerReference.setId(relationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
}
/**
* 添加页脚,带有页码
*
* @return 页脚对象
*/
public static Ftr createFooterPageNr() {
ObjectFactory factory = Context.getWmlObjectFactory();
Ftr ftr = factory.createFtr();
P paragraph = factory.createP();
RPr fontRPr = getRPr("宋体", "000000", "20", STHint.EAST_ASIA, false);
R run = factory.createR();
run.setRPr(fontRPr);
paragraph.getContent().add(run);
paragraph.getContent().add(getTextField("第"));
paragraph.getContent().add(getFieldBegin());
paragraph.getContent().add(getPageNumberField());
paragraph.getContent().add(getFieldEnd());
paragraph.getContent().add(getTextField("页"));
paragraph.getContent().add(getTextField(" 总共"));
paragraph.getContent().add(getFieldBegin());
paragraph.getContent().add(getTotalPageNumberField());
paragraph.getContent().add(getFieldEnd());
paragraph.getContent().add(getTextField("页"));
setCellContentStyle(paragraph, JcEnumeration.CENTER);
ftr.getContent().add(paragraph);
return ftr;
}
生成 docx 文档,这个代码来自Git
public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
// mdp.addParagraphOfText("hello world");
// mdp.addParagraphOfText("welcome");
mdp.addStyledParagraphOfText("Title","hello world");
mdp.addStyledParagraphOfText("Subtitle","welcome");
mdp.addParagraphOfText("today is good day . welcome all students come here.");
ProtectDocument protection = new ProtectDocument(wordMLPackage);
protection.restrictEditing(STDocProtect.READ_ONLY, "foobaa");
DocxUtil.createFooter(wordMLPackage);
String filename = System.getProperty("user.dir") + "/OUT_hello.docx";
Docx4J.save(wordMLPackage, new java.io.File(filename), Docx4J.FLAG_SAVE_ZIP_FILE);
System.out.println("Saved " + filename);
}
来源:oschina
链接:https://my.oschina.net/u/1989321/blog/702784