Page 1 of 1

How to set banded columns/rows using docx4j

PostPosted: Sat Mar 30, 2013 3:20 am
by jarod
Hi,
I am creating a word document using docx4j within which I have several tables.
I would like to set table design/style for each table as well as setting the options such as "header row" "first column" etc.

I have the following:
Code: Select all
Tbl table = factory.createTbl();
// here adding content to the table ...
TblPr tblPr = factory.createTblPr();
TblPr tblPr = factory.createTblPr();
TblStyle tblStyle = factory.createCTTblPrBaseTblStyle();
tblStyle.setVal("LightGrid");
tblPr.setTblStyle(tblStyle);

// somewhere here I would like to set the column values an so on.
// something that looks like this in wml:
// <w:tblLook w:val=\"04A0\" w:firstRow=\"1\" w:lastRow=\"0\" w:firstColumn=\"1\" w:lastColumn=\"0\" w:noHBand=\"0\" w:noVBand=\"1\"/>

table.setTblPr(tblPr);
wordMLPackage.getMainDocumentPart().addObject(table);


I tried this to set the tableLook:
Code: Select all
CTShortHexNumber tblLook = factory.createCTShortHexNumber()
// ?? don't know what to do here
tblPr.setTblLook(tblLook);


I see two solutions:
1) Finding out how CTShortHexNumber tblLook can be set correctly
2) or finding out how to "push" <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/> this into the tblPr

Thanks for any help, I couldn't find anything on this subject somwhere else.

Re: How to set banded columns/rows using docx4j

PostPosted: Sat Mar 30, 2013 7:23 am
by jason
Thanks for raising this interesting issue.

In Open XML 1ed (ECMA 376), the definition was:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
                        <xsd:element name="tblLook" type="CT_ShortHexNumber" minOccurs="0"
                                maxOccurs="1">
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


In 2ed, it was changed to:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
                        <xsd:element name="tblLook" type="CT_TblLook" minOccurs="0" maxOccurs="1">
                                <xsd:annotation>
                                        <xsd:documentation>Table Style Conditional Formatting Settings</xsd:documentation>
                                </xsd:annotation>
                        </xsd:element>

        <xsd:complexType name="CT_TblLook">
                <xsd:attribute name="firstRow" type="s:ST_OnOff">
                        <xsd:annotation>
                                <xsd:documentation>First Row</xsd:documentation>
                        </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="lastRow" type="s:ST_OnOff">
                        <xsd:annotation>
                                <xsd:documentation>Last Row</xsd:documentation>
                        </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="firstColumn" type="s:ST_OnOff">
                        <xsd:annotation>
                                <xsd:documentation>First Column</xsd:documentation>
                        </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="lastColumn" type="s:ST_OnOff">
                        <xsd:annotation>
                                <xsd:documentation>Last Column</xsd:documentation>
                        </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="noHBand" type="s:ST_OnOff">
                        <xsd:annotation>
                                <xsd:documentation>No Horizontal Banding</xsd:documentation>
                        </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="noVBand" type="s:ST_OnOff">
                        <xsd:annotation>
                                <xsd:documentation>No Vertical Banding</xsd:documentation>
                        </xsd:annotation>
                </xsd:attribute>
        </xsd:complexType>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


The transitional migration XSD allows both forms:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <xsd:complexType name="CT_TblLook">
    <xsd:attribute name="firstRow" type="s:ST_OnOff"/>
    <xsd:attribute name="lastRow" type="s:ST_OnOff"/>
    <xsd:attribute name="firstColumn" type="s:ST_OnOff"/>
    <xsd:attribute name="lastColumn" type="s:ST_OnOff"/>
    <xsd:attribute name="noHBand" type="s:ST_OnOff"/>
    <xsd:attribute name="noVBand" type="s:ST_OnOff"/>
    <xsd:attribute name="val" type="ST_ShortHexNumber"/>
  </xsd:complexType>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


With a couple of exceptions, the wml classes in docx4j were generated from the 1st ed schema.

The classes need to be upgraded to support either form. I've created https://github.com/plutext/docx4j/issues/53 to track this.

