I've recently been working on a web app that merges data from a database with templates created in TinyMCE. The output from docx4j is very nice, but the headers are not working right for me. They show on the DocX as "This image could not be displayed". The same image loads perfectly when added to the header using the header image sample, but my app uses the xhtml importer to get the image tag it's passed from tinyMCE. If I add the image using an html tag to the body of the document it renders correctly in the docx. When investigating I discovered that that the docx file for the locally stored version contained a 'header.xml.rel' whereas the one with the HTML image did not. Is there a way to make this work without having to get the image file and add it in the same way as the sample?
My code for adding the header:
- Code: Select all
def createHeaderPart(header) {
HeaderPart headerPart = new HeaderPart();
headerPart.setPackage(wordMLPackage);
headerPart.setJaxbElement(createHeader(header, headerPart));
return wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
}
def createHeaderReference(Relationship relationship) {
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);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectionProperties.getEGHdrFtrReferences().add(headerReference);
}
def createHeader(headerText, sourcePart) {
Hdr header = factory.createHdr();
P paragraph = factory.createP();
R run = factory.createR();
XHTMLImporterImpl XHTMLImporter1 = new XHTMLImporterImpl(wordMLPackage);
run.getRunContent().addAll( XHTMLImporter1.convert( processContent(headerText), null) );
paragraph.getParagraphContent().add(run);
header.getContent().add(paragraph)
return header;