On 17.12.18 22:17, paoim101084 wrote:
Do you know how to autoResize the column in XSLFTable?
I got painful when I use XSLFTable. I cannot calculate the exact table
height.
I've just realized that the table height is not updated [1], only the bounding
boxes of the cells are updated on rendering. So I need to rework that, but ...
So currently you are left with setting the column width and the start position
of the table anchor.
Why do you need to calculate it's height?
Do you want something like an autofit option, where you set the table content
and the column sizes should adapt?
I thought about this, but this is kind of an optimization where you need to
find a minima on all row heights/column widths and for a table in slideshow
this seems to be a bit too much effort.
If you generate such a price list and want to know when to continue on the next
page, that's something feasible for my mentioned rework ... so you would add a
row, check the table height and eventually jump to the next page.
Andi
[1] Test program to show that table is displayed more or less ok, when not
knowing it's size:
public void testResize()throws Exception {
String[][] data = {
{"ID","Name","Description","Price","Percent","Current Value" },
{"123","Car","It is new car in 2019","$40000","88%","$38000"},
{"123","Car","It is new car in 2019","$40000","88%","$38000"},
{"123","Car","It is new car in 2019","$40000","88%","$38000"}
};
XMLSlideShow ppt =new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
// a red box in the background, to show/verify the table dimensions
XSLFAutoShape as = slide.createAutoShape();
XSLFTable tab = slide.createTable(data.length, data[0].length);
for (int row=0; row<data.length; row++) {
for (int col=0; col<data[row].length; col++) {
XSLFTableCell c = tab.getCell(row, col);
c.setText(data[row][col]);
c.setTextAutofit(TextShape.TextAutofit.SHAPE);
XSLFTextRun tr = c.getTextParagraphs().get(0).getTextRuns().get(0);
tr.setFontSize(20.);
}
}
tab.setColumnWidth(0,60);
tab.setColumnWidth(1,60);
// this is not working correctly, the width/height is 0 Rectangle2D rect =
tab.getAnchor();
rect =new Rectangle2D.Double(100,100,rect.getWidth(),rect.getHeight());
tab.setAnchor(rect);
as.setShapeType(ShapeType.RECT);
as.setFillColor(Color.RED);
as.setAnchor(rect);
try (FileOutputStream fos =new FileOutputStream("tabtest.pptx")) {
ppt.write(fos);
}
}