In terms of creating the ShortHexNumber, You can do something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                Tbl tbl = Context.getWmlObjectFactory().createTbl();           
               
                // w:tblPr
                String strTblPr =  "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION + ">"
                        + "<w:tblStyle w:val=\"TableGrid\"/>"
                        +       "<w:tblW w:w=\"0\" w:type=\"auto\"/>"
                        +   "<w:tblLook w:val=\"04A0\"/>"
                        + "</w:tblPr>";
                TblPr tblPr = null;
                try {
                        tblPr = (TblPr)XmlUtils.unmarshalString(strTblPr);
                } catch (JAXBException e) {
                        // Shouldn't happen
                        e.printStackTrace();
                }
                tbl.setTblPr(tblPr);

 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


See TblFactory.

For some reason, tblLook seems to be missing from ObjectFactory, but alternatively you could try:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
CTShortHexNumber tblLook= new CTShortHexNumber();
tblLook.setVal(value)
 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


or:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                CTShortHexNumber tblLook= new JAXBElement<CTShortHexNumber>(
                                new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main",
                                                "tblLook"),
                                                CTShortHexNumber.class, TblPr.class, value);
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4



You'll have to look in 1ed of the standard, Google or experiment to find out what order the bits are combined into the hex value.

See also http://msdn.microsoft.com/en-us/library ... =office.12).aspx for Microsoft extensions.

Re: How to set banded columns/rows using docx4j

PostPosted: Sat Mar 30, 2013 7:34 am
by jarod
Thanks a lot for your answer,
I won't be able to test this before tuesday,but be sure to receive some feedback from me on whether your alternative proposals worked or not (in the meantime that the TblLook is updated to the newer version).

Have a nice weekend,

Re: How to set banded columns/rows using docx4j

PostPosted: Mon Apr 01, 2013 10:10 am
by jason

Re: How to set banded columns/rows using docx4j

PostPosted: Tue Apr 02, 2013 7:53 pm
by jarod
Hi Jason,
thanks for the updated code, I am however very unfamiliar with github. I succeeded to install and download the code from github, however I still have some errors (it seems that the Maven dependencies don't work properly).
Anyway, my question is: is there an nightly build (jar file) of the docx4j that includes the changes from github ?
I saw the docx4j-nightly-20130331.jar here http://www.docx4java.org/docx4j/, but it doesn't seem to include the changed I need.
Thanks for the great help

Re: How to set banded columns/rows using docx4j

PostPosted: Wed Apr 03, 2013 12:13 am
by jarod
Some feedback:
even if I still didn't manage to resolve the github version maven dependencies, I "hacked" the jar I had by adding/replacing the new/changed classes into the jar, it's dirty, but I needed to test the result quickly :mrgreen:

Anyway, the update works perfectly. For the record, here is my code with the new version:
Code: Select all
Tbl table = factory.createTbl();
// here adding content to the table ...
TblPr tblPr = factory.createTblPr();
TblPr tblPr = factory.createTblPr();
TblStyle tblStyle = factory.createCTTblPrBaseTblStyle();
tblStyle.setVal("LightGrid");
tblPr.setTblStyle(tblStyle);
CTTblLook tblLook = factory.createCTTblLook();
        tblLook.setFirstColumn(STOnOff.FALSE);
        tblLook.setFirstRow(STOnOff.TRUE); // Header Row
        tblLook.setLastRow(STOnOff.FALSE); //Total Row
        tblLook.setLastColumn(STOnOff.FALSE);
        tblLook.setNoHBand(STOnOff.TRUE);
        tblLook.setNoVBand(STOnOff.FALSE);
tblPr.setTblLook(tblLook);
table.setTblPr(tblPr);
wordMLPackage.getMainDocumentPart().addObject(table);


Cheers

Re: How to set banded columns/rows using docx4j

PostPosted: Wed Apr 03, 2013 1:19 pm
by jason
Thanks for the feedback.

There is now the nightly http://www.docx4java.org/docx4j/docx4j- ... 130403.jar

It'd be good to resolve your issues building. Please open another thread for that, including your IDE in the title. Its nice to have docx4j sources running in your IDE without error. That's one thing. The other is to be able to build a jar using mvn or ant from the command line. If you are having trouble with one or other of those tools, please let us know.