I'm trying to get the HTML in the header but i'm not having too much luck. Can anyone please tell me what i'm doing wrong?
if i want to add the html to the body i would just
Using java Syntax Highlighting
/**
* @param html
* @param wordMLPackage
* @throws InvalidFormatException
* @throws ReportGenerationException
*/
private static void addChunk(final String html, final WordprocessingMLPackage wordMLPackage) throws InvalidFormatException, ReportGenerationException {
final AlternativeFormatInputPart part = createHTMLPart(html);
final MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
final Relationship altChunkRel = mdp.addTargetPart(part);
final CTAltChunk ac = WML_OBJECT_FACTORY.createCTAltChunk();
ac.setId(altChunkRel.getId());
mdp.addObject(ac);
}
* @param html
* @param wordMLPackage
* @throws InvalidFormatException
* @throws ReportGenerationException
*/
private static void addChunk(final String html, final WordprocessingMLPackage wordMLPackage) throws InvalidFormatException, ReportGenerationException {
final AlternativeFormatInputPart part = createHTMLPart(html);
final MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
final Relationship altChunkRel = mdp.addTargetPart(part);
final CTAltChunk ac = WML_OBJECT_FACTORY.createCTAltChunk();
ac.setId(altChunkRel.getId());
mdp.addObject(ac);
}
Parsed in 0.015 seconds, using GeSHi 1.0.8.4
But this doesn't work for the header. Here's the full code (based on Example header/footer, cover page with page numbers )
Using java Syntax Highlighting
/**
*
*/
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.List;
import javax.xml.bind.JAXBElement;
import org.docx4j.jaxb.Context;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Body;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.Br;
import org.docx4j.wml.CTSimpleField;
import org.docx4j.wml.FooterReference;
import org.docx4j.wml.Ftr;
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.P;
import org.docx4j.wml.PPr;
import org.docx4j.wml.PPrBase;
import org.docx4j.wml.R;
import org.docx4j.wml.RPr;
import org.docx4j.wml.STBrType;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.SectPr.PgMar;
import org.docx4j.wml.Text;
/**
* @author wisecream
*/
class Docx4JReportGenerator {
private static ContentType HTML_CONTENT_TYPE = new ContentType("text/html");
private static ObjectFactory WML_OBJECT_FACTORY = Context.getWmlObjectFactory();
private static BigInteger MARGIN = new BigInteger("700");
/**
* @param args
* @throws Exception
*/
public static void main(final String[] args) throws Docx4JException {
generateReport(new File("a.docx"));
}
/**
* @param html
* @return
* @throws UnsupportedEncodingException
* @throws Exception
*/
private static byte[] getBytes(final String html) throws UnsupportedEncodingException {
if (html == null) {
return new byte[0];
}
return html.getBytes("UTF-8");
}
/**
* @param body
*/
private static void addMargin(final WordprocessingMLPackage pack) {
final MainDocumentPart mainDocumentPart = pack.getMainDocumentPart();
final Body body = mainDocumentPart.getJaxbElement().getBody();
final SectPr sectPr = body.getSectPr();
final PgMar pgMar = sectPr.getPgMar();
pgMar.setLeft(MARGIN);
pgMar.setRight(MARGIN);
pgMar.setTop(MARGIN);
pgMar.setBottom(MARGIN);
}
/**
* @param out
* @throws Docx4JException
*/
public static void generateReport(final File out) throws Docx4JException {
final WordprocessingMLPackage pkg = WordprocessingMLPackage.createPackage();
createHeaderAndFooter(pkg);
final MainDocumentPart mainDocumentPart = pkg.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Normal", "First Page");
mainDocumentPart.addObject(makePageBr());
mainDocumentPart.addStyledParagraphOfText("Heading1", "Nice Page");
mainDocumentPart.addObject(makePageBr());
mainDocumentPart.addStyledParagraphOfText("Normal", "Boring page");
addMargin(pkg);
pkg.save(out);
}
/**
* @param pkg
* @throws InvalidFormatException
*/
private static void createHeaderAndFooter(final WordprocessingMLPackage pkg) throws InvalidFormatException {
final List<SectionWrapper> sections = pkg.getDocumentModel().getSections();
final SectPr sectPr = getLastSectionPart(pkg, sections);
createAndAddHeader(pkg, sectPr);
createAndAddFooter(pkg, sectPr);
}
/**
* @param pkg
* @param sections
* @return
*/
private static SectPr getLastSectionPart(final WordprocessingMLPackage pkg, final List<SectionWrapper> sections) {
final SectionWrapper lastSect = sections.get(sections.size() - 1);
final SectPr currentSectPr = lastSect.getSectPr();
if (currentSectPr != null) {
return currentSectPr;
}
final SectPr sectPr = WML_OBJECT_FACTORY.createSectPr();
pkg.getMainDocumentPart().addObject(sectPr);
lastSect.setSectPr(sectPr);
return sectPr;
}
/**
* @param pkg
* @param sectPr
* @throws InvalidFormatException
*/
private static void createAndAddFooter(final WordprocessingMLPackage pkg, final SectPr sectPr) throws InvalidFormatException {
final FooterPart content_ftr_part = new FooterPart(new PartName("/word/content-footer.xml"));
pkg.getParts().put(content_ftr_part);
//page number test
final Ftr content_ftr = getFtr();
// Bind the header JAXB elements as representing their header parts
content_ftr_part.setJaxbElement(content_ftr);
// Add the reference to both header parts to the Main Document Part
final Relationship content_ftr_rel = pkg.getMainDocumentPart().addTargetPart(content_ftr_part);
content_ftr.getContent().add(makeParagraph(content_ftr_part, "Content footer text"));
// link cover and content footers
final FooterReference ftr_ref = WML_OBJECT_FACTORY.createFooterReference();
ftr_ref.setId(content_ftr_rel.getId());
ftr_ref.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(ftr_ref);
}
/**
* @param pkg
* @param sectPr
* @throws InvalidFormatException
*/
private static void createAndAddHeader(final WordprocessingMLPackage pkg, final SectPr sectPr) throws InvalidFormatException {
final HeaderPart headerPart = new HeaderPart(new PartName("/word/content-header.xml"));
pkg.getParts().put(headerPart);
final Hdr content_hdr = WML_OBJECT_FACTORY.createHdr();
// Bind the header JAXB elements as representing their header parts
headerPart.setJaxbElement(content_hdr);
// Add the reference to header part to the Main Document Part
final Relationship content_hdr_rel = pkg.getMainDocumentPart().addTargetPart(headerPart);
// final Object makeParagraph = createHeaderHtml(new PartName("/word/headerthing.xml"));
final Object makeParagraph = makeParagraph(headerPart, "Hello, Sir!");
content_hdr.getContent().add(makeParagraph);
// link cover and content headers
final HeaderReference hdr_ref = WML_OBJECT_FACTORY.createHeaderReference();
hdr_ref.setId(content_hdr_rel.getId());
hdr_ref.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(hdr_ref);
}
/**
* @param partName
* @return
* @throws InvalidFormatException
* @throws UnsupportedEncodingException
*/
private static AlternativeFormatInputPart createHeaderHtml(final PartName partName) throws InvalidFormatException, UnsupportedEncodingException {
return createHTMLPart("<html><body><p>Hello, Sir!</p></body></html>", partName);
}
/**
* @param html
* @param partName
* @return
* @throws InvalidFormatException
* @throws UnsupportedEncodingException
*/
private static AlternativeFormatInputPart createHTMLPart(final String html, final PartName partName) throws InvalidFormatException,
UnsupportedEncodingException {
final AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(partName);
afiPart.setBinaryData(getBytes(html));
afiPart.setContentType(HTML_CONTENT_TYPE);
return afiPart;
}
/**
* @param part
* @param text
* @return
*/
public static P makeParagraph(final Part part, final String text) {
final P p = WML_OBJECT_FACTORY.createP();
final R r = WML_OBJECT_FACTORY.createR();
p.getContent().add(r);
final Text t = WML_OBJECT_FACTORY.createText();
r.getContent().add(t);
t.setValue(text);
return p;
}
/**
* @return
*/
private static P makePageBr() {
final P p = WML_OBJECT_FACTORY.createP();
final R r = WML_OBJECT_FACTORY.createR();
final Br br = WML_OBJECT_FACTORY.createBr();
br.setType(STBrType.PAGE);
r.getContent().add(br);
p.getContent().add(r);
return p;
}
/**
* @return
*/
public static Ftr getFtr() {
// AddPage Numbers
final CTSimpleField pgnum = WML_OBJECT_FACTORY.createCTSimpleField();
pgnum.setInstr(" PAGE \\* MERGEFORMAT ");
final PPr ppr = WML_OBJECT_FACTORY.createPPr();
ppr.setJc(alignCenter());
final R run = WML_OBJECT_FACTORY.createR();
pgnum.getContent().add(run);
final JAXBElement<CTSimpleField> fldSimple = WML_OBJECT_FACTORY.createPFldSimple(pgnum);
final P para = WML_OBJECT_FACTORY.createP();
para.getContent().add(fldSimple);
para.setPPr(ppr);
return footerFromP(para);
}
/**
* @param para
* @return
*/
private static Ftr footerFromP(final P para) {
final Ftr ftr = WML_OBJECT_FACTORY.createFtr();
ftr.getContent().add(para);
return ftr;
}
/**
* @return
*/
private static Jc alignCenter() {
final Jc jc = WML_OBJECT_FACTORY.createJc();
jc.setVal(JcEnumeration.CENTER);
return jc;
}
}
*
*/
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.List;
import javax.xml.bind.JAXBElement;
import org.docx4j.jaxb.Context;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Body;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.Br;
import org.docx4j.wml.CTSimpleField;
import org.docx4j.wml.FooterReference;
import org.docx4j.wml.Ftr;
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.P;
import org.docx4j.wml.PPr;
import org.docx4j.wml.PPrBase;
import org.docx4j.wml.R;
import org.docx4j.wml.RPr;
import org.docx4j.wml.STBrType;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.SectPr.PgMar;
import org.docx4j.wml.Text;
/**
* @author wisecream
*/
class Docx4JReportGenerator {
private static ContentType HTML_CONTENT_TYPE = new ContentType("text/html");
private static ObjectFactory WML_OBJECT_FACTORY = Context.getWmlObjectFactory();
private static BigInteger MARGIN = new BigInteger("700");
/**
* @param args
* @throws Exception
*/
public static void main(final String[] args) throws Docx4JException {
generateReport(new File("a.docx"));
}
/**
* @param html
* @return
* @throws UnsupportedEncodingException
* @throws Exception
*/
private static byte[] getBytes(final String html) throws UnsupportedEncodingException {
if (html == null) {
return new byte[0];
}
return html.getBytes("UTF-8");
}
/**
* @param body
*/
private static void addMargin(final WordprocessingMLPackage pack) {
final MainDocumentPart mainDocumentPart = pack.getMainDocumentPart();
final Body body = mainDocumentPart.getJaxbElement().getBody();
final SectPr sectPr = body.getSectPr();
final PgMar pgMar = sectPr.getPgMar();
pgMar.setLeft(MARGIN);
pgMar.setRight(MARGIN);
pgMar.setTop(MARGIN);
pgMar.setBottom(MARGIN);
}
/**
* @param out
* @throws Docx4JException
*/
public static void generateReport(final File out) throws Docx4JException {
final WordprocessingMLPackage pkg = WordprocessingMLPackage.createPackage();
createHeaderAndFooter(pkg);
final MainDocumentPart mainDocumentPart = pkg.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Normal", "First Page");
mainDocumentPart.addObject(makePageBr());
mainDocumentPart.addStyledParagraphOfText("Heading1", "Nice Page");
mainDocumentPart.addObject(makePageBr());
mainDocumentPart.addStyledParagraphOfText("Normal", "Boring page");
addMargin(pkg);
pkg.save(out);
}
/**
* @param pkg
* @throws InvalidFormatException
*/
private static void createHeaderAndFooter(final WordprocessingMLPackage pkg) throws InvalidFormatException {
final List<SectionWrapper> sections = pkg.getDocumentModel().getSections();
final SectPr sectPr = getLastSectionPart(pkg, sections);
createAndAddHeader(pkg, sectPr);
createAndAddFooter(pkg, sectPr);
}
/**
* @param pkg
* @param sections
* @return
*/
private static SectPr getLastSectionPart(final WordprocessingMLPackage pkg, final List<SectionWrapper> sections) {
final SectionWrapper lastSect = sections.get(sections.size() - 1);
final SectPr currentSectPr = lastSect.getSectPr();
if (currentSectPr != null) {
return currentSectPr;
}
final SectPr sectPr = WML_OBJECT_FACTORY.createSectPr();
pkg.getMainDocumentPart().addObject(sectPr);
lastSect.setSectPr(sectPr);
return sectPr;
}
/**
* @param pkg
* @param sectPr
* @throws InvalidFormatException
*/
private static void createAndAddFooter(final WordprocessingMLPackage pkg, final SectPr sectPr) throws InvalidFormatException {
final FooterPart content_ftr_part = new FooterPart(new PartName("/word/content-footer.xml"));
pkg.getParts().put(content_ftr_part);
//page number test
final Ftr content_ftr = getFtr();
// Bind the header JAXB elements as representing their header parts
content_ftr_part.setJaxbElement(content_ftr);
// Add the reference to both header parts to the Main Document Part
final Relationship content_ftr_rel = pkg.getMainDocumentPart().addTargetPart(content_ftr_part);
content_ftr.getContent().add(makeParagraph(content_ftr_part, "Content footer text"));
// link cover and content footers
final FooterReference ftr_ref = WML_OBJECT_FACTORY.createFooterReference();
ftr_ref.setId(content_ftr_rel.getId());
ftr_ref.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(ftr_ref);
}
/**
* @param pkg
* @param sectPr
* @throws InvalidFormatException
*/
private static void createAndAddHeader(final WordprocessingMLPackage pkg, final SectPr sectPr) throws InvalidFormatException {
final HeaderPart headerPart = new HeaderPart(new PartName("/word/content-header.xml"));
pkg.getParts().put(headerPart);
final Hdr content_hdr = WML_OBJECT_FACTORY.createHdr();
// Bind the header JAXB elements as representing their header parts
headerPart.setJaxbElement(content_hdr);
// Add the reference to header part to the Main Document Part
final Relationship content_hdr_rel = pkg.getMainDocumentPart().addTargetPart(headerPart);
// final Object makeParagraph = createHeaderHtml(new PartName("/word/headerthing.xml"));
final Object makeParagraph = makeParagraph(headerPart, "Hello, Sir!");
content_hdr.getContent().add(makeParagraph);
// link cover and content headers
final HeaderReference hdr_ref = WML_OBJECT_FACTORY.createHeaderReference();
hdr_ref.setId(content_hdr_rel.getId());
hdr_ref.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(hdr_ref);
}
/**
* @param partName
* @return
* @throws InvalidFormatException
* @throws UnsupportedEncodingException
*/
private static AlternativeFormatInputPart createHeaderHtml(final PartName partName) throws InvalidFormatException, UnsupportedEncodingException {
return createHTMLPart("<html><body><p>Hello, Sir!</p></body></html>", partName);
}
/**
* @param html
* @param partName
* @return
* @throws InvalidFormatException
* @throws UnsupportedEncodingException
*/
private static AlternativeFormatInputPart createHTMLPart(final String html, final PartName partName) throws InvalidFormatException,
UnsupportedEncodingException {
final AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(partName);
afiPart.setBinaryData(getBytes(html));
afiPart.setContentType(HTML_CONTENT_TYPE);
return afiPart;
}
/**
* @param part
* @param text
* @return
*/
public static P makeParagraph(final Part part, final String text) {
final P p = WML_OBJECT_FACTORY.createP();
final R r = WML_OBJECT_FACTORY.createR();
p.getContent().add(r);
final Text t = WML_OBJECT_FACTORY.createText();
r.getContent().add(t);
t.setValue(text);
return p;
}
/**
* @return
*/
private static P makePageBr() {
final P p = WML_OBJECT_FACTORY.createP();
final R r = WML_OBJECT_FACTORY.createR();
final Br br = WML_OBJECT_FACTORY.createBr();
br.setType(STBrType.PAGE);
r.getContent().add(br);
p.getContent().add(r);
return p;
}
/**
* @return
*/
public static Ftr getFtr() {
// AddPage Numbers
final CTSimpleField pgnum = WML_OBJECT_FACTORY.createCTSimpleField();
pgnum.setInstr(" PAGE \\* MERGEFORMAT ");
final PPr ppr = WML_OBJECT_FACTORY.createPPr();
ppr.setJc(alignCenter());
final R run = WML_OBJECT_FACTORY.createR();
pgnum.getContent().add(run);
final JAXBElement<CTSimpleField> fldSimple = WML_OBJECT_FACTORY.createPFldSimple(pgnum);
final P para = WML_OBJECT_FACTORY.createP();
para.getContent().add(fldSimple);
para.setPPr(ppr);
return footerFromP(para);
}
/**
* @param para
* @return
*/
private static Ftr footerFromP(final P para) {
final Ftr ftr = WML_OBJECT_FACTORY.createFtr();
ftr.getContent().add(para);
return ftr;
}
/**
* @return
*/
private static Jc alignCenter() {
final Jc jc = WML_OBJECT_FACTORY.createJc();
jc.setVal(JcEnumeration.CENTER);
return jc;
}
}
Parsed in 0.033 seconds, using GeSHi 1.0.8.4