Page 1 of 1

XHTML Image in Header Part

PostPosted: Fri Oct 30, 2015 7:34 am
by srendall
Hi all,

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.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();
            sections.get(sections.size() - 1).setSectPr(sectionProperties);

        HeaderReference headerReference = factory.createHeaderReference();

    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) );
        return header;

Re: XHTML Image in Header Part

PostPosted: Wed Nov 04, 2015 8:17 pm
by jason
You'd implement XHTMLImageHandler, so that it adds the image part as a rel of the header part.

The default implementation, XHTMLImageHandlerDefault adds the image part as a rel of the main document part.

To use your implementation, invoke XHTMLImporter1.setXHTMLImageHandler(yourxHTMLImageHandler)

Copy the default implementation, XHTMLImageHandlerDefault. The only change you need to make really is instead of

imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, imageBytes);

imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, headerPart, imageBytes);

Re: XHTML Image in Header Part

PostPosted: Thu May 25, 2017 1:26 am
by ludgea
Sorry to dig this post but I'm in a similar trouble.

My header contains a XHTML with two pictures in it, coded like this :
Code: Select all
<div style="position:absolute;z-index:1"><img src="http://fru1doc1:8080/images/image001.jpg" /></div>

I understand that contrary to the body, you have to handle the pciture in the header with rel.

Unfortunately, i wasn't able to understand the imagePart of your answer.

Does the URL stil lhave to be declared in the XHTML ?

Here's the code I use for my header :

Code: Select all
        //Header Part start
        HeaderPart headerPart = new HeaderPart();
        Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);

        XHTMLImporterImpl xHTMLImporter2 = new XHTMLImporterImpl(wordMLPackage);

        Hdr hdr = Context.getWmlObjectFactory().createHdr();
        hdr.getContent().addAll(xHTMLImporter2.convert(new File(inputfilepath2), null));

        List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();

        SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
        // There is always a section wrapper, but it might not contain a sectPr

        if (sectPr == null) {
            sectPr = objectFactory.createSectPr();
            sections.get(sections.size() - 1).setSectPr(sectPr);

        HeaderReference headerReference = objectFactory.createHeaderReference();