Page 1 of 1

How to use TraversalUtil in footers

PostPosted: Thu Nov 25, 2010 9:00 am
by vanius
Hi,

The "Getting Started" manual says "TraversalUtil can also be applied to headers, footers etc.". I only got TraversalUtil under mainDocumentPart. How to pass footers to TraversalUtil?

I tried walk the parts:

Code: Select all
Body body;
RelationshipsPart rp = opcPackage.getRelationshipsPart();
for (Relationship r : rp.getRelationships().getRelationship()) {
        Part part = rp.getPart(r);
        if (part instanceof JaxbXmlPart) {
                Object o = ((JaxbXmlPart) part).getJaxbElement();
                if (o instanceof org.docx4j.wml.Document) {
                        body = ((org.docx4j.wml.Document) o).getBody();
                        new TraversalUtil(body, this);
                }
        }
}


But doesn´t work.

Thanks

Re: How to use TraversalUtil in footers

PostPosted: Thu Nov 25, 2010 11:24 am
by jason
That wouldn't work.

apart from anything else, the 2nd arg to TraversalUtil has to be your implementation of the Callback interface.

Try something like this:
Code: Select all
   public static void traverseFooter(WordprocessingMLPackage wordMLPackage) {
      
      HeaderFooterPolicy hfp = wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy();
      
      Ftr ftr = hfp.getDefaultFooter().getJaxbElement();
            
      FooterTraverser traverser = new FooterTraverser();
      new TraversalUtil(ftr.getEGBlockLevelElts(),
            traverser);
      
   }

    static class FooterTraverser extends CallbackImpl {
             
       @Override
      public List<Object> apply(Object o) {
          
          System.out.println("Encountered " + o.getClass().getName() );
                  
         return null;
      }
   }


Note that this example assumes that the first section in the docx has a default footer.

Re: How to use TraversalUtil in footers

PostPosted: Thu Nov 25, 2010 11:38 pm
by vanius
Works!

I made only minor adjustments. Because getEGBlockLevelElts() returns ArrayList I used:

Code: Select all
            ...
            List<Object> e = ftr.getEGBlockLevelElts();
            for (int j = 0; j < e.size(); j++)
                new TraversalUtil(e.get(j), this);


And I had...

Code: Select all
   
    @Override
    public void walkJAXBElements(Object parent) {
        List children = getChildren(parent);
        if (children != null)
            for (Object o : children) {
                o = XmlUtils.unwrap(o);
                this.apply(o);
                walkJAXBElements(o);
            }
    }


and changed to:

Code: Select all
    @Override
    public void walkJAXBElements(Object parent) {
        this.apply(XmlUtils.unwrap(parent));
        List children = getChildren(parent);
        if (children != null)
            for (Object o : children) {
                o = XmlUtils.unwrap(o);
                walkJAXBElements(o);
            }
    }


With this changes works perfectly!

Thank you very much!!!