I have 3 htms, header, body and footer to generate a docx letter.
In body the images shows perfectly, but not in header.
I saw that there was a similar bug, but from docx to html (https://github.com/plutext/docx4j/commi ... a9aecfac54).
I use 3.3.5 release, but I still have the problem. Is solved in master?
It's possible to fix it? Thanks!!!
My code:
public static void convert(String header, String body, String footer, OutputStream output) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
List<Object> headerList = XHTMLImporter.convert(toXHTML(header), output);
List<Object> bodyList = XHTMLImporter.convert(toXHTML(body), null);
List<Object> footerList = XHTMLImporter.convert(toXHTML(footer), null);
createHeaderPart(wordMLPackage, headerList);
wordMLPackage.getMainDocumentPart().getContent().addAll(bodyList);
wordMLPackage.save(output);
}
private static void createHeaderPart(WordprocessingMLPackage wordMLPackage, List<Object> headerList) throws InvalidFormatException {
ObjectFactory factory = Context.getWmlObjectFactory();
HeaderPart headerPart = new HeaderPart();
headerPart.setPackage(wordMLPackage);
Hdr header = factory.createHdr();
header.getContent().addAll(headerList);
headerPart.setJaxbElement(header);
Relationship relationship = wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
SectPr sectionProperties = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectionProperties == null) {
sectionProperties = factory.createSectPr();
wordMLPackage.getMainDocumentPart().addObject(sectionProperties);
sections.get(sections.size() - 1).setSectPr(sectionProperties);
}
/*
* Remove Header if it already exists.
*/
List<CTRel> relations = sectionProperties.getEGHdrFtrReferences();
Iterator<CTRel> relationsItr = relations.iterator();
while (relationsItr.hasNext()) {
CTRel relation = relationsItr.next();
if (relation instanceof HeaderReference) {
relationsItr.remove();
}
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectionProperties.getEGHdrFtrReferences().add(headerReference);
HeaderReference firstPageHeaderRef = factory.createHeaderReference();
firstPageHeaderRef.setId(relationship.getId());
firstPageHeaderRef.setType(HdrFtrRef.FIRST);
sectionProperties.getEGHdrFtrReferences().add(firstPageHeaderRef);
}