Image not fit within table cell

Hi team,

I tested on Aspose.Words.For.Java 14.4.

If I create a table with N cells in a row (assume one row and 4 cells) and insert image, the image is not automatically fit within the cell width. Note, I don’t set cell width, image height/width. Is there a way to specify to fit images automatically within cell width/height?

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry.

Please use the following code example to resize the image with proportional height/width according to table’s cell. Hope this helps you. Please let us know if you have any more queries.


Document doc = new
Document(MyDir + “in.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);

Cell cell = (Cell)doc.getChild(NodeType.CELL, 0, true);

CellFormat format = cell.getCellFormat();

format.setLeftPadding(0.0);

format.setRightPadding(0.0);

format.setTopPadding(0.0);

format.setBottomPadding(0.0);

format.setWrapText(false);

format.setFitText(true);

double width = format.getWidth();

double height = cell.getParentRow().getRowFormat().getHeight();

builder.moveTo(cell.getFirstChild());

Shape image = builder.insertImage(MyDir + "Chrysanthemum.jpg");

double freePageWidth = width;

double freePageHeight = height;

// Is one of the sides of this image too big for the page?

ImageSize size = image.getImageData().getImageSize();

boolean exceedsMaxPageSize = size.getWidthPoints() > freePageWidth || size.getHeightPoints() > freePageHeight;

if (exceedsMaxPageSize)

{

// Calculate the ratio to fit the page size based on which side is longer.

boolean widthLonger = (size.getWidthPoints() > size.getHeightPoints());

double ratio = widthLonger ? freePageWidth / size.getWidthPoints() : freePageHeight / size.getHeightPoints();

// Set the new size.

image.setWidth(size.getWidthPoints() * ratio);

image.setHeight(size.getHeightPoints() * ratio);

}

doc.save(MyDir + “Out.docx”);

Hi Tahir,

I don’t want to load a word document that is already created. I want to work on the document that is being built using com.aspose.DocumentBuilder API. Is there a way to fit image within cell when cell width is not set?

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. Table.AllowAutoFit property allows Microsoft Word and Aspose.Words to automatically resize cells in a table to fit their contents. Unfortunately, this does not work correctly.

If table’s option ‘Automatically resize to fit contents’ is off in input document and insert an image in table’s cell, the image is resized. Aspose.Words does not mimic this behavior.

For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10174. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-10174) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.