Insert HTML with CSS in Word Document & Set Font Name Size of Table of Contents (TOC Field) C# .NET

Hi,
How do you define the style of the table of contents from HTML CSS? I have tried a number of things as follows:

  • Setting the body CSS but this seems to be ignored within the ToC. I would have expcted when using InsertHTML to a blank document the base styles (Normal and Default Paragraph) do not pick up the styles defined in the body CSS of the HTML.
  • Creating Toc1 CSS class.
  • Wrapping the ToC in a div with a class set that has an explicit font.
  • Setting useBuilderFormatting on InsertHtml

Attached is a sample project that shows this occuring and also included in the zip is an expected output.
ImageSizeIssue.zip (391.8 KB)

Thanks

@pjb_maintology,

I am afraid, we could not find any TOC fields (table of contents) in AsposeOutput.docx and ExpectedOutput.docx documents. And there are no such styles in ImageSizeIssue.html file either. Can you please double check if you had attached the correct files?

Oh sorry, that was the wrong attachment!

Please try this one: AsposeToCStyleIssue.zip (42.3 KB)

It would be great if the ToC picks up the Toc1, Toc2, etc styles defiend in the CSS.

@riftsoft,

One simple way to adjust the font formatting of TOC items in Word document is by using the Aspose.Words’ DOM classes. Please check the following code:

Document doc = new Document("C:\\Temp\\AsposeToCStyleIssue\\Output.docx");

foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field;
        if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
        {
            Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);
            foreach (Run run in tocItem.GetChildNodes(NodeType.Run, true))
            {
                run.Font.Name = "Arial";
                run.Font.Size = 12;
            }
        }
    }
}

doc.Save("C:\\temp\\AsposeToCStyleIssue\\expected.docx");

Hi Awais,
I have been able to do a work around of changing the font in the Toc1, Toc2, etc. styles but we have a system where the CSS is used for user defined styling of the ToC which means there could be all sorts of different styles (fonts, weighting, size, color, and various other styling). So this work around only really corrects the lack of the default font being used in the ToC.

Is it expected for HTML CSS ToC styles to be picked up or is there a reason they are not applied in the same way other styles are from CSS (such as Headers)?

Thanks

@riftsoft,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-22425. We will further look into the details of this requirement and will keep you updated here on the status of the linked ticket. We apologize for any inconvenience.

@riftsoft We have completed analyzing the issue WORDSNET-22425. Currently Aspose.Words doesn’t allow to import TOC styles from HTML. As a workaround you can use the following approach:

  1. Add CSS style for TOC into HTML document. For example, the following style:
.CssTocStyle1 {
font-family: arial;
font-weight: bold;
font-size: 18pt;
color: red;
}
  1. Currently Aspose.Words doesn’t import CSS styles, if they aren’t used by any HTML element, so it is required to add “TOC style holder” paragraph to HTML document. For example:
<p class="CssTocStyle1">TOC style holder.</p>
  1. After HTML import it is required to replace old TOC style with new TOC style. For example:
foreach (Paragraph paragraph in doc.FirstSection.Body.Paragraphs)
{
    if (paragraph.ParagraphStyle.Name == "TOC 1")
    {
        paragraph.ParagraphFormat.StyleName = "CssTocStyle1";
    }
}
  1. Before saving to DOCX, remove “TOC style holder” paragraph. For example, if “TOC style holder” paragraph is the first paragraph in the document:
doc.FirstSection.Body.FirstParagraph.Remove();

Also see the updated version of your project: AsposeToCStyleIssueUpdated.zip (42.8 KB)