Page 1 of 1

Creating Tables

PostPosted: Mon Oct 05, 2009 4:36 pm
by Erik Lundqvist
Hi,

I need to create tables with merged cells. Is this something that can easily be done with docx4j? If so does anyone have a nice example of the best way to do this? Also general examples of working with tables would be welcome. The expected output here is a '.docx' file.

Regards,
Erik

Re: Creating Tables

PostPosted: Mon Oct 05, 2009 5:14 pm
by td16
Erik Lundqvist wrote:Hi,

I need to create tables with merged cells. Is this something that can easily be done with docx4j? If so does anyone have a nice example of the best way to do this? Also general examples of working with tables would be welcome. The expected output here is a '.docx' file.

Regards,
Erik


For horizontal merge, you can create a cell with the code below and insert into a table row
Code: Select all
 
public static Tc createTableCellGspan(WordprocessingMLPackage wordMLPackage,
                                        P p, int gridspan) {
    org.docx4j.wml.Tc tc = factory.createTc();
    org.docx4j.wml.TcPr tcpr = factory.createTcPr();
    tc.setTcPr(tcpr);
    CTVerticalJc valign = factory.createCTVerticalJc();
    valign.setVal(STVerticalJc.TOP);
    tcpr.setVAlign(valign);
    org.docx4j.wml.TcPrInner.GridSpan gspan = factory.createTcPrInnerGridSpan();
    gspan.setVal(new BigInteger("" + gridspan));
    tcpr.setGridSpan(gspan);
    tc.getEGBlockLevelElts().add(p);
    return tc;
  }


For vertical merge, you can use the code below for your cell.

Code: Select all

public static Tc createTableCellVMerge(WordprocessingMLPackage wordMLPackage,
                                         P p, String vMerge) {
    org.docx4j.wml.Tc tc = factory.createTc();
    org.docx4j.wml.TcPr tcpr = factory.createTcPr();
    tc.setTcPr(tcpr);
    CTVerticalJc valign = factory.createCTVerticalJc();
    valign.setVal(STVerticalJc.TOP);
    tcpr.setVAlign(valign);

    org.docx4j.wml.TcPrInner.VMerge vm = factory.createTcPrInnerVMerge();
    vm.setVal(vMerge);
    tcpr.setVMerge(vm);

    tc.getEGBlockLevelElts().add(p);
    return tc;
  }


Hope this helps!

Re: Creating Tables

PostPosted: Mon Oct 05, 2009 5:51 pm
by Erik Lundqvist
Hi td16,

Thanks for that, it looks helpful. I have some questions though on the vertical merge method. Could you explain the parameters maybe and also why it does not talk a 'span' argument like the other method?

Thanks
Erik

Re: Creating Tables

PostPosted: Mon Oct 05, 2009 6:15 pm
by td16
Erik Lundqvist wrote:Hi td16,

Thanks for that, it looks helpful. I have some questions though on the vertical merge method. Could you explain the parameters maybe and also why it does not talk a 'span' argument like the other method?

Thanks
Erik


You can unmarshall an existing document to see what attributes are supported or just create a docx with the format you want (in MS Word), change the extension to .zip, open it and look at document.xml. All you need to then do is replicate that in your code.

Tushneem.

Re: Creating Tables

PostPosted: Tue Oct 06, 2009 12:56 am
by jason
A couple of quick remarks:

  • the easiest way to look at an existing document is, in Word, to "save as" .xml. This gives you a Flat OPC format file. No need to unzip etc. I find XPontus - an open source java xml editor - a handy way to pretty print (I usuallly cut/paste into it, since its file dialog is slow).
  • if you are creating tables, you can use the object factory like Tushneem showed, or, if you have existing XML you are trying to copy, a shortcut is to use XmlUtils.unmarshalString, or unmarshallFromTemplate if you wish to substitute data
  • for particulars on the docx table model, see the specs at ECMA or ISO; i'll post a sticky link to these
  • we also have org.docx4j.model.table, which is used for converting docx tables to html, pdf etc. Its javadoc says:

    In docx, horizontally merged cells are represented by one cell
    * with a gridSpan attribute; while vertically merged cells are
    * represented as a top cell containing the actual content and a series
    * of dummy cells having a vMerge tag with "continue" attribute.

cheers .. Jason

Re: Creating Tables

PostPosted: Wed Oct 07, 2009 4:21 pm
by Erik Lundqvist
Thank you guys

I have to say that after reading the ECMA-376 the docx4j API makes a lot more sense :)

http://www.ecma-international.org/publications/standards/Ecma-376.htm

Re: Creating Tables

PostPosted: Sun Oct 25, 2009 12:37 pm
by jason
There is now a class TblFactory for creating tables. See http://dev.plutext.org/trac/docx4j/changeset/943