DocxFormatProvider bug on export right to left span

0 Answers 16 Views
Mohammad Afghari
Top achievements
Rank 2
Mohammad Afghari asked on 09 Dec 2021, 09:46 PM | edited on 09 Dec 2021, 09:47 PM

Hi Dear Telerik Developers
It seems there is a bug on exporting RadDocument to docx using DocxFormatProvider.  The problem is when the document has a rtl span. Not only the span which we want to be rtl dose not get right to left direction, but also some other properties of the span (such as bold and italic styles) get buggy and do not work properly on Microsoft Word. 

So, after a couple of hours working on this bug (decompiling  Telerik.Windows.Controls.RichTextBox.dll via JustDecompile and searching about what is going on inside of the component)  finally is turns out that the problem is in ExportTextDirection method of SpanExporter class under Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Export namespace. This method uses "rtl" WML element which is not recognizes by Microsoft Word and should be "bidi" to perform correctly.
Thanks to OpenXML technology, currently I am using the following code as a trick to solve the problem:

private static void TestTelerik()
var document = new RadDocument();
var section = new Section();
var paragraph = new Paragraph();
paragraph.TextAlignment = Telerik.Windows.Documents.Layout.RadTextAlignment.Justify;
paragraph.FlowDirection = System.Windows.FlowDirection.RightToLeft;
var span = new Span();
span.FlowDirection = System.Windows.FlowDirection.RightToLeft;
span.Text = "متن فارسی نمومه";
span.FontWeight = System.Windows.FontWeights.Bold;
span.FontStyle = System.Windows.FontStyles.Italic;
span.FontFamily = new System.Windows.Media.FontFamily("Tahoma");
var provider = new DocxFormatProvider();
var bytes = provider.Export(document);
bytes = RefineDocx(bytes);
IO.File.Write("RTLSpanBugFix.docx", bytes);

private static byte[] RefineDocx(byte[] docx) 
var stream = new System.IO.MemoryStream(docx);
var openXml = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(stream, true);
var main = openXml.MainDocumentPart;
var result = stream.ToArray();
return result;
private static void FixBugOnRightToLeft(DocumentFormat.OpenXml.Packaging.MainDocumentPart main)
var outerXML = main.Document.OuterXml;
outerXML = outerXML.Replace("w:rtl w:val=", "w:bidi w:val=");
main.Document = new DocumentFormat.OpenXml.Wordprocessing.Document(outerXML);


Telerik team
commented on 13 Dec 2021, 11:42 AM

Hello Mohammad Afghari,

The bidi element is part of the paragraph properties and should not be applied to spans. A span could have a right-to-left or left-to-right direction determined by the rtl element. The paragraphs can contain spans with different directions, then they could be marked with bidi.

When it comes to the formatting options, it turned out that the formatting for complex script languages is not exported properly and I opened a new report for that behavior: RichTextBox: DocxFormatProvider doesn't export font weight and font style for complex script languages. A fix for it is already in development and I hope it will be released in one of our next internal builds.

Could you wait for the updated version and let us know how the control works for you with the fix? The item linked above will be updated with information about the specific version including the fix when it is done.

I have added Telerik points to your account in appreciation for bringing this behavior to our attention.

Hope this is helpful.

No answers yet. Maybe you can help?

Asked by
Mohammad Afghari
Top achievements
Rank 2
Share this question