We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Centering images in cells

I have to add a list of images to a document.

Here are the requirements

  • A table (which does not exist in the document, and should be added by code) should contain these images
  • table should occupy all available width in the page
  • there may be one or two images per row, according to some context information.
  • each column should be given 50% of the available space
  • when a single image is to be added to a row, the two cells should be merged, and the image should use the full resulting space
  • Images may have different sizes and aspect ratio
  • images should be scaled when larger than the cell
  • images should be centered (either vertically or horizontally, according to their size) in the containing cell

So actually the main issue is this: I cannot find out a way to get the effective width of the column to be able to resize the image if need be. I’ve tried to set PreferredWidth as percentage, but then all widths are always 0, both for cells and the table itself.

here is a sample of the required result
images-in-table.zip (911.4 KB)

Is it possible to get that?


np I’ve found the correct settings for the table and cells
Just one more question: is there a way to have a table automatically fill up the full width of the page as fixed value instead of percentage, apart from manually (i.e. pagewidth - left margin - right margin) setting that value?


Thanks for your inquiry. In your case, we suggest you please use the Table.AutoFit(AutoFitBehavior.AutoFitToWindow) method to resize the table and cells according to the page width. Please check the following code example.

// Open the document
Document doc = new Document(dataDir + "TestFile.doc");

Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// Autofit the first table to the page width.

// Save the document to disk.
doc.Save(dataDir + "TestFile.AutoFitToWindow Out.doc");