When we inserted an HTML table into a table cell by aspose.word 24.09 , the table was compressed and deformed, and the width of the table was not specified in the HTML .
the problem of style As shown in the red box below 。
image.png (21.9 KB)
source html file as bellow zipped .
1.7z (495 Bytes)
document of word as bellow
1.docx (22.3 KB)
code as bellow
Document doc = new Document("c:\\temp\\1.docx", new Aspose.Words.Loading.LoadOptions() { LoadFormat = LoadFormat.Auto });
// doc.Save("d:\\1.html", SaveFormat.Html);
DocumentBuilder db = new DocumentBuilder(doc);
var htmlContenxt = File.ReadAllText("c:\\temp\\1.html" ,Encoding.UTF8 );
db.MoveToCell(0, 0, 1, 0);
db.InsertHtml(htmlContenxt, HtmlInsertOptions.RemoveLastEmptyParagraph);
//db.MoveToDocumentEnd();
//db.InsertHtml("<p style=\"margin-top:0pt; margin-bottom:0pt; text-align:justify; font-size:12pt\"><span style=\"font-family:宋体\">Testing</span><span> </span><span style=\"font-family:宋体\">Result</span><span> 1.2</span><span style=\"vertical-align:sub\">kg</span></p>", HtmlInsertOptions.RemoveLastEmptyParagraph | HtmlInsertOptions.UseBuilderFormatting);
doc.Save("c:\\temp\\style.docx", SaveFormat.Docx);
@wengyeung
Summary
Inserting an HTML table that has no explicit width information into a Word table cell with DocumentBuilder.InsertHtml causes Aspose.Words to auto‑fit the HTML table to the width of the surrounding cell. Because the HTML does not define column widths, the library compresses the table, producing a distorted appearance.
Why it happens
- When an HTML table is inserted, Aspose.Words treats it as auto‑fit unless the HTML supplies
width attributes or CSS width rules.
- The default
HtmlInsertOptions you are using (RemoveLastEmptyParagraph) does not preserve the original layout, so the layout engine recalculates the table to match the cell width.
- Saving the document can trigger another layout pass, which may further shrink the table, especially when the table is nested inside another table cell.
What to do next
- Add explicit widths to the HTML (preferred solution)
Edit the source HTML (1.html) and give the table or its columns a fixed width, for example:
<table style="width:500pt;">
<col style="width:150pt;">
<col style="width:200pt;">
<col style="width:150pt;">
…
</table>
Any combination of width attributes, inline CSS, or a <colgroup> will work.
- Preserve the original HTML formatting when inserting
Document doc = new Document(@"c:\temp\1.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
string html = File.ReadAllText(@"c:\temp\1.html", Encoding.UTF8);
builder.MoveToCell(0, 0, 1, 0); // target cell
builder.InsertHtml(
html,
HtmlInsertOptions.RemoveLastEmptyParagraph |
HtmlInsertOptions.KeepSourceFormatting); // keep original layout
KeepSourceFormatting tells Aspose.Words not to auto‑fit the table to the cell.
- Force a fixed‑width layout after insertion (if you cannot modify the HTML)
// Insert the HTML first
builder.InsertHtml(html, HtmlInsertOptions.RemoveLastEmptyParagraph);
// Locate the table that was just inserted
Table insertedTable = (Table)doc.GetChild(
NodeType.Table,
doc.GetChildNodes(NodeType.Table, true).Count - 1,
true);
// Apply fixed‑column behavior
insertedTable.AutoFit(AutoFitBehavior.FixedColumnWidths);
// Optionally set preferred widths for each column
foreach (Row row in insertedTable.Rows)
{
foreach (Cell cell in row.Cells)
{
// Example: set each cell to 100 points (adjust as needed)
cell.CellFormat.PreferredWidth = PreferredWidth.FromPoints(100);
}
}
This forces the table to keep the widths you assign, preventing automatic compression on save.
-
Check the behavior with a newer Aspose.Words version
The same auto‑fit behavior has been reported in versions such as 24.09. Running the same code on a more recent build (e.g., 25.x) may show different results. If the newer version resolves the issue, consider upgrading; otherwise, the steps above provide a reliable workaround.
-
Validate the result
- Open the generated
style.docx and verify that the inserted table retains the expected column widths.
- If you need to export to PDF, check that the layout remains unchanged.
Checklist
- [ ] Add explicit
width attributes or CSS to the HTML table/columns.
- [ ] Use
HtmlInsertOptions.KeepSourceFormatting when calling InsertHtml.
- [ ] If editing the HTML is not possible, locate the inserted
Table node and apply AutoFit(AutoFitBehavior.FixedColumnWidths) plus appropriate PreferredWidth settings.
- [ ] Test the document after saving to confirm that the table is no longer compressed.
- [ ] (Optional) Run the same code on the latest Aspose.Words version to see whether the behavior has changed.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
@wengyeung
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28817
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Thank you very much for your prompt response. As we were working on other documents, we discovered that some content disappeared after inserting a section of HTML into the document. Can we see if we can handle it all together?
The source HTML document is compressed as follows, with the same code as above
2.7z (3.6 KB)
The following image shows the normal content of HTML.
image.png (56.3 KB)
After generating the report, the content corresponding to the location is missing.
image.png (134.2 KB)
@wengyeung As I can see the provided HTML snippet was produced by Aspose.Words and then modified. The mentioned text has -aw-import:ignore attribute, which instructs Aspose.Words to skip the html node upon importing HTML.
<span style="font-family:Arial; -aw-import:ignore">For #5744, #5741, #5746</span>
So the behavior is expected.
Dear Sir
You mean the aw-ignore attribute is converted from Word base on Aspose.Word component ,so what type of element in word document would be converted as a aw-ignore attribute ?
@wengyeung -aw-* CSS properties is Aspose.Words attempt to preserve MS Word roundtrip information in HTML.