@e.scanlan Please try modifying your code like this:
s.Width = c.CellFormat.Width - c.CellFormat.LeftPadding - c.CellFormat.RightPadding;
@e.scanlan Please try modifying your code like this:
s.Width = c.CellFormat.Width - c.CellFormat.LeftPadding - c.CellFormat.RightPadding;
Thank you @alexey.noskov now the images are correctly contained in the tables, this is great!
I have a remaining question please:
When creating a table in Word using Aspose, the table margins appear within the text boundary instead of aligning correctly. Could you clarify why this happens and suggest how to ensure the table respects the expected page margins?
The first 2 tables in the image are Aspose, the third table is Word. We would like to have the Aspose tables looking like the last Word table.
@e.scanlan Looks like cell left padding affects the position of the table. Please try modifying your code like this:
string htmlContent = File.ReadAllText(@"C:\Temp\in.html");
// Create a new Word document
Aspose.Words.Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert the HTML content into the document
builder.InsertHtml(htmlContent);
foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
{
foreach (Shape s in t.GetChildNodes(NodeType.Shape, true))
{
s.AspectRatioLocked = true;
s.Width = 1;
}
t.AutoFit(AutoFitBehavior.AutoFitToWindow);
t.AutoFit(AutoFitBehavior.FixedColumnWidths);
t.LeftIndent = t.FirstRow.FirstCell.CellFormat.LeftPadding; // <---- this line has been added
foreach (Shape s in t.GetChildNodes(NodeType.Shape, true))
{
Cell c = s.GetAncestor(NodeType.Cell) as Cell;
if (c != null)
{
s.AspectRatioLocked = true;
s.Width = c.CellFormat.Width - c.CellFormat.LeftPadding - c.CellFormat.RightPadding;
}
}
}
// Save the document as a Word file
doc.Save(@"C:\Temp\out.docx");
Thank you @alexey.noskov
What we would like to see is the table layout exactly matching the word default table behavior.
Observation : With the new fix the Left Indent is set to 0.22 cm in the Aspose generated table. The default table inserted directly in word does not contain Left Indent.
Expected Outcome :
The table margin should be aligned to the default table behavior in word.
The content in the first column of the table (img, Text etc) should be aligned to the text outside the table (e.g. tttttt text in the image attached).
@e.scanlan The tables in your documents are created from HTML and then updated through the code, that affects the tables. If you would like the table to follow default MS Word behavior, the table should be built from scratch.
As it was mentioned above, Aspose.Words is designed to work with MS Word documents. HTML documents and MS Word documents object models are quite different and it is not always possible to provide 100% fidelity after conversion one format to another.