This project has moved. For the latest updates, please go here.

Applying paragraph styles to cells in table.

Coordinator
Oct 8, 2012 at 3:55 PM

In my implementation of Html to OpenXml, I handled a request for the tables to export to Word a certain way by adding a table style to my template (blank word .docx) and HtmlConverter.ProcessTag:

properties.Add(new TableStyle() { Val = htmlStyles.GetStyle("My_Table", false) });

This used to work pretty well, but I've applied some of the recent patches of Html to OpenXml in order to fix a lot of issues I have had with ordered and numbered lists.  That issue is now essentially resolved, but I have a new problem.

Previously the export to Word would take the font size defined in my table style.  Now, it's defaulting the style in the text cells to "Normal", which is overriding the font size defined in the table style.  This isn't a problem per-se, I could just apply a new paragraph style to the cells, except I can't seem to figure out where to apply the class tag in my html in order to convert the text in my OpenXml. 

Has anybody has luck setting the class attribute in a way that alters the table cell?  If I apply a class to my <th>, <tr>, or <td> cells nothing seems to work. Html to OpenXml seems to create a special paragraph tag in those cells and applies the "Normal" style but I can't seem to figure out how to override the style.

Also., setting the "font-size" attribute directly doesn't work, because by design the code is ignoring that attribute.  (See RunStyleCollection.ProcessCommonRunAttributes for a good explanation).

Coordinator
Oct 8, 2012 at 5:30 PM

One other thing I tried was to add a style to this part of HtmlConverter.ProcessTags.ProcessTableColumn:

// we create a new currentParagraph to add new runs inside the TableCell
currentParagraph = new Paragraph();
currentParagraph.InsertInProperties(new ParagraphStyleId { Val = "TableCells" });
cell.Append(currentParagraph);

This didn't work, either, the paragraph style in the table is still "Normal".

Coordinator
Feb 27, 2013 at 3:34 PM
I'm still trying to find a solution to this issue. Recent builds of HtmlToOpenXML have helped a little bit- I figured out that I can apply Word character styles to the rows and columns (as opposed to paragraph styles). The character styles have helped, as well as modifying HtmlToOpenXml to accept Table styles as classes.

The one issue that remains is neither character styles or table styles handles line spacing or space before and after paragraphs. Table styles theoretically have spacing, but they are being overridden by the "Normal" style being auto-applied to all columns in the table. I have not found a place yet where I can insert a paragraph style into a table cell. The code seems to create a new paragraph as a container inside the cell, but I have yet to find a way to change the attributes of that paragraph to use a different style than Normal.
Coordinator
Feb 28, 2013 at 12:43 AM
Hum sounds interesting... If you accept to share your code, I will be happy to push it into the source code.

For the spacing, I can reproduce the problem using Word 2010. But I don't find the menu to correct the spacing.
If you can find how to fix using the UI, you can decompile the dock and inspect the OpenXml style...
Coordinator
Feb 28, 2013 at 1:28 PM
Sure. I am still figuring out how to use this site, I will try to make my code available for the table style. If I can't do that for some reason, all I did was add the following to HtmlConverter.ProcessTag.ProcessTable:
string classValue = en.Attributes["class"];
if (classValue != null)
{
     properties.Add(new TableStyle() { Val = htmlStyles.GetStyle(classValue, StyleValues.Table, false) });
}
I placed it right before this line:
string align = en.Attributes["align"];
As for the spacing in Word 2010, there's two ways that can be done. The first is to highlight the cells in the table and manually select a paragraph style that you have defined to have no line spacing. The second is to highlight the cells and select the "Line and Paragraph Spacing" button in the "Paragraph" section of the home ribbon. I'm still learning how work with OpenXml itself, but since table cells need to have a paragraph in them, I am assuming that they get assigned "Normal" as a style if no other style is defined for them. If I change my "Normal" style to not have spacing in it, then the spacing in the cells disappear. However, that upsets all of the other formatting I have in place for the paragraphs outside the table, and it's more difficult for me to control those styles.
Mar 23, 2013 at 5:17 AM
Edited Mar 23, 2013 at 5:18 AM
Hi guys,

First of all, thank you for this wonderful library! It has helped me immensely.

I faced this exact issue and I was able to resolve it by adding styles to each <p> tag within each <td> tag. I'm using the html agility pack to do some pre-processing before parsing my html to Open XML.
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

// process tables
HtmlNodeCollection tableNodeCollection = htmlDoc.DocumentNode.SelectNodes("//table");
if (tableNodeCollection != null)
{
    foreach (HtmlNode tableNode in tableNodeCollection)
    {
        // apply styles to table
        tableNode.Attributes.Add("style", "font: 10pt Calibri; width: auto;");

        // apply styles to paragraphs inside table
        HtmlNodeCollection pNodeCollection = tableNode.SelectNodes("//p");
        foreach (HtmlNode p in pNodeCollection)
        {
            p.Attributes.Add("style", "margin: 0; padding: 0;");
        }
    }
}

// send processed html back to string
html = htmlDoc.DocumentNode.WriteTo();
I hope this helps. The problem that I am having now is getting the table width to auto-size according to the contents. width: auto does not seem to work as the table always fills the entire document space between the margins. Any ideas on that one?
Mar 23, 2013 at 6:12 AM
Edited Mar 23, 2013 at 6:14 AM
To answer my own question, I figured this out by changing line 756 from the latest build in HtmlConverter.ProcessTag.cs from:
properties.Add(new TableWidth() { Type = TableWidthUnitValues.Pct, Width = "5000" });
to:
properties.Add(new TableWidth() { Type = TableWidthUnitValues.Pct, Width = "auto" });
Cheers!
Coordinator
Mar 25, 2013 at 6:37 PM
Ok I'm fine with the change, I commit the code
Thaink you