Adding existing word document table styles to html...before loading

Hi,

I am trying to create a Aspose.Word document (.net) from html. There are couple of tables in the html. I would like to give them pre-existing/pre-defined styles from MS word. (if we open a blank MS word document…we can see that the document already has a lot of pre-defined styles like “Table Grid”, “Table Light Grid”…etc). Is there anyway I can take advantage of these styles? Can I define tables with these classes in my html and while loading…aspose will apply those styles by default?

I am looking for something like this:-


I have tried it…but it doesn’t seem to work. I was wondering if you guys can tell me if this is possible or not. If it is then point me to some examples.

Thanks,
Indu.

@Induma Unfortunately, there is no way to specify table style directly in HTML. However, you can apply table style right after importing HTML into your document. For example, you can use INodeChangingCallback to collect tables imported from HTML and then apply table style.

Document doc = new Document();

// Specify NodeChangingCallback to collect the tables inserted into the document.
NodeChangingCallback nodeChanging = new NodeChangingCallback();
doc.NodeChangingCallback = nodeChanging;

DocumentBuilder builder = new DocumentBuilder(doc);
// Insert table from HTML
builder.InsertHtml(@"<table>
        <tr>
            <td>Name</td>
            <td>Value</td>
        </tr>
        <tr>
            <td>test</td>
            <td>test</td>
        </tr>
    </table>");

// Apply table style to the inserted tables.
nodeChanging.ApplyTableStyle();

doc.Save(@"C:\Temp\out.docx");
private class NodeChangingCallback : INodeChangingCallback
{
    public void NodeInserted(NodeChangingArgs args)
    {
        // Collect tables
        if (args.Node.NodeType == NodeType.Table)
            mTables.Add((Table)args.Node);
    }

    public void NodeInserting(NodeChangingArgs args)
    {
        // Do nothing.
    }

    public void NodeRemoved(NodeChangingArgs args)
    {
        // Do nothing.
    }

    public void NodeRemoving(NodeChangingArgs args)
    {
        // Do nothing.
    }

    public void ApplyTableStyle()
    {
        foreach (Table t in mTables)
            t.StyleIdentifier = StyleIdentifier.GridTable1LightAccent1;
    }

    List<Table> mTables = new List<Table>();
}

Thank you for your quick reply.
I have one more follow up question. When we are iterating thru tables in function ApplyTableStyle(), how can we differentiate between tables? Say I have two different styles and I want to apply between different tables based on the table name or a class on the table. Is there a way where I can check the attribute names…I did not see any.
Please let me know.

Thanks,
Indu.

@Induma Unfortunately, there is no way to access the original html/style attributes through Aspose.Words DOM. The only possible workaround is parsing your original HTML document and somehow map between tables in your original HTML and tables in Aspose.Words DOM.

Thanks for the reply. Do you have any examples or can you think of a way to map them? HTML is in my control…I can add stuff there. But not sure how to retrieve it in aspose word document.

@Induma You can first load your HTML document into XML document and get tables as XML nodes, from these XML nodes you can get style attribute.
Then by XML node index you can map to the tables collected upon inserting your HTML into Aspose.Words.Document.
The idea is to suppose that index of table in your source HTML corresponds to the index of table in Aspose.Words.Document.

This is very helpful. I was able to map them up and use Table.StyleName property to set a pre-existing table style from word. The colors and font are coming out great but the size of the font and the cell spacing is way - way off. We are using Aspose.Words for .net 13.12 version. I don’t think this version supports creating new table styles. I just want to control the font size and cell spacing - to have 0pt for both before and after. Let me know if there is a way to do that. The pre-existing table style in the word document has all these, but somehow they are getting lost.

Thanks,
Indu.

@Induma I suspect the problem occurs because formatting applied to the text and cell explicitly overrides the formatting applied to the elements via table style. To work this around, you can try clearing formatting of Cell, Paragraph and Run nodes in the table before applying the style:

private static void ClearFormatting(Table table)
{
    NodeCollection cells = table.GetChildNodes(NodeType.Cell, true);
    foreach (Cell c in cells)
    {
        c.CellFormat.ClearFormatting();
        foreach (Paragraph p in c.Paragraphs)
        {
            p.ParagraphFormat.ClearFormatting();
            foreach (Run r in p.Runs)
                r.Font.ClearFormatting();
        }
    }
}