Free Support Forum - aspose.com

Table width exceeding margins with inserthtml

We are inserting HTML code from HTML file into word document. Problem is that table is exceeding page and even when we set table to “autofit to window” in generated word document (manually in office), width is not corrected. Please find screenshot and files used

public static void InsertHTML(String FileOut)
{
    StreamReader sr = new StreamReader(@"C:\Users\raja_c\Downloads\temp table.html");
    String html = sr.ReadLine();
    html= html+ sr.ReadLine();
    sr.Close();
    Document doc = new Document();
    DocumentBuilder db = new DocumentBuilder(doc);
    db.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    db.Writeln("Tables");
    db.InsertHtml(html);
    doc.Save(FileOut);
}

image.png (5.4 KB)

tables.zip (8.4 KB)

@crshekharam

Please use the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document();
DocumentBuilder db = new DocumentBuilder(doc);
db.ParagraphFormat.Alignment = ParagraphAlignment.Center;
db.Writeln("Tables");
db.InsertHtml(File.ReadAllText(MyDir + "temp table.html"));

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{        
    table.AutoFit(AutoFitBehavior.AutoFitToWindow);
}

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.Compliance = OoxmlCompliance.Iso29500_2008_Strict;
doc.Save(MyDir + "21.5.docx", options);