Page 1 of 1

Reorder Rows of a Table

PostPosted: Wed Jan 02, 2013 6:25 pm
by Zaeem
Is it possible, using docx4j, to re-order the rows of a certain table in a DOCX? If yes, then can you please hint over it?

Re: Reorder Rows of a Table

PostPosted: Wed Jan 02, 2013 8:54 pm
by Zaeem
Nevermind, I got it :) Here's the code (for a table having four rows) if someone else needs it:

Code: Select all
public class TableOrder {
   private static WordprocessingMLPackage wordMLPackage;
   private static ObjectFactory factory;

   public static void main (String args[]) throws Exception {
      String tableDOCXFilePath = "C:\\TableOrder\\table.docx";
      WordprocessingMLPackage tableDOCX = DOCXHelper.getInput(tableDOCXFilePath);
      List<Object> tableList = DOCXHelper.getAllElementFromObject(tableDOCX.getMainDocumentPart(), Tbl.class);
      Tbl table = (Tbl)tableList.get(0);
      List<Object> rowList = DOCXHelper.getAllElementFromObject(table, Tr.class);
      
      wordMLPackage = WordprocessingMLPackage.createPackage();
      factory = Context.getWmlObjectFactory();
      Tbl newTable = factory.createTbl();
      
      newTable.getContent().add(rowList.get(3));
      newTable.getContent().add(rowList.get(1));
      newTable.getContent().add(rowList.get(2));
      newTable.getContent().add(rowList.get(0));
      
      wordMLPackage.getMainDocumentPart().addObject(newTable);
      wordMLPackage.save(new java.io.File("C:\\TableOrder\\tableNew.docx"));
   }
}

Re: Reorder Rows of a Table

PostPosted: Mon Jan 07, 2013 3:49 pm
by sreejiths_123
Hi Zaeem ,

Please help me to find the class DOCXHelper . Where can i get this class ?
There is no such class in docx4j library called DOCXHelper.

Waiting for your earliest reply .

Re: Reorder Rows of a Table

PostPosted: Mon Jan 07, 2013 3:57 pm
by jason
Looks like he's just using that class of his to find all the tables in his document.

As per the Getting Started guide, there are 2 ways to find things in docx4j, via XPath and via TraversalUtil...