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>
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'
<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