- Code: Select all
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator;
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.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.utils.BufferUtil;
import org.docx4j.wml.Br;
import org.docx4j.wml.FldChar;
import org.docx4j.wml.Hdr;
import org.docx4j.wml.HdrFtrRef;
import org.docx4j.wml.HeaderReference;
import org.docx4j.wml.Jc;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.PPr;
import org.docx4j.wml.STBrType;
import org.docx4j.wml.STFldCharType;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.Tabs;
import org.docx4j.wml.Text;
public class HeaderFooterCreate {
private static ObjectFactory objectFactory = new ObjectFactory();
public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
Relationship styleRel = mdp.getStyleDefinitionsPart().getSourceRelationships().get(0);
mdp.getRelationshipsPart().removeRelationship(styleRel);
createHeaderReference(wordMLPackage, relationship);
wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_Header_footer.docx"));
}
public static Relationship createHeaderPart(
WordprocessingMLPackage wordprocessingMLPackage)
throws Exception {
HeaderPart headerPart = new HeaderPart();
Relationship rel = wordprocessingMLPackage.getMainDocumentPart()
.addTargetPart(headerPart);
headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart));
return rel;
}
public static void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship )
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
if (sectPr==null ) {
sectPr = objectFactory.createSectPr();
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart) throws Exception {
Hdr hdr = objectFactory.createHdr();
File file = new File(System.getProperty("user.dir")
+ "/windows.jpg" );
java.io.InputStream is = new java.io.FileInputStream(file);
hdr.getContent().add(
newImage(wordprocessingMLPackage,
sourcePart,
BufferUtil.getBytesFromInputStream(is),
"filename", "alttext", 1, 2, 1000
)
);
return hdr;
}
public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage,
Part sourcePart,
byte[] bytes,
String filenameHint, String altText,
int id1, int id2, long cx) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, sourcePart, bytes);
Inline inline = imagePart.createImageInline( filenameHint, altText,
id1, id2, cx, false);
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.R run = factory.createR();
p.getContent().add(run);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String footer_time = df.format(new Date());
Text text = new Text();
text.setValue(footer_time);
run.getContent().add(text);
return p;
}
}
then got the following result:
but I want to add some spaces between the logo and the date string, e.g:
I tried to the following code:
- Code: Select all
Text text = new Text();
text.setValue(" " + footer_time);
run.getContent().add(text);
, but failed!
how can I modify the code in newImage() to get this, thanks a lot!