[SOLVED] Resize images and tables

Hi,

I need to re-size an image present into a table to fit in the cell.

I have trouble figuring out the width of the cell in the final document.

Is there a way to get the real width of the cell ?

Hi Roseline,

Thanks for your inquiry. Please use getWidth/setWidth methods of CellFormat class to get/set the width of the cell in points. When the parent table’s Auto Fit option is disabled, then getWidth method specifies the actual width of the cell. but when the parent table’s Auto Fit option is enabled, then this width can be updated by the table layout algorithm according to PreferredWidth of this cell and other factors.
Similarly, you can use getHeight/setHeight of RowFormat class to get/set the height of the table row in points.

Please use the following code snippet for your kind reference and let us know if you have any more queries.

Document doc = new Document(MyDir + "table.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
Table table = (Table)shape.getAncestor(NodeType.TABLE);
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
((Row)shape.getAncestor(NodeType.ROW)).getRowFormat().setHeight(shape.getHeight());
((Row)shape.getAncestor(NodeType.ROW)).getRowFormat().setHeightRule(HeightRule.EXACTLY);
((Cell)shape.getAncestor(NodeType.CELL)).getCellFormat().setWidth(shape.getWidth());
doc.save(MyDir + "out.docx");

Hi,

Thanks for your reply.

With the explanations you gave on the table behavior, I managed to re-size the picture to the size of the cell.

I set the table to FIXED_COLUMN_WIDTHS and disable the autofit option.

Then I set the width on the picture.

Document doc = new Document(MyDir + "table.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
Table table = (Table)shape.getAncestor(NodeType.TABLE);
table.setAllowAutoFit(false);
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
Cell cell = (Cell)shape.getAncestor(NodeType.CELL);
double ratio = shape.getHeight() / shape.getWidth();
shape.setWidth(cell.getCellFormat().getWidth());
shape.setHeight(cell.getCellFormat().getWidth()*ratio);
((Cell)shape.getAncestor(NodeType.CELL)).getCellFormat().setWidth(shape.getWidth());
doc.save(MyDir + "out.docx");

Regards.

Hi Roseline,

It is nice to hear from you that you have managed to re-size the picture to the size of the cell. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.