0
votes

I created a Word document, and added some text in it.My problem is with Persian/Arabic text and English text. I can set the Complex and Latin font in Word like the picture below, but in OpenXML, when I set font for ASCII and Complex font, Word is not initializing it. My code in c#:

 public string CreateWordprocessingDocument()
    {
        string fileName = HttpContext.Current.Server.MapPath("~/TempFile/125.docx");
        string txt = @"  بسم الله الرحمن الرحیم
                this is test";
        using (WordprocessingDocument wordDocument =
            WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
            mainPart.Document = new Document();
            Body body = mainPart.Document.AppendChild(new Body());
            Paragraph para = body.AppendChild(new Paragraph());
            SetRightToLeft(para);
            Run run = new Run();
            RunProperties rPr = new RunProperties();
            RunFonts rFont = new RunFonts();
            rFont.Ascii = "Arial";
            rFont.ComplexScript = "B Nazanin";
            rFont.EastAsia = "B Nazanin";
            rPr.Append(rFont);
            run.AppendChild(rPr);
            run.AppendChild(new Text(txt));
            para.AppendChild(run);
        }
        return fileName;
    }
}

and the result in word xml like this:

 <w:document>
  <w:body>
   <w:p>
    <w:pPr>
     <w:bidi/>
    </w:pPr>
    <w:r>
     <w:rPr>
       <w:rFonts w:ascii="Arial" w:eastAsia="B Nazanin" w:cs="B Nazanin"/>
     </w:rPr>
    <w:t> بسم الله الرحمن الرحیم this is test</w:t>
   </w:r>
  </w:p>
 </w:body>
 </w:document>

word setting complex script and latin

The problem is when I open Word document all of the text is 'Arial'. I want font of 'this is test' to be 'Arial' and font of 'بسم الله الرحمن الرحیم' to be 'B Nazanin'

2
Hmm, that's odd, according to the spec that XML should work the way you have it. I suppose I would experiment by expanding the .docx, editing the XML directly, and repackaging into a docx to try things out. I might try putting the two into separate <w:t> elements. I would also make a Word document that looks the way I want and then inspect the XML. I expect Word puts them in separate runs.scanny

2 Answers

1
votes

You must split Persian text from Latin and then apply font properties for each one separately.

0
votes

Create two different runs with different font properties. One run with the Arial font and "this is text" Text and the other with B Nazanin font and the above specified text.