docx4j官方提供了一些例子 - v3.2.2,本文隻是其中一部分應用的簡單例子。
需要注意的地方是頁眉和頁腳,必須建立對應關系才能起作用。
頁眉和頁腳添加圖檔的時候,調用的
createImagePart
方法必須用包含
sourcePart
參數的,帶這個參數的方法會建立圖檔的對應關系,否則就會出現頁眉頁腳看不到圖檔的情況。
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.jaxb.Context;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.*;
import java.io.File;
import java.util.List;
/**
* @author liuzh
*/
public class Docx4jTest {
public static void main(String[] args) throws Exception {
//建立一個word
//讀取可以使用WordprocessingMLPackage.load方法
WordprocessingMLPackage word = WordprocessingMLPackage.createPackage();
String imageFilePath = Docx4jTest.class.getResource("/lw.jpg").getPath();
//建立頁眉
HeaderPart headerPart = createHeader(word);
//頁眉添加圖檔
headerPart.getContent().add(newImage(word, headerPart, imageFilePath));
//建立頁腳
FooterPart footerPart = createFooter(word);
//添加圖檔
footerPart.getContent().add(newImage(word, footerPart, imageFilePath));
//主内容添加文本和圖檔
word.getMainDocumentPart().getContent().add(newText("https://mybatis.io"));
word.getMainDocumentPart().getContent().add(newText(");
word.getMainDocumentPart().getContent().add(
newImage(word, word.getMainDocumentPart(), imageFilePath));
//儲存
word.save(new File("d:/1.docx"));
}
private static final ObjectFactory factory = Context.getWmlObjectFactory();
/**
* 建立一段文本
*
* @param text
* @return
*/
public static P newText(String text){
P p = factory.createP();
R r = factory.createR();
Text t = new Text();
t.setValue(text);
r.getContent().add(t);
p.getContent().add(r);
return p;
}
/**
* 建立包含圖檔的内容
*
* @param word
* @param sourcePart
* @param imageFilePath
* @return
* @throws Exception
*/
public static P newImage(WordprocessingMLPackage word,
Part sourcePart,
String imageFilePath) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
.createImagePart(word, sourcePart, new File(imageFilePath));
//随機數ID
int id = (int) (Math.random() * 10000);
//這裡的id不重複即可
Inline inline = imagePart.createImageInline("image", "image", id, id * 2, false);
Drawing drawing = factory.createDrawing();
drawing.getAnchorOrInline().add(inline);
R r = factory.createR();
r.getContent().add(drawing);
P p = factory.createP();
p.getContent().add(r);
return p;
}
/**
* 建立頁眉
*
* @param word
* @return
* @throws Exception
*/
public static HeaderPart createHeader(
WordprocessingMLPackage word) throws Exception {
HeaderPart headerPart = new HeaderPart();
Relationship rel = word.getMainDocumentPart().addTargetPart(headerPart);
createHeaderReference(word, rel);
return headerPart;
}
/**
* 建立頁眉引用關系
*
* @param word
* @param relationship
* @throws InvalidFormatException
*/
public static void createHeaderReference(
WordprocessingMLPackage word,
Relationship relationship )
throws InvalidFormatException {
List<SectionWrapper> sections = word.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();
word.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
/**
* 建立頁腳
*
* @param word
* @return
* @throws Exception
*/
public static FooterPart createFooter(WordprocessingMLPackage word) throws Exception {
FooterPart footerPart = new FooterPart();
Relationship rel = word.getMainDocumentPart().addTargetPart(footerPart);
createFooterReference(word, rel);
return footerPart;
}
/**
* 建立頁腳引用關系
*
* @param word
* @param relationship
* @throws InvalidFormatException
*/
public static void createFooterReference(
WordprocessingMLPackage word,
Relationship relationship )
throws InvalidFormatException {
List<SectionWrapper> sections = word.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();
word.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
FooterReference footerReference = factory.createFooterReference();
footerReference.setId(relationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
}
}