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

Free Support Forum - aspose.com

Unable to get Repeated headers in Html to Word

Hello Team,

I am using the below html and exporting that to docx and also setting the first row to header type, But i do not see the repeated header in my final word file. Can you please help me in generating the repeated header.

NodeCollection tables = theDoc.GetChildNodes(NodeType.Table, true);

            for (int i = 0; i < tables.Count; i++)
            {
                var table = tables[i] as Table;
                if (table.Rows.Count > 5)
                {
                    table.Rows[0].RowFormat.HeadingFormat = true;
                }
            }

Input html file input.zip (9.2 KB)

input html has headers in

tags.

Thanks,
Lalitya

@lalityay

Thanks for your inquiry. Please use the Table.TextWrapping as None to get the desired output.

Document theDoc = new Document(MyDir + "input.html");
NodeCollection tables = theDoc.GetChildNodes(NodeType.Table, true);

for (int i = 0; i < tables.Count; i++)
{
    var table = tables[i] as Table;
    if (table.Rows.Count > 5)
    {
        table.Rows[0].RowFormat.HeadingFormat = true;
    }
    table.TextWrapping = TextWrapping.None;
}
theDoc.Save(MyDir + "19.2.docx");

Thanks @tahir.manzoor

This is not working for the below html file. Can you please help.

input_AZ.zip (14.8 KB)

Thanks,
Lalitya

@lalityay

Thanks for your inquiry. In this case, the tables are nested in your input document. Please check the attached DOM image.

Please use following code example to get the desired output.

Document theDoc = new Document(MyDir + "input_AZ.html");
NodeCollection tables = theDoc.GetChildNodes(NodeType.Table, true);

for (int i = 0; i < tables.Count; i++)
{
    var table = tables[i] as Table;

    if (table.GetAncestor(NodeType.Table) == null)
    {
        table.Rows[0].RowFormat.HeadingFormat = true;
    }
    table.TextWrapping = TextWrapping.None;
}
theDoc.Save(MyDir + "19.2.docx");

Thanks @tahir.manzoor for the quick response. but i want to get the headers repeated for the inner tables is that possible? most of the times the entire html i want to export have nested table structure with inner tables having large data.
image.png (4.0 KB)

Thanks,
Lalitya

@lalityay

Thanks for your inquiry. The header row does not repeat for nested table. Please note that Aspose.Words mimics the behavior of MS Word. If you perform the same scenario using MS Word, you will get the same output.

@tahir.manzoor, is there any way that i can move the table to the root of the document
or
detect the page break while looping through rows and insert header row in next page ?

Thanks,
Lalitya

@lalityay

Thanks for your inquiry. Yes, you can extract the nested table from parent table’s cell and add it under Body node. Please check the following code example.

Document theDoc = new Document(MyDir + "input_AZ.html");
NodeCollection tables = theDoc.GetChildNodes(NodeType.Table, true);

foreach (Table table in tables)
{
    foreach (Table tbl in table.GetChildNodes(NodeType.Table, true))
    {
        Table newTable = (Table)table.ParentNode.InsertBefore(tbl.Clone(true), table);
        newTable.TextWrapping = TextWrapping.None;
        tbl.Remove();
    }

    if (table.ToString(SaveFormat.Text).Trim().Length == 0)
    {
        table.Remove();
    }
}

tables = theDoc.GetChildNodes(NodeType.Table, true);

for (int i = 0; i < tables.Count; i++)
{
    var table = tables[i] as Table;

    if (table.GetAncestor(NodeType.Table) == null)
    {
        table.Rows[0].RowFormat.HeadingFormat = true;
    }
    table.TextWrapping = TextWrapping.None;
}

theDoc.Save(MyDir + "19.2.docx");