Search in sources :

Example 6 with HeaderPart

use of org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart in project docx4j-template by vindell.

the class Docx4J_例子2 method createHeaderPart.

// 图片页眉
public Relationship createHeaderPart(WordprocessingMLPackage wordprocessingMLPackage, MainDocumentPart t, ObjectFactory factory, boolean isUnderLine, String underLineSize) throws Exception {
    HeaderPart headerPart = new HeaderPart();
    Relationship rel = t.addTargetPart(headerPart);
    // After addTargetPart, so image can be added properly
    headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, factory, headerPart, isUnderLine, underLineSize));
    return rel;
}
Also used : Relationship(org.docx4j.relationships.Relationship) HeaderPart(org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart)

Example 7 with HeaderPart

use of org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart in project docx4j-template by vindell.

the class Docx4jTest method createHeader.

/**
 * 创建页眉
 *
 * @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;
}
Also used : Relationship(org.docx4j.relationships.Relationship) HeaderPart(org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart)

Example 8 with HeaderPart

use of org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart in project docx4j-template by vindell.

the class Docx4jTest method main.

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("http://www.mybatis.tk"));
    word.getMainDocumentPart().getContent().add(newText("http://blog.csdn.net/isea533"));
    word.getMainDocumentPart().getContent().add(newImage(word, word.getMainDocumentPart(), imageFilePath));
    // 保存
    word.save(new File("d:/1.docx"));
}
Also used : FooterPart(org.docx4j.openpackaging.parts.WordprocessingML.FooterPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) HeaderPart(org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart)

Aggregations

HeaderPart (org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart)8 Relationship (org.docx4j.relationships.Relationship)7 File (java.io.File)1 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)1 FooterPart (org.docx4j.openpackaging.parts.WordprocessingML.FooterPart)1