Page 1 of 1

Examples for creating formatted tables

PostPosted: Wed May 13, 2009 10:52 pm
by td16
Hi, are there any examples that show how to use docx4j to create formatted tables? I am looking to create tables that have cells with different background colors etc. tx.

Re: Examples for creating formatted tables

PostPosted: Sat May 16, 2009 6:00 am
by jason
I'd suggest you use XmlUtils.unmarshalString to unmarshall the Open XML representation of a table like the one you want to create. There is an earlier forum post on this.

The easiest way to get the Open XML representation is to create a table in Word, save the document, and look at the XML (either by unzipping the docx, or saving as .xml in the first place).

Re: Examples for creating formatted tables

PostPosted: Tue May 19, 2009 4:41 pm
by td16
Thanks Jason. I have written a utility class to create tables easily. Will post the code after I clean it up.

Tushneem.

Re: Examples for creating formatted tables

PostPosted: Tue May 19, 2009 8:10 pm
by td16
Jason, I can't seem to find options to set the table row height. TrPr does not have any function or class to get this. Am I missing something?

Re: Examples for creating formatted tables

PostPosted: Wed Oct 21, 2009 3:25 pm
by pro-tbl
I too have problems to create a formatted table.
How can i get the table styles?
Is docx4j has any API to give all the table styles (including the cells) as a CSS class which is like handling P and R now?
BTW what is that TableGrid
Is this help to get the table styles in any ways?. What is that insideH and insideV. Is this help to set the cell borders?

I have written a utility class to create tables easily. Will post the code after I clean it up.

td16, Can u post the utility code.

Re: Examples for creating formatted tables

PostPosted: Thu Oct 22, 2009 2:15 am
by jason
pro-tbl wrote:How can i get the table styles?


You need to look at the ECMA spec to see what all the elements mean. A link was posted a week or 2 ago - also in relation to tables. Also useful for quick reference is the openiso.org site.

pro-tbl wrote:Is docx4j has any API to give all the table styles (including the cells) as a CSS class which is like handling P and R now?


Not right now, but I'll be doing some work in this area soon.

Could you describe what you are trying to do? Sounds like you are interested in HTML output?

Re: Examples for creating formatted tables

PostPosted: Thu Oct 22, 2009 2:05 pm
by pro-tbl
Jason,

Thanks for ur quick reply. Yes i am trying to convert docx->html. As of now docx4j does not fully supports the formatted table. So i try to do it by myself. BTW i need the style information of both table and cell.

Now i got some idea about table and cell styles. The default styles which are present in the styles.xml file. If i do any changes. i.e., the modified table styles which are present in document.xml. If it so then i decided to get the informations of both table and cell style from there and going to put this as style attribute of table and td in the html.

Is it right? Correct me if i am wrong.

Now i can able read/get the style information from document.xml. But how can i read/get the information from styles.xml file.

Any code/suggestion will be appreciated.

Re: Examples for creating formatted tables

PostPosted: Thu Oct 22, 2009 3:33 pm
by jason
Generally speaking the appearance will be determined by any styles (in styles.xml) which are applied, coupled with any directly applied formatting.

See the comment at the top of org.docx4j.model.PropertyResolver for more details.

Because styles are "based on" of other styles (eg H1 based on Normal), you need a way of making sure all the properties in the basedon styles are taken into account.

org.docx4j.convert.out.html.HtmlExporterNG sought to do this by creating a CSS style for say, Heading 1, which had incorporated in it all the properties in its based on styles. For example, if both Heading1 and Normal defined font-size, and Normal defined the font to be Comic then the Heading1 CSS definition would explicitly include font-size from Heading1, and font Comic from Normal. It would use class value of the form <p class="Heading1"> (ie single entry)

org.docx4j.convert.out.html.HtmlExporterNG2 takes a different approach. It uses class values of the form <p class="Heading1 Normal"> (ie multiple entries) and the CSS rules are placed in the correct order to ensure that, if say, both Heading1 and Normal defined font-size, then the Heading1 definition would be used.

I'd recommend you work with HtmlExporterNG2. That's what I am focused on enhancing. If you can think of reasons why the earlier NG architecture is better or should be retained/enhanced as well, then do tell!

So org.docx4j.convert.out.html.HtmlExporterNG2 needs to be modified to also write any table related css rules.

And MainDocumentPart.getStylesInUse needs to be modified to take note of styles used in table.

Finally org.docx4j.model.table.TableModel is the model of the table from which the HTML is produced (by org.docx4j.convert.out.html.TableWriter.java), so those 2 classes need to represent and write the formatting.

Cheers .. Jason

Re: Examples for creating formatted tables

PostPosted: Sun Oct 25, 2009 12:33 pm
by jason
pro-tbl wrote:And MainDocumentPart.getStylesInUse needs to be modified to take note of styles used in table.


Done. in http://dev.plutext.org/trac/docx4j/changeset/943

Re: Examples for creating formatted tables

PostPosted: Mon Oct 26, 2009 3:10 pm
by pro-tbl
Hi Jason,

Thanks for ur reply.


Have u done this completely?. Because MainDocumentPart.getStylesInUse() did not return any of the table styles at all. Where u r forming the styles for the table in the code?. Still i am not clear completely what u have done for the table.

