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

Free Support Forum - aspose.com

Resize Image to Fit within the Table Column Bounds (Width) | HTML to PDF Conversion (Java)

Dear support-Team,

we are using Aspose Words for Java, and we have HTML as input with a table.
The table has 2 columns and one of these columns has a fixed width of 50%.
Inserting an image into the column which is wider than the column leads to the image being inserted scaled down such that it can fit into the column with 50% width.
When we insert the HTML in Aspose, in the resulting docx, the image is not scaled down. Instead, the column has become larger than the configured 50%.

The table in HTML:
table-with-picture-in-cell-html.png (372 Bytes)
The table in Word:
table-with-picture-in-cell-docx.png (484 Bytes)

Please use this code to reproduce (the src-attribute to the image needs to point to a suitable image file that is larger than the column):

public class AsposeTest {
    public static void main(String[] args) {
        try {
            File file =  File.createTempFile("tempFile",".docx");
            FileOutputStream openOutputStream = FileUtils.openOutputStream(file);
            DocumentBuilder documentBuilder = new DocumentBuilder();
            documentBuilder.insertHtml("<table style=\"border-collapse: collapse; border: 1px solid black;\" border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\"><tbody><tr><td>&nbsp;</td><td style=\"width: 50%;\"><img src=\"...\"></td></tr></tbody></table>");
            documentBuilder.getDocument().save(openOutputStream, new OoxmlSaveOptions());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The problem can be reproduced in the most recent version of Aspose Words for Java 21.4.

Thanks for your support

@orgavision,

Please see following sample Word documents, HTML and Image files:

Sample Files - 229831.zip (18.0 KB)

  • Untitled.png is the image that I used for testing
  • msw-2019.docx was produced by using MS Word 2019. I opened HTMLPage1.html with MS Word 2019 and converted it to DOCX format by using the “Save As” command.
  • awjava-21.5.docx was produced by using the following code of latest (21.5) version of Aspose.Words for Java:
DocumentBuilder documentBuilder = new DocumentBuilder();
String html = "<table style=\"border-collapse: collapse; border: 1px solid black;\" border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\"><tbody><tr><td>&nbsp;</td><td style=\"width: 50%;\"><img src=\"C:\\Temp\\Untitled.png\"></td></tr></tbody></table>";
documentBuilder.insertHtml(html);
documentBuilder.getDocument().save("C:\\Temp\\awjava-21.5.docx");

So, this seems to be an expected behavior as Aspose.Words mimics the behavior of MS Word 2019 in this case - i.e. MS Word and Aspose.Words both reduce the widths of first table column/cell.

Ah sorry, unfortunately I missed a small but important detail in my example:
In our website we have a CSS-attribute max-width: 100%; for the img-tag.

So the input-html is effectively:

<table style=\"border-collapse: collapse; border: 1px solid black;\" border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\"><tbody><tr><td>&nbsp;</td><td style=\"width: 50%;\"><img src=\"...\" style="max-width:100%;"></td></tr></tbody></table>

This has the effect that the image scales down when it does not fit inside the table column, and therefore, the image is never wider than the configured 50% of the table width.
Using Aspose Words to convert the table to Word, we would like to achieve the same effect in the resulting Word document.

Thank you for your support!

@orgavision,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-22282. We will further look into the details of this requirement and will keep you updated on the status of the linked issue.

I tried to reproduce the bug in the new version 21.8, and the problem is still there.

@orgavision,

Unfortunately, your issue is not resolved yet; We have completed the analysis of this issue and the root cause has been identified. We will inform you via this forum thread as soon as this issue will get resolved in future and apologize for any inconvenience.

The status of the issue has not changed since August. Can we expect this to be resolved anytime soon?

@orgavision We have completed the analysis of this issue and most likely the defect will be closed as ‘Not a Bug’, because the problem is cause by limitations of the MS Word document model.
The responsible developer will take a look at the issue once again next week. We will let you know the resolution.

@orgavision We have completed analysis of the issue and concluded to close it as Not a Bug, because the issue is caused by a limitation of the document model and the resulting document generated by Aspose.Words looks similar to what MS Word produces.