Insert HTML in Word Document | Scale Images with Widths in Percentage Relative to Cell | C# .NET

We have HTML that contains a table (styled to 100% width) with 10 cells, each cell contians an image with a 100% width style. Each image is 100px wide. When using DocumentBuilder.InsertHTML() the images are rendered at original width causing the table to go beyond the edge of the page. Where as the HTML viewed in a browser limits the table to 100% width and forces the images to occupy 100% of the available space within each cell, effectivly about 50% reduction in original image size.

I know that this mimics the behaviour of opening the same HTML in MS Word but is there a generic way around this (this is one of numerous tables containing images)? I have tried to detect the size of the parent container of the Shape object but this is set to the over size as the image is already rendered within the cell causing it to have the same size as the original image.

Sample .NET Code:

var doc = new Document();
var builder = new DocumentBuilder(doc);
builder.InsertHtml(_bodyHtml);
doc.Save("Test.docx", SaveFormat.Docx);

Thanks

@pjb_maintology,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified HTML file containing the Table with Images
  • Aspose.Words generated output DOCX document showing the undesired behavior
  • Your expected Word file showing the desired output. You can create this file manually by using MS Word or any other editor.

As soon as you get these pieces of information ready, we will then start further investigation into your scenario and provide you more information.

Hi Awais,
Attached is a zip with the files you requested.

Thanks
ImageSizeIssue.zip (391.8 KB)

@pjb_maintology,

We have logged your requirement/problem in our issue tracking system. Your ticket number is WORDSNET-22320. We will further look into the details of this issue and will keep you updated here on the status of the linked ticket. We apologize for any inconvenience.

Any updates on this?

@maartenkelder The issue is considered as not a bug. Aspose.Words mimics MS Word behavior.
A shape can’t be placed in a cell with relative of cell width(height) scale.

It is required scale images during import from Html. For example , the following code changes sizes of shapes and the resulting document:

var doc = new Document();
var builder = new DocumentBuilder(doc);
builder.InsertHtml(_bodyHtml);
foreach (Node node in doc.GetChildNodes(NodeType.Shape, true))
{
    Shape shape = (Shape)node;
    // In case LockAspectRatio is set.
    shape.Width /= 2.0;
}
doc.Save("Test.docx", SaveFormat.Docx);

There is no generic way to do such calculation for any table. It depends on page width, table width, cell width and etc.

Thanks for your reply. I think my case is a little different so i’ve created a new ticket here.

@maartenkelder I have already answered in the mentioned topic.