Table cell's width to CSS conversion is missing
Posted: Thu Feb 25, 2016 10:06 pm
Dear Jason,
I was using the PropertyFactory.createProperties(TcPr tcPr) to convert Word's table cell properties to CSS, and noticed that this does not pick up the cell's width.
So I've added my own conversion class which does this, handles percentage and pixel/dxa widths. Not complete though, I don't know how this works with XslFO !!!
There is one question: in my experiences the dxa width was somehow correct if I was dividing the dxa value by 15 instead of the "official" value of 20. Why this is happening here? So fix this if you don't see this is correct????
Thanks
Zoltan
Feel free to include this in the next release:
I was using the PropertyFactory.createProperties(TcPr tcPr) to convert Word's table cell properties to CSS, and noticed that this does not pick up the cell's width.
So I've added my own conversion class which does this, handles percentage and pixel/dxa widths. Not complete though, I don't know how this works with XslFO !!!
There is one question: in my experiences the dxa width was somehow correct if I was dividing the dxa value by 15 instead of the "official" value of 20. Why this is happening here? So fix this if you don't see this is correct????
Thanks
Zoltan
Feel free to include this in the next release:
- Code: Select all
import java.math.BigInteger;
import org.docx4j.model.properties.Property;
import org.docx4j.wml.TblWidth;
import org.w3c.dom.Element;
/**
* Table-cells' width
*/
public class TableWidthProperty extends Property {
public final static String CSS_NAME = "width";
public TableWidthProperty(TblWidth tcW) {
setObject(tcW);
}
@Override
public String getCssName() {
return CSS_NAME;
}
@Override
public String getCssProperty() {
TblWidth tblWidth = (TblWidth)this.getObject();
BigInteger width = tblWidth.getW();
if (width==null) {
log.warn("FIXME");
return CSS_NULL;
}
String type = tblWidth.getType();
// fix pixel size
if (TblWidth.TYPE_DXA.equalsIgnoreCase(type)) {
// a fixed width
int value = width.intValue();
// TODO: the normal dxa seems to mean 20px, but in my Word that seems 15px
// see: https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
// int px = UnitsOfMeasurement.twipToPoint(value);
int px = value/15;
return composeCss(CSS_NAME, px + "px");
}
// percentage size
if ("pct".equalsIgnoreCase(type)) {
double pct = width.intValue() / 50f;
String value = (pct == ((int) pct)) ? String.valueOf((int) pct) : String.format("%.1f", Double.valueOf(pct));
return composeCss(CSS_NAME, value +"%");
}
return CSS_NULL;
}
/* (non-Javadoc)
* @see org.docx4j.model.properties.Property#setXslFO(org.w3c.dom.Element)
*/
@Override
public void setXslFO(Element foElement) {
// TODO: not yet implemented
log.warn("FIXME");
}
}