org.docx4j.wml.Hdr Java Examples

The following examples show how to use org.docx4j.wml.Hdr. 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: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, boolean isUnderLine,
		String underLineSize) throws Exception {
	Hdr hdr = factory.createHdr();
	File file = new File("f:/saveFile/tmp/xxt.jpg");
	java.io.InputStream is = new java.io.FileInputStream(file);
	hdr.getContent().add(
			newImage(wordprocessingMLPackage, factory, sourcePart,
					BufferUtil.getBytesFromInputStream(is), "filename",
					"这是页眉部分", 1, 2, isUnderLine, underLineSize,
					JcEnumeration.CENTER));
	if (isUnderLine) {
		hdr.getContent().add(
				createHeaderBlankP(wordprocessingMLPackage, factory,
						underLineSize, JcEnumeration.CENTER));
	}
	return hdr;
}
 
Example #2
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, String content,
		JcEnumeration jcEnumeration) throws Exception {
	Hdr hdr = factory.createHdr();
	P headP = factory.createP();
	Text text = factory.createText();
	text.setValue(content);
	R run = factory.createR();
	run.getContent().add(text);
	headP.getContent().add(run);

	PPr pPr = headP.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	headP.setPPr(pPr);
	hdr.getContent().add(headP);
	return hdr;
}
 
Example #3
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,  
        ObjectFactory factory, Part sourcePart, String content,  
        JcEnumeration jcEnumeration) throws Exception {  
    Hdr hdr = factory.createHdr();  
    P headP = factory.createP();  
    Text text = factory.createText();  
    text.setValue(content);  
    R run = factory.createR();  
    run.getContent().add(text);  
    headP.getContent().add(run);  
  
    PPr pPr = headP.getPPr();  
    if (pPr == null) {  
        pPr = factory.createPPr();  
    }  
    Jc jc = pPr.getJc();  
    if (jc == null) {  
        jc = new Jc();  
    }  
    jc.setVal(jcEnumeration);  
    pPr.setJc(jc);  
      
    PBdr pBdr=pPr.getPBdr();  
    if(pBdr==null){  
        pBdr=factory.createPPrBasePBdr();  
    }  
      
    CTBorder value=new CTBorder();  
    value.setVal(STBorder.SINGLE);  
    value.setColor("000000");  
    value.setSpace(new BigInteger("0"));  
    value.setSz(new BigInteger("3"));  
    pBdr.setBetween(value);  
    pPr.setPBdr(pBdr);  
    headP.setPPr(pPr);  
    setParagraphSpacing(factory, headP, jcEnumeration, "0", "0");  
    hdr.getContent().add(headP);  
    hdr.getContent().add(createHeaderBlankP(wordprocessingMLPackage, factory, jcEnumeration));  
    return hdr;  
}
 
Example #4
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,  
        ObjectFactory factory, Part sourcePart) throws Exception {  
    Hdr hdr = factory.createHdr();  
    File file = new File("f:/saveFile/tmp/xxt.jpg");  
    java.io.InputStream is = new java.io.FileInputStream(file);  
    hdr.getContent().add(  
            newImage(wordprocessingMLPackage, factory, sourcePart,  
                    BufferUtil.getBytesFromInputStream(is), "filename",  
                    "这是页眉部分", 1, 2, JcEnumeration.CENTER));  
    hdr.getContent().add(createHeaderBlankP(wordprocessingMLPackage, factory,JcEnumeration.CENTER));  
    return hdr;  
}
 
Example #5
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart) throws Exception {
	Hdr hdr = factory.createHdr();
	File file = new File("f:/saveFile/tmp/xxt.jpg");
	java.io.InputStream is = new java.io.FileInputStream(file);
	hdr.getContent().add(
			newImage(wordprocessingMLPackage, factory, sourcePart,
					BufferUtil.getBytesFromInputStream(is), "filename",
					"这是页眉部分", 1, 2, JcEnumeration.CENTER));
	return hdr;
}
 
Example #6
Source File: Conversion.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    try {
        Rectangle page = document.getPageSize();

        if (headerFooterPolicy.getHeader(writer.getPageNumber())!=null) {
         Hdr hdr = headerFooterPolicy.getHeader(writer.getPageNumber()).getJaxbElement();
         PdfPTable head = new PdfPTable(1); // num cols	            
         // TODO - no cell borders
         traverseBlockLevelContent( hdr.getEGBlockLevelElts(), head);
         head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
         head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(),
             writer.getDirectContent());
        }
        
        if (headerFooterPolicy.getFooter(writer.getPageNumber())!=null) {
         Ftr ftr = headerFooterPolicy.getFooter(writer.getPageNumber()).getJaxbElement();	            
         PdfPTable foot = new PdfPTable(1);
         traverseBlockLevelContent( ftr.getEGBlockLevelElts(), foot);
         foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
         foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
             writer.getDirectContent());
        }
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
 
Example #7
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 #8
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 #9
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, String content,
		boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
		throws Exception {
	Hdr hdr = factory.createHdr();
	P headP = factory.createP();
	Text text = factory.createText();
	text.setValue(content);
	R run = factory.createR();
	run.getContent().add(text);
	headP.getContent().add(run);

	PPr pPr = headP.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);

	if (isUnderLine) {
		PBdr pBdr = pPr.getPBdr();
		if (pBdr == null) {
			pBdr = factory.createPPrBasePBdr();
		}
		CTBorder value = new CTBorder();
		value.setVal(STBorder.SINGLE);
		value.setColor("000000");
		value.setSpace(new BigInteger("0"));
		value.setSz(new BigInteger(underLineSz));
		pBdr.setBetween(value);
		pPr.setPBdr(pBdr);
		headP.setPPr(pPr);
	}
	setParagraphSpacing(factory, headP, jcEnumeration, true, "0", "0",
			null, null, true, "240", STLineSpacingRule.AUTO);
	hdr.getContent().add(headP);
	if (isUnderLine) {
		hdr.getContent().add(
				createHeaderBlankP(wordprocessingMLPackage, factory,
						underLineSz, jcEnumeration));
	}
	return hdr;
}