Page 1 of 1

How to replace variables in footer using docx4j?

PostPosted: Tue Nov 20, 2012 2:16 am
by siva kumar
Hi jason,
The normal way of replacing variables is not working for footer variable replacement, can u pls add some sample code to do that
im using below code for replacement

-----------------------------
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

// unmarshallFromTemplate requires string input
String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(),
true);
obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
------------------------------

Thanx in advance




Regards,
Siva Kumar

Re: How to replace variables in footer using docx4j?

PostPosted: Tue Nov 20, 2012 2:56 pm
by jason
Get each footer using the document model, or by iterating through the parts looking for parts of type footer.

For example, MailMerger does the following (headers/footers in first document section only):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                        SectionWrapper sw = input.getDocumentModel().getSections().get(0);
                        SectPr sectPr = sw.getSectPr();
                       
                        List<CTRel> hdrFtrRefs = sectPr.getEGHdrFtrReferences();
                        titlePage = sectPr.getTitlePg();
                       
                        for (CTRel rel : hdrFtrRefs) {
                                String relId = rel.getId();
                                log.debug("for h|f relId: " + relId);
                               
                                JaxbXmlPart part = (JaxbXmlPart)input.getMainDocumentPart().getRelationshipsPart().getPart(relId);
                                :
                        }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: How to replace variables in footer using docx4j?

PostPosted: Tue Nov 27, 2012 9:24 pm
by siva kumar
Hi Jason,

I'm getting below error while replacing variables in footer at the last line of code "footerPart.setJaxbElement((Ftr)obj);" . pls check

"java.lang.NullPointerException"

#CODE:
-----------------------
List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
String xml = null;
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
if (hfp != null) {
FooterPart footerPart = hfp.getDefaultFooter();
if (footerPart != null) {
//xml --> string
xml = XmlUtils.marshaltoString(footerPart.getJaxbElement(), true);
System.err.println(xml);
}

HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("testing footer", "${test}");
Object obj = null;
try {

obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
Ftr ftr = (Ftr)obj;
System.err.println(ftr.getContent().toString());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// Inject result into docx
footerPart.setJaxbElement((Ftr)obj);
-----------------------

Re: How to replace variables in footer using docx4j?

PostPosted: Wed Nov 28, 2012 9:19 pm
by jason
well, is hfp.getDefaultFooter() returning null?

What footers do you have in the relevant section?

You can run the sample https://github.com/plutext/docx4j/blob/ ... rList.java to find out