Between i am trying some other way of getting the table styles. i.e., i am reading both the document.xml and styles.xml and get the style information and Merge the same and set to the table as a style attribute.

But i am not clear about how to set the td styles. And where i can the td style information. Is the insideV and the insideH values decide the td style? But insideV and insideH seems the property of table not the td.

Correct me if i am wrong.

i am trying to achieve the below html code with style for table.

<table cellpadding="0" cellspacing="0" style="border-right: solid #000000 1px; border-left: solid #000000 1px; border-top: solid #000000 1px; border-bottom: solid #000000 1px">
<tr>
<td width="58" valign="top" style="border-style: none solid solid none; border-width: medium 3pt 3pt medium; padding: 0in 5.4pt; width: 43.45pt;">
something
</td>
</table>

from the above html i can get the table border information, as described above. but where from i can get the td border info.

Re: Examples for creating formatted tables

PostPosted: Mon Oct 26, 2009 4:02 pm
by jason
I've just about finished all I'm planning to do for now anyway.

See comments at top of html/TableWriter for what that is :-)

The work is mainly in http://dev.plutext.org/trac/docx4j/changeset/949 and http://dev.plutext.org/trac/docx4j/changeset/950

table level innerV and innerH aren't reflected yet; i'll look at those tomorrow.

What I've done so far is in tonight's nightly - just replaced, so that it includes 950.

getStylesInUse() should pickup any table style used in the document (Word 2007 seems to use TableGrid by default). That is, provided Word created the document. If you created it with docx4j and added a table with a style, the use of the style should be picked up, but for docx4j to know anything about the style, iirc it needs to be in KnownStyles.xml.

Using HtmlExporterNG2, you should be able to get pretty close to your example html. It won't write cellpadding="0" cellspacing="0", and you'll have to wait til tomorrow for the innerV innerH stuff.

Re: Examples for creating formatted tables

PostPosted: Tue Oct 27, 2009 8:38 am
by jason
I've uploaded a sample document (sample-docs/table-features.xml ) which shows off the following table features in HtmlExporterNG2 html output:

  • table indent and width
  • table styles
  • merged cells (horizontal, vertical)
  • vertical alignment
  • cell borders and shading

To see the HTML, use the CreateHtml sample, and docx4j-nightly-20091027.jar. You can open the sample document in Word 2007 and compare it to the HTML output.

This should be enough to support a range of basic tables. More esoteric settings will require someone keen to implement support.

I still need to have a quick look at XSL FO output; it shouldn't be far behind.

Enjoy .. Jason

Re: Examples for creating formatted tables

PostPosted: Tue Oct 27, 2009 1:15 pm
by pro-tbl
Hi jason,

Thanks for ur quick reply and the valuable effort in this.
Now all the outputs comes as i wish, except for the td border in one particular type of table. For td border u did

if (styleTree.getTableStylesTree().get("TableGrid")!=null) {
Style tg = styleTree.getTableStylesTree().get("TableGrid").getData().getStyle();
TblBorders tblBorders = tg.getTblPr().getTblBorders();
result.append("/* TMP FIXME in HtmlExporterNG2! */ \n");
result.append("td { ");
if (tblBorders.getInsideH()!=null) {
BorderTop bt = new BorderTop(tblBorders.getTop() );
result.append(bt.getCssProperty());
BorderBottom bb = new BorderBottom(tblBorders.getBottom() );
result.append(bb.getCssProperty());
}
if (tblBorders.getInsideV()!=null) {
BorderRight br = new BorderRight(tblBorders.getRight() );
result.append(br.getCssProperty());
BorderLeft bl = new BorderLeft(tblBorders.getLeft() );
result.append(bl.getCssProperty());
}
// Ensure empty cells have a sensible height
result.append("height: 5mm;");
result.append("}\n");
}

is this right?

I inserted a simple table in word 2007 and removed the table borders. Now converts to HTML. Table comes peoperly with table border. Actually i removed the table border. This is the HTML output.

td { border-top-style: solid;border-top-width: 0.18mm;border-bottom-style: solid;border-bottom-width: 0.18mm;border-right-style: solid;border-right-width: 0.18mm;border-left-style: solid;border-left-width: 0.18mm;height: 5mm;}

/* PARAGRAPH STYLES */
.Normal {display:block;}

/* CHARACTER STYLES */ .DefaultParagraphFont {display:inline;}
-->
</style>
</head>
<body>
<table class="TableGrid TableNormal " style="border-top-style: none;border-top-width: 0mm;border-bottom-style: none;border-bottom-width: 0mm;border-left-style: none;border-left-width: 0mm;border-right-style: none;border-right-width: 0mm;table-layout: fixed;vertical-align: top;border-collapse: collapse;">
<colgroup>
<col style="width: 3.33in;"/>
<col style="width: 3.33in;"/>
</colgroup>
<tr style="vertical-align: top;">
<td>

Re: Examples for creating formatted tables

PostPosted: Wed Oct 28, 2009 10:14 am
by jason
Table cells which inherit borderless now render correctly with SVN changeset 960.

I'll upload a nightly in a few hours.