Page 1 of 1

edit footnotes

PostPosted: Fri Jul 29, 2011 8:50 pm
by Timothy
I'm new to docx4j and struggling with following issue.
In our project we have a template (*.docx) file with text and placeholders. I use the unmarshallFromTemplate function te replace them (Actually we made a local unmarshalFromTemplate that uses the docx4j implementation).

I can replace placeholders in the maindocument, the footers but not the footnotes? Is there a reason for that.

I execute the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
if ("/word/footnotes.xml".equals(part.getPartName().getName())) {
        FootnotesPart footnotesPart = (FootnotesPart) part;
        CTFootnotes ctFootnotes = footnotesPart.getJaxbElement();
        String footnotesXML = XmlUtils.marshaltoString(ctFootnotes, true);
       
        // valorize template
        Object footnotesObj = null;
        try {
                footnotesObj = LocalXmlUtils.unmarshallFromTemplate(footnotesXML, mappings);
        } catch (JAXBException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
        }

        if (footnotesObj != null) {
                try{
                        footnotesPart.setJaxbElement((CTFootnotes) footnotesObj);
                }catch(Exception e){
                        e.printStackTrace();
                }
        }
}
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


I get following error: javax.xml.bind.JAXBElement cannot be cast to org.docx4j.wml.CTFootnotes

If I use the code above for the maindocument or footer it works.

For example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
if ("/word/footer1.xml".equals(part.getPartName().getName())) {
        FooterPart footerPart = (FooterPart) part;
        Ftr ftr = footerPart.getJaxbElement();
        String footerXML = XmlUtils.marshaltoString(ftr, true);

        // valorize template
        Object footerObj = null;
        try {
                footerObj = LocalXmlUtils.unmarshallFromTemplate(footerXML, mappings);
        } catch (JAXBException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
        }

        if (footerObj != null) {
                footerPart.setJaxbElement((Ftr) footerObj);
        }
}
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


Can somebody give me a hint? Is it possible to edit placeholders in footnotes?

Thx in advance!

Re: edit footnotes

PostPosted: Fri Jul 29, 2011 11:56 pm
by jason
Looks like your footnotesObj is a javax.xml.bind.JAXBElement; your CTFootnotes is probably inside it.

XmlUtils contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
 
        public static Object unwrap(Object o) {
               
                if (o==null) return null;
               
                if (o instanceof javax.xml.bind.JAXBElement) {
                        log.debug("Unwrapped " + ((JAXBElement)o).getDeclaredType().getName() );
                        log.debug("name: " + ((JAXBElement)o).getName() );
                        return ((JAXBElement)o).getValue();
                } else {
                        return o;
                }
        }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Try invoking that.

Re: edit footnotes

PostPosted: Mon Aug 01, 2011 7:33 pm
by Timothy
That works, thx for the quick reply!