Unable to preserve Superscript and Subscripts formatting when converting from HTML to Word using Aspose.Words

Hi Aspose Team,

Referencing a past issue (Unable to preserve strikethrough formatting when converting from HTML to Word using Aspose.Words), we are encountering further inconsistencies with Aspose.Words while converting HTML to Word, particularly with superscripts and subscripts.

Issue 1: Superscripts from CSS
<span style="color: red;">m <span style="position: relative; font-size: 0.75em; line-height: 0; vertical-align: baseline; top: -0.5em;">2 </span>

Issue 2: Superscripts from HTML tag
<sup>2 </sup>

We face similar issues for subscripts.

Are there specific HTML formats or styles for these that Aspose can correctly convert to Word? We are operating under the Aspose.Total license.

Thanks,
Alex

@alexvillegasc Please note that HTML and Word file formats have distinct structures, and converting between them can sometimes yield unexpected results.

To better assist you, could you please attach the output document and indicate the specific error you are encountering? I have tested the provided HTML by inserting it into MS Word application and creating a document using Aspose.Words (v23.6.0) with the code snippet below, and I received the same result in both cases:

var html = @"<span style=""color: red;"">m <span style=""position: relative; font-size: 0.75em; line-height: 0; vertical-align: baseline; top: -0.5em;"">2 </span><sup>2 </sup>";

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

builder.Font.Name = "Arial";
builder.Font.Size = 12;

builder.InsertHtml(html);

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

If there are any modifications or additional steps required to reproduce the issue, please let me know.

output.docx (7.1 KB)
wordTest.zip (9.7 KB)

Hi Eduardo,

Thanks for the quick response. I just notice an alternative way to make the Superscript work.

Now the only concern is with Subscripts:

"<span style=\"vertical-align: sub;\">2<span>

"<sub>2</sub>"

<span style="color: red;">m <span style="position: relative; font-size: 0.75em; line-height: 0; vertical-align: baseline; bottom: -0.25em;">2 </span>

None of them are working. We are saving the file as a “.doc”

Could please double-check that case and confirm if it’s an issue, or it there’s any alternative?

Here’s the output document:
Output from HTML Convertion.zip (7.1 KB)

Thanks in advance!
Alex

@alexvillegasc Please note that, similar to MS Word, the first two methods are interpreted as subscripts by Aspose.Words API. To demonstrate this behavior, please refer to the code example below and the corresponding output:

var html = @"<p>m<span style=""vertical-align: sub; font-size: 10px"">2</span></p>
<p>m<sub style=""font-size: 10px"">2</sub></p>
<span style=""color: red;"">m <span style=""position: relative; font-size: 0.75em; line-height: 0; vertical-align: baseline; bottom: -0.25em;"">2 </span>";

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

builder.Font.Name = "Arial";
builder.Font.Size = 12;

builder.InsertHtml(html);

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

output.zip (1.9 KB)

As you can observe, both the HTML tags <span style="vertical-align: sub; font-size: 10px">2</span> and <sub style="font-size: 10px">2</sub> are interpreted as subscripts by Aspose.Words API. This behavior is consistent with the interpretation in MS Word application.
wordOutput.zip (5.7 KB)

Thanks, this one is working for Subscripts as well, appreciate your assistance!:

<span style="vertical-align: sub;">2</span>

Thanks a lot,
Alex

2 Likes