org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart Java Examples

The following examples show how to use org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Docx4jTest.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
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"));
}
 
Example #2
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 6 votes vote down vote up
public void walk() {

        RelationshipsPart relationshipsPart = document.getMainDocumentPart().getRelationshipsPart();

        // walk through elements in headers
        List<Relationship> headerRelationships = getRelationshipsOfType(document, Namespaces.HEADER);
        for (Relationship header : headerRelationships) {
            HeaderPart headerPart = (HeaderPart) relationshipsPart.getPart(header.getId());
            walkContent(headerPart.getContent());
        }

        // walk through elements in main document part
        walkContent(document.getMainDocumentPart().getContent());

        // walk through elements in headers
        List<Relationship> footerRelationships = getRelationshipsOfType(document, Namespaces.FOOTER);
        for (Relationship footer : footerRelationships) {
            FooterPart footerPart = (FooterPart) relationshipsPart.getPart(footer.getId());
            walkContent(footerPart.getContent());
        }
    }
 
Example #3
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createTextHeaderPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, String content,
		boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
		throws Exception {
	HeaderPart headerPart = new HeaderPart();
	Relationship rel = t.addTargetPart(headerPart);
	headerPart.setJaxbElement(getTextHdr(wordprocessingMLPackage, factory,
			headerPart, content, isUnderLine, underLineSz, jcEnumeration));
	return rel;
}
 
Example #4
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: Docx4jTest.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * 创建页眉
 *
 * @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;
}
 
Example #6
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createTextHeaderPart(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory, String content,  
        JcEnumeration jcEnumeration) throws Exception {  
    HeaderPart headerPart = new HeaderPart();  
    Relationship rel = t.addTargetPart(headerPart);  
    headerPart.setJaxbElement(getTextHdr(wordprocessingMLPackage, factory,  
            headerPart, content, jcEnumeration));  
    return rel;  
}
 
Example #7
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createHeaderPart(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory) 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));  
    return rel;  
}
 
Example #8
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createTextHeaderPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, String content,
		JcEnumeration jcEnumeration) throws Exception {
	HeaderPart headerPart = new HeaderPart();
	Relationship rel = t.addTargetPart(headerPart);
	headerPart.setJaxbElement(getTextHdr(wordprocessingMLPackage, factory,
			headerPart, content, jcEnumeration));
	return rel;
}
 
Example #9
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createHeaderPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory) 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));
	return rel;
}
 
Example #10
Source File: WordProcessor.java    From kbase-doc with Apache License 2.0 4 votes vote down vote up
private Relationship createHeaderPart(String nameSuffix) throws Docx4JException, IOException, JAXBException {
	HeaderPart headerPart = new HeaderPart(new PartName("/word/header-" + nameSuffix + ".xml"));
	Relationship rel =  wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
	setHdr(headerPart);
	return rel;
}
 
Example #11
Source File: WordProcessor.java    From kbase-doc with Apache License 2.0 4 votes vote down vote up
private void setHdr(HeaderPart headerPart) throws Docx4JException, IOException, JAXBException  {
	ImagePngPart imagePart = new ImagePngPart(new PartName("/media/background.png"));
	try (InputStream in = new FileInputStream(imageFile)){
		imagePart.setBinaryData(in);
	}
	Relationship rel = headerPart.addTargetPart(imagePart, AddPartBehaviour.REUSE_EXISTING); // the one image is shared by the 3 header parts

	String openXML = "<w:hdr mc:Ignorable=\"w14 wp14\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
			+ "<w:p>"
			+ "<w:pPr>"
			+ "<w:pStyle w:val=\"Header\"/>"
			+"</w:pPr>"
			+ "<w:r>"
			+ "<w:rPr>"
			+ "<w:noProof/>"
			+"</w:rPr>"
			+ "<w:pict>"
			+ "<v:shapetype coordsize=\"21600,21600\" filled=\"f\" id=\"_x0000_t75\" o:preferrelative=\"t\" o:spt=\"75\" path=\"m@4@5l@4@11@9@11@9@5xe\" stroked=\"f\">"
			+ "<v:stroke joinstyle=\"miter\"/>"
			+ "<v:formulas>"
			+ "<v:f eqn=\"if lineDrawn pixelLineWidth 0\"/>"
			+ "<v:f eqn=\"sum @0 1 0\"/>"
			+ "<v:f eqn=\"sum 0 0 @1\"/>"
			+ "<v:f eqn=\"prod @2 1 2\"/>"
			+ "<v:f eqn=\"prod @3 21600 pixelWidth\"/>"
			+ "<v:f eqn=\"prod @3 21600 pixelHeight\"/>"
			+ "<v:f eqn=\"sum @0 0 1\"/>"
			+ "<v:f eqn=\"prod @6 1 2\"/>"
			+ "<v:f eqn=\"prod @7 21600 pixelWidth\"/>"
			+ "<v:f eqn=\"sum @8 21600 0\"/>"
			+ "<v:f eqn=\"prod @7 21600 pixelHeight\"/>"
			+ "<v:f eqn=\"sum @10 21600 0\"/>"
			+"</v:formulas>"
			+ "<v:path gradientshapeok=\"t\" o:connecttype=\"rect\" o:extrusionok=\"f\"/>"
			+ "<o:lock aspectratio=\"t\" v:ext=\"edit\"/>"
			+"</v:shapetype>"
			+ "<v:shape id=\"WordPictureWatermark835936646\" o:allowincell=\"f\" o:spid=\"_x0000_s2050\" style=\"position:absolute;margin-left:0;margin-top:0;width:640px;height:400px;z-index:-251657216;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin\" type=\"#_x0000_t75\">"
			+ "<v:imagedata blacklevel=\"22938f\" gain=\"19661f\" o:title=\"docx4j-logo\" r:id=\"" + rel.getId() + "\"/>"
			+"</v:shape>"
			+"</w:pict>"
			+"</w:r>"
			+"</w:p>"
			+"</w:hdr>";

	Hdr hdr = (Hdr)XmlUtils.unmarshalString(openXML);
	headerPart.setJaxbElement(hdr);
}
 
