簡體   English   中英

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

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

如何在C#中使用OpenXML在單詞中設置從右到左的方向? 我使用以下代碼對其進行定義,但它們不會做任何更改:

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

但是在輸出文件中看不到任何變化。

我找到了一些帖子,但是它們根本沒有幫助我,例如:

在Word文件中更改文本方向

如何解決這個問題?

提前致謝。

使用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中處理從右到左文本內容的文章。 有關更多信息,請參見此鏈接。

此代碼對我有用,將方向設置為從右到左

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