Free Support Forum - aspose.com

RichText - subscript and superscript - incorrect rendering

Hi

We have noticed an issue with rendering of RichText data with regards to subscripts and superscripts. Subscript and superscript formatting is not appearing in the document. For example the rich text shown in the attached "Subscrip-superscript Rendering.png" appears as "test case title12".

The RichText data is stored as a byte[]. We use the following code to convert the RichText data to a Aspose.Words.Document:

//Get bytes for this rich text content

byte[] rtfBytes = this.GetRichTextBytes();

//Create memorystream

using (MemoryStream rtfStream = new MemoryStream(rtfBytes))

{

//Create document from stream

Document rtfDoc = new Document(rtfStream);

//Insert rtd document into destination document

this.InsertDocument(parentParagraph, rtfDoc);

}

Do you see any problems with this approach which might be causing the subscript and superscript to not render correctly?

(I can confirm that the RichText data is stored correctly because our application's RichText editor controls display the text correctly.)

Thanks in advance.

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

Thanks for your request. I cannot reproduce the problem on my side using the latest version of Aspose.Words (9.3.0). You can download this version from here:

http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/category1188.aspx

I use the following code for testing:

//Get bytes for this rich text content

byte[] rtfBytes = File.ReadAllBytes("in.txt");

//Create memorystream

using (MemoryStream rtfStream = new MemoryStream(rtfBytes))

{

//Create document from stream

Document rtfDoc = new Document(rtfStream);

rtfDoc.Save("out.rtf");

}

The input and the output documents are attached.

Best regards,

Hi

Thanks for the response. Unfortunately, we are limited to using 9.1.0 for our project. Is there any way for you to verify if this problem existed with 9.1.0?

Thank you.

Hi Sunny,

Thanks for your inquiry.

I have tested the same situation again using Aspose.Words 9.1 and Andrey's code and documents. I was unable to reproduce the issue. I also went a step further and tried to reproduce the issue by inserting the rtf document into another document but was still unable to reproduce any problems.

The code I used is below:

Document doc = new Document();

//Get bytes for this rich text content

byte[] rtfBytes = File.ReadAllBytes(dataDir + "in.txt");

//Create memorystream

using (MemoryStream rtfStream = new MemoryStream(rtfBytes))

{

//Create document from stream

Document rtfDoc = new Document(rtfStream);

rtfDoc.Save(dataDir + "out1.rtf");

InsertDocument(doc.FirstSection.Body.FirstParagraph, rtfDoc);

doc.Save(dataDir + "out2.rtf");

}

Could you please attach your template and output documents here for testing?

Thanks,

Hi

I have some more information about this issue.

The rich text data control we are using is actually a WPF FlowDocument. I was able to reproduce the issue with a small WPF application (see the attached WpfApplication1.zip). The FlowDocument content is extracted and saved to a stream, which is then saved as a document. (The output files are saved in the project's "bin" directory.)

Is there a particular way to handle subscripts and superscripts or is there a workaround?

Thank you.

Hi Sunny,

Thanks for the additional information and the premade solution. I was able to to reproduce the issue but as it appears the problem is not with Aspose.Words, but with the RichTextBox control itself. If you save the output from the TextRange to disk using for example:

FileStream stream = new FileStream("out.rtf", FileMode.Create);

textRange.Save(stream, DataFormats.Rtf);

and open the document in MS Word then you will see that the super/subscript formatting does not appear in the saved output from the control.

I did see however saving as the XAML format from the control does output a file which retains the formatting. Unfourtantely Aspose.Words does not support loading XAML, only saving to the XAML format (currently in beta). Most likely loading of this format will be supported some time in the future.

From what it looks like you are trying to achieve, there is only one work around I can suggest right now. If this is applicable, you could instead use a Windows Form and use the RichTextBox control. I did a quick test and saving content from this control into a stream and pasing this to an Aspose.Words document object will preserve the super/subscript formatting.

For example you could use this code:

MemoryStream stream = new MemoryStream();

System.Windows.Forms.RichTextBox richTextBox = new System.Windows.Forms.RichTextBox();

richTextBox.SaveFile(stream, RichTextBoxStreamType.RichText);

Document doc = new Document(stream);

doc.Save("Document out.doc");

Thanks,