Search code examples
openxmldocx4jfootnotes

Programmatically set style for footnoteReference run


I have written a generator with docx4j that takes a proprietary data model as input and produces a docx file as output.

I try to add footnotes (and footnote-references), following the example here: https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/FootnoteAdd.java

However, I fail to see how I can add styling to the run that represents a footnoteReference. I create it exactly like in the example:

CTFtnEdnRef ftnednref = wmlObjectFactory.createCTFtnEdnRef(); 
JAXBElement<org.docx4j.wml.CTFtnEdnRef> ftnednrefWrapped = wmlObjectFactory.createRFootnoteReference(ftnednref); 
r.getContent().add( ftnednrefWrapped); 
ftnednref.setId( BigInteger.valueOf( i) );

How can I add styling information to ftnednref? Eventually, this "run" is a normal Run in my result, but it does not have the RStyle FootnoteReference, which means that it is not in superscript in my document. I figured this style should be applied automatically, but it isn't. My result looks like this:

<w:r>
  <w:rPr>
    <w:highlight w:val="darkCyan"/>
  </w:rPr>
  <w:footnoteReference w:id="2"/>
</w:r>

It re-uses the rPr of the preceeding run. How can I make sure this footnoteReference-Run has the correct styling?


Solution

  • If you create a sample document in Word, you can use the docx4j webapp or Helper Word AddIn, to generate corresponding Java code.

    I this case, I got:

            <w:r>
                <w:rPr>
                    <w:rStyle w:val="FootnoteReference"/>
                    <w:lang w:val="en-AU"/>
                </w:rPr>
                <w:footnoteReference w:id="1"/>
            </w:r>
    

    and

            // Create object for r
            R r2 = wmlObjectFactory.createR(); 
            p.getContent().add( r2); 
                // Create object for rPr
                RPr rpr2 = wmlObjectFactory.createRPr(); 
                r2.setRPr(rpr2); 
                    // Create object for rStyle
                    RStyle rstyle = wmlObjectFactory.createRStyle(); 
                    rpr2.setRStyle(rstyle); 
                        rstyle.setVal( "FootnoteReference"); 
                    // Create object for lang
                    CTLanguage language2 = wmlObjectFactory.createCTLanguage(); 
                    rpr2.setLang(language2); 
                        language2.setVal( "en-AU"); 
                // Create object for footnoteReference (wrapped in JAXBElement) 
                CTFtnEdnRef ftnednref = wmlObjectFactory.createCTFtnEdnRef(); 
                JAXBElement<org.docx4j.wml.CTFtnEdnRef> ftnednrefWrapped = wmlObjectFactory.createRFootnoteReference(ftnednref); 
                r2.getContent().add( ftnednrefWrapped); 
                    ftnednref.setId( BigInteger.valueOf( 1) ); 
    

    So the bit you need is:

                // Create object for rStyle
                RStyle rstyle = wmlObjectFactory.createRStyle(); 
                rpr.setRStyle(rstyle); 
                    rstyle.setVal( "FootnoteReference");
    

    You'll also want to define the FootnoteReference style in your styles part.