简体   繁体   English

在C#中通过OpenXML在Word文件中使用RTL方向定义段落

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

How to set the right-to-left direction for a paragraph in word with OpenXML in C#? 如何在C#中使用OpenXML在单词中设置从右到左的方向? I use codes below to define it but they won't make any change: 我使用以下代码对其进行定义,但它们不会做任何更改:

 RunProperties rPr = new RunProperties();

 Style style = new Style();
 style.StyleId = "my_style";
 style.Append(new Justification() { Val = JustificationValues.Right });
 style.Append(new TextDirection() { Val = TextDirectionValues.TopToBottomRightToLeft });
 style.Append(rPr);

and at the end I will set this style for my paragraph: 最后,我将为我的段落设置以下样式:

...
heading_pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "my_style" };

But is see no changes in the output file. 但是在输出文件中看不到任何变化。

I have found some post but they didn't help me at all,like: 我找到了一些帖子,但是它们根本没有帮助我,例如:

Changing text direction in word file 在Word文件中更改文本方向

How can solve this problem? 如何解决这个问题?

Thanks in advance. 提前致谢。

Use the BiDi class to set the text direction to RTL for a paragraph. 使用BiDi类将段落的文本方向设置为RTL。 The following code sample searches for the first paragraph in a word document and sets the text direction to RTL using the BiDi class: 下面的代码示例在Word文档中搜索第一段,并使用BiDi类将文本方向设置为RTL:

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. Microsoft Word中的双向文本还有其他几个方面。 SanjayKumarM wrote an article about how right-to-left text content is handled in Microsoft Word. SanjayKumarM撰写了一篇有关如何在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
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM