Page 1 of 1

Cannot add table to docx create by Libreoffice

PostPosted: Sun Sep 30, 2012 1:27 am
by jvdvegt
I created a template document using LibreOffice 3.5. If I open the document and try to add an empty table to it using the table factory:

TblFactory.createTable(3, 3, cellWidthTwips);

I get these log messages:

16:09:26,494 ERROR [main] PropertyResolver:1405 - Expected TableNormal to have <w:basedOn ??
16:21:02,390 WARN [main] MainDocumentPart:548 - TableGrid couldn't be activated!

If I use a debugger to change the 'result1' variable in PropertyResolver, the resulting docx file opens just fine In LibreOffice. It seems style.xml created by LibreOffice does not define a style called 'TableNormal'. Is the 'result1' variable really required? I don't know the docx-spec well enough to know if the check in the PropertyResolver is for the spec or the MS-Word implementation.

Re: Cannot add table to docx create by Libreoffice

PostPosted: Sun Sep 30, 2012 9:33 am
by jason
I couldn't reproduce your issue with LibreOffice 3.5.4.2, which didn't seem to create table styles.

Could you please attach your LibreOffice template document?

Re: Cannot add table to docx create by Libreoffice

PostPosted: Sun Sep 30, 2012 8:57 pm
by jvdvegt
I've attached both the template file (a newly cretaed file will do) and the code used to add a table.

Re: Cannot add table to docx create by Libreoffice

PostPosted: Wed Oct 03, 2012 10:06 pm
by jason
In TblFactory:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                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>";
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


However there is no definition of style 'TableGrid' in the styles part.

So, you should either add that style to your styles part before calling TblFactory, or change/adapt TblFactory not to use that style.

To add the style, you can invoke (Style)XmlUtils.unmarshalString on say:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:style w:type="table" w:styleId="TableGrid">
        <w:name w:val="Table Grid"/>
        <w:basedOn w:val="TableNormal"/>
        <w:uiPriority w:val="59"/>
        <w:pPr>
            <w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
        </w:pPr>
        <w:tblPr>
            <w:tblInd w:w="0" w:type="dxa"/>
            <w:tblBorders>
                <w:top w:val="single" w:sz="4" w:space="0" w:color="000000" w:themeColor="text1"/>
                <w:left w:val="single" w:sz="4" w:space="0" w:color="000000" w:themeColor="text1"/>
                <w:bottom w:val="single" w:sz="4" w:space="0" w:color="000000" w:themeColor="text1"/>
                <w:right w:val="single" w:sz="4" w:space="0" w:color="000000" w:themeColor="text1"/>
                <w:insideH w:val="single" w:sz="4" w:space="0" w:color="000000" w:themeColor="text1"/>
                <w:insideV w:val="single" w:sz="4" w:space="0" w:color="000000" w:themeColor="text1"/>
            </w:tblBorders>
            <w:tblCellMar>
                <w:top w:w="0" w:type="dxa"/>
                <w:left w:w="108" w:type="dxa"/>
                <w:bottom w:w="0" w:type="dxa"/>
                <w:right w:w="108" w:type="dxa"/>
            </w:tblCellMar>
        </w:tblPr>
    </w:style>

 
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


Don't forget to add namespace Namespaces.W_NAMESPACE_DECLARATION to the string first!