【问题标题】:Defining paragraph with RTL direction in word file by OpenXML in C#在 C# 中通过 OpenXML 在 Word 文件中定义具有 RTL 方向的段落
【发布时间】:2013-03-17 13:04:14
【问题描述】:

如何在 C# 中使用 OpenXML 为 word 中的段落设置从右到左的方向?我使用下面的代码来定义它,但它们不会做任何改变:

 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);

最后我会为我的段落设置这个样式:

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

但是在输出文件中看不到任何变化。

我找到了一些帖子,但它们根本没有帮助我,比如:

Changing text direction in word file

如何解决这个问题?

提前致谢。

【问题讨论】:

    标签: c# ms-word openxml openxml-sdk


    【解决方案1】:

    使用BiDi 类将段落的文本方向设置为 RTL。 以下代码示例在 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);
    
    }
    

    Microsoft Word 中的双向文本还有更多方面。 SanjayKumarM 写了一篇关于如何从右到左的文本内容 在 Microsoft Word 中处理。有关详细信息,请参阅this 链接。

    【讨论】:

      【解决方案2】:

      这段代码对我有用,可以设置从右到左的方向

      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
          }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        • 1970-01-01
        相关资源
        最近更新 更多