Table Styles - how to manually set them on Table object

Hi,

I need to apply styles to .NET Table object based on CSS properties that are supported by Word (there are quite a few of them). Could you tell me how I can do it to have them set on Table object and inherited by its descendants?

I can see Word does so when html file opened using Word. Please see the html example.

Thanks,
LukaszTableStyles.zip (522 Bytes)

@acturisaspose,

We need your expected output MS Word file. Please ZIP and attach your expected Word document (DOCX file) showing the correct output for our reference. Please create this expected document by using MS Word. We will investigate your expected document and update you about our findings.

You may also visit our online documentation for details on HTML.

Hi Ikram,

Please see both .html and .docx attached.

Thanks,
LukaszTableStyles.zip (9.8 KB)

@acturisaspose,

Thanks for your inquiry. We tested the scenario and have managed to reproduce the Table formatting problems during converting HTML to DOCX. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-16960. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Hi,

I think we might have misunderstood each other. Unfortunately conversion from html to docx does not match our needs, as we are creating a new document programatically and dynamically, based on user input.

The problem I am facing is that I am not able to set style properties on a .NET Table object. The files I provided contain only a small set of properties I would like to set when creating a Table object. For example I have a user html input

<html>
<body>
	<table style="background-color: blue; font-size: 20pt; text-align: left;" width="100% ">
		<tbody>
			<tr>
				<th width="30%">Product</th>
				<th width="70%">Insurer</th>
			</tr>
			<tr>
				<td width="30%">Aicraft</td>
				<td width="70%">Allianz</td>
			</tr>
		</tbody>
	</table>				
</body>

I have been trying to set these when constructing a table, but with no effect. Can you tell how should I do it?

        var doc= new Document();
        var builder = new DocumentBuilder(doc);
        var table = builder.StartTable();
        builder.InsertCell();
        builder.Write("Row 1, Cell 1 Content.");
        table.Style.ParagraphFormat.Alignment = ParagraphAlignment.Right;
        table.Style.Font.Size = 20;
        builder.EndRow();
        builder.EndTable();
        builder.Document.Save(@".\test2.docx");

Thanks,
Lukasz

@acturisaspose,

We would suggest you please pass the HTML string directly to DocumentBuilder.InsertHtml method and let Aspose.Words reflect Table formatting from HTML to inside Word document. Please try using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

HandleNodeChanging handler = new HandleNodeChanging();

doc.NodeChangingCallback = handler;

builder.InsertHtml(File.ReadAllText(MyDir + "HTMLPage1.html"));

// Get Table(s) reference that we just inserted via InsertHtml for any further manipulation
foreach (Table table in handler.InsertedTables)
    foreach (Row row in table)
        foreach (Cell cell in row)
            Console.WriteLine(cell.ToString(SaveFormat.Text));            

doc.Save("D:\\Temp\\18.6.docx");
//////////////////////////////////////
public class HandleNodeChanging : INodeChangingCallback
{
    void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
    {
        if (args.Node.NodeType == NodeType.Table)
            mInsertedTables.Add(args.Node);
    }

    void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
    {
        // Do Nothing
    }

    void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
    {
        // Do Nothing
    }

    void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
    {
        // Do Nothing
    }

    public List<Node> InsertedTables
    {
        get { return mInsertedTables; }
    }

    private readonly List<Node> mInsertedTables = new List<Node>();
}

P.S. We have saved your HTML string into a HTMLPage1.html file.

Hi,

Thank you for your response. Unfortunately we cannot use InsertHtml as well, because it does not suit our needs. It does not support mso fields we use and looking at the solution provided by you the output document does not match the output when html opened with Word.

Could you please tell me if there is a way to manually set individual style properties on a Table object?

Thanks,
Lukasz

@acturisaspose,

You can use Table.Style property for this purpose. Also, RowFormat class represents all formatting for a table row. And CellFormat class represents all formatting for a table cell.

Hi,

As I stated earlier, I tried using Table.Style to set formatting, however, it does not work. Can you advise how it can be used?

    var doc= new Document();
    var builder = new DocumentBuilder(doc);
    var table = builder.StartTable();
    builder.InsertCell();
    builder.Write("Row 1, Cell 1 Content.");
    table.Style.ParagraphFormat.Alignment = ParagraphAlignment.Right;
    table.Style.Font.Size = 20;
    builder.EndRow();
    builder.EndTable();
    builder.Document.Save(@".\test2.docx");

thanks,
Lukasz

@acturisaspose,

You need to specify these formatting on Paragraph and Run nodes instead. Please use the following code:

var doc = new Document();
var builder = new DocumentBuilder(doc);
var table = builder.StartTable();

builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");
builder.EndRow();
builder.EndTable();

foreach(Paragraph para in table.GetChildNodes(NodeType.Paragraph, true))
{
    para.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}

foreach (Run run in table.GetChildNodes(NodeType.Run, true))
{
    run.Font.Size = 20;
}
            
doc.Save("D:\\Temp\\18.6.docx");

@acturisaspose,

Regarding WORDSNET-16960, we do not see any HTML-related bugs here. Aspose.Words produces a document that is closer to the original HTML than the document generated by MS Word. In MS Word’s generated document we see the following defects:

  • Left table border width is too narrow, meaning that the “border-left-width: 20pt” style is imported incorrectly.
  • Text in the table is white instead of black.
  • Text in the first row is centered, while in browsers it is left-aligned.

Regarding the “.table1” class formatting, neither Aspose.Words, nor MS Word apply it to the imported table as a table style. Instead, both programs apply table formatting directly. The “table1” style created by MS Word on import is a paragraph style that cannot be applied (and is not applied in fact) to a table, and it is never used in the resulting document.

As far as we understand, you are not happy with the fact that table-wide formatting is applied directly, not via a table style. This happens because table style management is not yet implemented in Aspose.Words and we cannot work with table styles until the dependent issues WORDSNET-5312 and WORDSNET-9641 are resolved. Until then, you should also use paragraph styles or direct formatting when working with tables:

<table style="background-color: blue; font-size: 20pt; text-align: right">
    <td>Row 1, Cell 1 Content.</td>
</table>

C# Code:

var builder = new DocumentBuilder();
 
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Font.Size = 20;
builder.Font.Color = Color.Black;
builder.CellFormat.Shading.BackgroundPatternColor = Color.Blue;
 
var table = builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");
builder.EndRow();
builder.EndTable();
builder.Document.Save("out.docx");

The implementation of this issue has been postponed until the issues (WORDSNET-16960 depends upon) are resolved and Aspose.Words can work with table styles.