HTML formatting in not rendered as expected in Aspose.Words

Hi Team,

We are using below syntax to render html in our word document.

<<html [html_text_expression] -sourceStyles>>

Above syntax renders html fine in the document but font size and font-family is getting changed from source.

In our usecase, we have contentcontrol(which is replaced by html content) font as “Calibri” and size as “11px” and in html text we don’t have any formatting but when we generate document using Aspose linq engine it applies different size (12px) and fonts (Times New Roman).

Sample Html Content:

This is rich text field - 1
<ul><li>Rich Text Field - 1</li></ul>

<ol><li>This is rich text field's content.</li></ol>

Could someone please help us to know how or from where it is getting applying to html content and how can we resolve it.

@RajmalConga In your case to get the expected output you should disable -sourceStyles switch. it makes the engine to use corresponding styles of a template document. This makes content of a result document look more consistent. For example see the following code and difference in the output document:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Calibri";
builder.Font.Size = 14;
builder.Writeln("<<html [html_text_expression] -sourceStyles>>");
builder.Writeln("<<html [html_text_expression]>>");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, "<ul><li>Rich Text Field - 1</li></ul>", "html_text_expression");

doc.Save(@"C:\Temp\out.docx");

Hi @alexey.noskov,

Thanks for the quick response.

We have removed switch (sourceStyles), font and styling issue gets resolved but again we are getting issue when we have a hyperlink inside html.

I have already created a below ticket earlier and we got a solution to add switch to make it work.

https://forum.aspose.com/t/rendering-html-string-in-to-the-aspose-word-document-using-linq-reporting-engine/261065

Now, We want both the scenarios to be working together because both are part of html. By doing this right now either one scenario is working but we want both scenarios to be working together.

Could you please help us to resolve this?

Thanks,
Rajmal Dhakad

@RajmalConga

The behavior of -sourceStyles is straightforward: when it is missing, the engine uses formatting defined in a template, when the switch is present, the engine uses formatting from HTML. It turns out that a mixture of the both is needed: some formatting should come from a template, whereas the rest should come from HTML. I am afraid, we are not going to provide such an option through template syntax and would like to suggest considering one of the alternatives:

  1. Inject font name and size information into HTML and continue using -sourceStyles.
  2. Instead of inserting HTML directly, load it into a document, modify formatting of the document on the fly, and then insert that document. Here is how this can be done:
    1. Instead of <<html [value] -sourceStyles>>, use <<doc [Util.LoadHtml(value)] -sourceStyles>> within a template.
    2. Use the following code:
    ReportingEngine engine = new ReportingEngine();
    engine.KnownTypes.Add(typeof(Util));
    engine.BuildReport(...);

    //-------------------------------------------------------
    public static class Util
    {
        public static Document LoadHtml(string html)
        {
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(html));
            Document document = new Document(stream);
            // Do whatever is needed with document formatting here.
            return document;
        }
    }