Free Support Forum - aspose.com

HtmlExportCssStyleSheetType

When I set the HtmlExportCssStyleSheetType property to CssStyleSheetType.Embedded, almost all styles are still Inline. Hence, the file gets very large (repeating the same inline style on every cell)

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for your reasonable criticism.

There are many reasons why some formatting appears inline. Currently not all styles are output to the CSS. Only paragraph and character styles are there. List styles are partially analyzed but still output inline. Table styles are completely not supported in the document model. We plan to implement them but I expect this is not soon.

Also there can be some direct formatting in MS Word document that definitely goes inline. In well-designed documents everything is based on styles and direct formatting is minimized. Probably we can suggest changes in the document itself.

In general, for improving table formatting output we need first some improvements in the document model, particularly table styles. As a workaround you can post-process the output to reduce redundancy if you assume any affordable constraints.

You can also try calling Document.JoinRunsWithSameFormatting method before you save as HTML. In some documents the cause of redundancy is that text with same formatting is split into smaller pieces. The problem is explained in this thread:

http://www.aspose.com/community/forums/thread/120232.aspx

I can advise you more if you attach the document(s) here. Please show what specifically is important for your application and brings much redundancy. Your feedback is much appreciated.

Regards,

Thanks for the quick response!
Our document is entirely built from code. Here is an example:

Private Sub Test()
Dim doc As New Document()
doc.SaveOptions.HtmlExportCssStyleSheetType = CssStyleSheetType.Embedded
doc.SaveOptions.ExportPrettyFormat = True
Dim builder As New DocumentBuilder(doc)

'Set formating
builder.CellFormat.WrapText = False
builder.CellFormat.Shading.BackgroundPatternColor = ColorTranslator.FromHtml("#969696")
builder.Font.Color = Color.White
builder.Font.Size = 8
builder.Font.Name = "Tahoma"
builder.Font.Bold = True
builder.ParagraphFormat.KeepWithNext = True


For rowIndex As Integer = 1 To 10
For cellIndex As Integer = 1 To 5
builder.InsertCell()
builder.Write("test")
Next
builder.EndRow()
Next
builder.EndTable()

Response.Clear()
Context.Response.AddHeader("Content-Disposition", "attachment; filename=""test.mht""")
doc.Save(Response.OutputStream, SaveFormat.Mhtml) 'We use mhtml to embed images.
Response.End()
End Sub

The problem is the recurring style on the TD, SPAN and P elements. On large tables the file becomes huge. Perhaps it is possible to post-process the output, but the "quoted-printable" encoding of mht documents might make that difficult (?).

Hi again!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I see the problem. You don’t use styles and all formatting comes out inline.

I started writing a workaround for you but found another issue. In some cases Aspose.Words omits attribute export in CSS styles. I have logged this issue as #5257 in our defect database and already fixed in the current codebase. Before the very next hotfix you need also a workaround for this one. For resilience I added one more attribute to paragraph format of the style in my code. I commented all in detail. It is written in C# but I hope everything is straightforward here.

Let me know whether this helps for your solution.

public void TestHtmlWithStyle()

{

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Set embedded CSS type to reduce resulting document size.

// In production environment remove ExportPrettyFormat for the same purpose.

// It is good for debugging and troubleshooting only.

doc.SaveOptions.HtmlExportCssStyleSheetType = CssStyleSheetType.Embedded;

doc.SaveOptions.ExportPrettyFormat = true;

// Create a new style. This will add it to the document automatically.

// Paragraph styles can keep both paragraph and character properties.

// "MyTableCells" is just a name. You can give it any meaningful name.

Style myTableCells = doc.Styles.Add(StyleType.Paragraph, "MyTableCells");

// Set formatting that can be kept in style.

myTableCells.Font.Color = Color.White;

myTableCells.Font.Size = 8;

myTableCells.Font.Name = "Tahoma";

myTableCells.Font.Bold = true;

myTableCells.ParagraphFormat.KeepWithNext = true;

// Workaround for #5257: This adds a back-reference to the style definition.

// You can remove this line either after very next release or if you add several

// paragraph attributes above.

myTableCells.ParagraphFormat.Style = myTableCells;

// Set our new style as current paragraph style of the builder.

builder.ParagraphFormat.Style = myTableCells;

// Set formatting that should be inline due to current limitations in Aspose.Words.

builder.CellFormat.WrapText = false;

builder.CellFormat.Shading.BackgroundPatternColor = ColorTranslator.FromHtml("#969696");

// Add some "Hello World" stuff to the document.

for (int rowIndex = 0; rowIndex < 10; ++rowIndex)

{

for (int cellIndex = 0; cellIndex < 5; ++cellIndex)

{

builder.InsertCell();

builder.Write("test");

}

builder.EndRow();

}

builder.EndTable();

// Save the document. I tried on files just to look them easier.

// Switch to Response once you like everything.

doc.Save("sample.html");

doc.Save("sample.mhtml");

}

Best regards,

Thank you very much, the customer service here at Aspose is truly fast.
I was not aware that a Style Class existed, using it reduces the file size significantly.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Is it possible to do the following with styles also?

builder.CellFormat.Borders[BorderType.Left].LineStyle = LineStyle.Single;
builder.CellFormat.Borders[BorderType.Left].Color = ColorTranslator.FromHtml("#EAEAEA");
builder.CellFormat.Borders[BorderType.Left].LineWidth = 1;

builder.CellFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.Single;
builder.CellFormat.Borders[BorderType.Bottom].Color = ColorTranslator.FromHtml("#EAEAEA");
builder.CellFormat.Borders[BorderType.Bottom].LineWidth = 1;

builder.CellFormat.Borders[BorderType.Right].LineStyle = LineStyle.Single;
builder.CellFormat.Borders[BorderType.Right].Color = ColorTranslator.FromHtml("#EAEAEA");
builder.CellFormat.Borders[BorderType.Right].LineWidth = 1;

builder.CellFormat.Borders[BorderType.Top].LineStyle = LineStyle.Single;
builder.CellFormat.Borders[BorderType.Top].Color = ColorTranslator.FromHtml("#EAEAEA");
builder.CellFormat.Borders[BorderType.Top].LineWidth = 1;

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for your kind words.

Unfortunately, cell formatting cannot be moved to styles since table styles are not supported yet. We’ll provide this support in the future.

Regards,

The issues you have found earlier (filed as 5257) have been fixed in this update.