Example #12
Source File: WatermarkPicture.java    From kbase-doc with Apache License 2.0 4 votes vote down vote up
private void setHdr(HeaderPart headerPart) throws Exception  {

ImagePngPart imagePart = new ImagePngPart(new PartName("/media/background.png"));
imagePart.setBinaryData(image);
Relationship rel = headerPart.addTargetPart(imagePart, AddPartBehaviour.REUSE_EXISTING); // the one image is shared by the 3 header parts

String openXML = "<w:hdr mc:Ignorable=\"w14 wp14\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
           + "<w:p>"
               + "<w:pPr>"
                   + "<w:pStyle w:val=\"Header\"/>"
               +"</w:pPr>"
               + "<w:r>"
                   + "<w:rPr>"
                       + "<w:noProof/>"
                   +"</w:rPr>"
                   + "<w:pict>"
                       + "<v:shapetype coordsize=\"21600,21600\" filled=\"f\" id=\"_x0000_t75\" o:preferrelative=\"t\" o:spt=\"75\" path=\"m@4@5l@4@11@9@11@9@5xe\" stroked=\"f\">"
                           + "<v:stroke joinstyle=\"miter\"/>"
                           + "<v:formulas>"
                               + "<v:f eqn=\"if lineDrawn pixelLineWidth 0\"/>"
                               + "<v:f eqn=\"sum @0 1 0\"/>"
                               + "<v:f eqn=\"sum 0 0 @1\"/>"
                               + "<v:f eqn=\"prod @2 1 2\"/>"
                               + "<v:f eqn=\"prod @3 21600 pixelWidth\"/>"
                               + "<v:f eqn=\"prod @3 21600 pixelHeight\"/>"
                               + "<v:f eqn=\"sum @0 0 1\"/>"
                               + "<v:f eqn=\"prod @6 1 2\"/>"
                               + "<v:f eqn=\"prod @7 21600 pixelWidth\"/>"
                               + "<v:f eqn=\"sum @8 21600 0\"/>"
                               + "<v:f eqn=\"prod @7 21600 pixelHeight\"/>"
                               + "<v:f eqn=\"sum @10 21600 0\"/>"
                           +"</v:formulas>"
                           + "<v:path gradientshapeok=\"t\" o:connecttype=\"rect\" o:extrusionok=\"f\"/>"
                           + "<o:lock aspectratio=\"t\" v:ext=\"edit\"/>"
                       +"</v:shapetype>"
                       + "<v:shape id=\"WordPictureWatermark835936646\" o:allowincell=\"f\" o:spid=\"_x0000_s2050\" style=\"position:absolute;margin-left:0;margin-top:0;width:467.95pt;height:615.75pt;z-index:-251657216;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin\" type=\"#_x0000_t75\">"
                           + "<v:imagedata blacklevel=\"22938f\" gain=\"19661f\" o:title=\"docx4j-logo\" r:id=\"" + rel.getId() + "\"/>"
                       +"</v:shape>"
                   +"</w:pict>"
               +"</w:r>"
           +"</w:p>"
       +"</w:hdr>";

	Hdr hdr = (Hdr)XmlUtils.unmarshalString(openXML);
	
	headerPart.setJaxbElement(hdr);

}
 
Example #13
Source File: ExpressionReplacementInHeaderAndFooterTest.java    From docx-stamper with MIT License 4 votes vote down vote up
private void resolvedExpressionsAreReplacedInHeader(WordprocessingMLPackage document) {
    HeaderPart headerPart = getHeaderPart(document);

    P nameParagraph = (P) headerPart.getContent().get(1);
    Assert.assertEquals("In this paragraph, the variable name should be resolved to the value Homer Simpson.", new ParagraphWrapper(nameParagraph).getText());
}
 
Example #14
Source File: ExpressionReplacementInHeaderAndFooterTest.java    From docx-stamper with MIT License 4 votes vote down vote up
private void unresolvedExpressionsAreNotReplacedInHeader(WordprocessingMLPackage document) {
    HeaderPart headerPart = getHeaderPart(document);

    P fooParagraph = (P) headerPart.getContent().get(2);
    Assert.assertEquals("In this paragraph, the variable foo should not be resolved: ${foo}.", new ParagraphWrapper(fooParagraph).getText());
}
 
Example #15
Source File: ExpressionReplacementInHeaderAndFooterTest.java    From docx-stamper with MIT License 4 votes vote down vote up
private HeaderPart getHeaderPart(WordprocessingMLPackage document) {
    RelationshipsPart relPart = document.getMainDocumentPart().getRelationshipsPart();
    Relationship rel = relPart.getRelationshipByType(Namespaces.HEADER);
    return (HeaderPart) relPart.getPart(rel);
}
 
Example #16
Source File: WatermarkPicture.java    From kbase-doc with Apache License 2.0 3 votes vote down vote up
private Relationship createHeaderPart(String nameSuffix) throws Exception {
	
	HeaderPart headerPart = new HeaderPart(new PartName("/word/header-" + nameSuffix + ".xml"));
	Relationship rel =  wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
	
	setHdr( headerPart);

	return rel;
}