Defining paragraph with RTL direction in word file by OpenXML in C#

假如想象 提交于 2019-12-04 17:33:47

Use the BiDi class to set the text direction to RTL for a paragraph. The following code sample searches for the first paragraph in a word document and sets the text direction to RTL using the BiDi class:

using (WordprocessingDocument doc =
   WordprocessingDocument.Open(@"test.docx", true))
{
  Paragraph p = doc.MainDocumentPart.Document.Body.ChildElements.First<Paragraph>();

  if(p == null)
  {
    Console.Out.WriteLine("Paragraph not found.");
    return;
  }

  ParagraphProperties pp = p.ChildElements.First<ParagraphProperties>();

  if (pp == null)
  {
    pp = new ParagraphProperties();
    p.InsertBefore(pp, p.First());
  }

  BiDi bidi = new BiDi();
  pp.Append(bidi);

}

There are a few more aspects to bi-directional text in Microsoft Word. SanjayKumarM wrote an article about how right-to-left text content is handled in Microsoft Word. See this link for more information.

This code worked for me to set the direction Right to Left

var run = new Run(new Text("Some text"));
var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run);
paragraph.ParagraphProperties = new ParagraphProperties()
{
    BiDi = new BiDi(),
    TextDirection = new TextDirection()
    {
        Val = TextDirectionValues.TopToBottomRightToLeft
    }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!