Free Support Forum - aspose.com

Setting size of an image in a table

Hi,



I create a table and add content using the builder (see below).

Due to the way everything is setup I first create the table and apply formatting afterwards including column width and row height.



Is there a way to resize an image in a cell after creating the table (retrieve shape from cell and resize it)?

Or is there a way to resize an image in a cell based on the column width (before knowing the column width so something like auto size).



this.builder = builder;

this.table = this.builder.startTable();

this.builder.insertCell();

(loop) setCellValue( row, column, value)

this.builder.endRow();

this.table = this.builder.endTable();



I hope you can help me with this.



Thanks,



Marcel

Hi Marcel,


Thanks for your inquiry. I believe the following code helps in achieving what you are looking for:

Document
doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();

builder.RowFormat.Height = 250;

builder.CellFormat.Width = 400;

builder.InsertCell();

Shape shape = builder.InsertImage(@"C:\Temp\Sample.png", -1, -1);

builder.EndRow();

builder.EndTable();

Cell c = shape.GetAncestor(NodeType.Cell) as Cell;

shape.Width = c.CellFormat.Width;

shape.Height = c.ParentRow.RowFormat.Height;

doc.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best regards,

Hi Awais,



That is a very good start :slight_smile:



What if:

I have more than one cell with a shape and I know the location of the cells.



1. Can I retrieve the Shape from the cell (basically the other way around from what you do)?



or would it be easier/better to:



2. Store references to the Shape and loop through an array of them?



Thanks again for your prompt and very good help!



Marcel

Hi Marcel,


Thanks for your inquiry. I would suggest you please read the following article that outlines Document Tree Navigation:
http://www.aspose.com/docs/display/wordsjava/Document+Tree+Navigation
Marcel:
1. Can I retrieve the Shape from the cell (basically the other way around from what you do)?
Sure, you can use the following code snippet to obtain Shape(s) from within the Cell node:
Table tab = doc.getFirstSection().getBody().getTables().get(0);
Cell cell = tab.getFirstRow().getFirstCell();
NodeCollection shapes = cell.getChildNodes(NodeType.SHAPE, true);
Please let me know if I can be of any further assistance.

Best regards,

Super, thanks.

Is there a way to find the column width or row height without setting a (default) value?

Hi Marcel,


Thanks for your inquiry. Please note that there is no “Column” concept in the Word table. By Microsoft Word design Rows in a Table in a Microsoft Word document are completely independent. It means each row can have any number of Cells. Secondly, you can get the Row height by using the RowFormat.Height property. Please let me know if I can be of any further assistance.

Best regards,