4
votes

How can I style a Run and/or Paragraph to support both RTL and LTR words?

The problem is: I have a complex text which contains both Persian and English words, and I'm trying to create a .docx document using OpenXML SDK. But, the English words, get RTL too. Assume I have this text:

این متن Javad Amiry انگلیسی ست و باید چپ به راست شود.

I'm expecting to place the text in document like this:

enter image description here

But I'm getting this one:

enter image description here

As you can see, English words are vise-versa! Means, while I'm expecting Javad Amiry, I'm getting davaJ yrimA! Do you have any idea to fix that?

The style I'm using is shown below:

var text = "این متن Javad Amiry انگلیسی ست و باید چپ به راست شود.";
var par = new Paragraph();
var pPr = new ParagraphProperties(
    new BiDi(),
    new Justification { Val = JustificationValues.Both },
    new RunFonts {
        Ascii = "Arial",
        HighAnsi = "Arial",
        EastAsia = "Arial",
        ComplexScript = "B Mitra",
        Hint = FontTypeHintValues.ComplexScript,
    },
    new FontSize { Val = "24" },
    new FontSizeComplexScript { Val = "24" },
    new Languages { Bidi = "fa-IR", Val = "en-US", EastAsia = "en-US" }
);

var rPr = new RunProperties(
    new RightToLeftText(),
    new RunFonts {
        Ascii = "Arial",
        HighAnsi = "Arial",
        EastAsia = "Arial",
        ComplexScript = "B Mitra",
        Hint = FontTypeHintValues.ComplexScript
    },
    new FontSize { Val = "24" },
    new FontSizeComplexScript { Val = "24" },
    new Languages { Bidi = "fa-IR", Val = "en-US", EastAsia = "en-US" },
    new BiDi { Val = true }
);

var run = new Run();
run.AppendChild(rPr);
run.AppendChild(new Text(text));

par.AppendChild(pPr);
par.AppendChild(run);

// finally append to the body
body.Append(par);

Thanks in advance.

1
Hello. Are you sure it is possible to combine these two types of text in single line? I mean are you able to create such document using MsWord? If you will provide such document, 99% I will help you. Anyway my point is to use separate runs(with different text direction property) for each type of text.Shelest
Hi. Yes, the first image in Q is created in MS Word and it's correct. But, you are right. I did unzip the MSW created document and look in generated XML. It's absolutely using 2 separate Runs. But I'm curious if is there any way, to have them both in one Run. Thank you for the consulting. Cheers. +1amiry jd

1 Answers

2
votes

Well I finally figured-out the problem. The problem was RightToLeftText which I have added to Run. I replaced it with a BiDi and problem solved. Cheers.