Rez*_*M.A 4 c# ms-word openxml openxml-sdk
如何在 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);
Run Code Online (Sandbox Code Playgroud)
最后,我将为我的段落设置这种样式:
...
heading_pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "my_style" };
Run Code Online (Sandbox Code Playgroud)
但是在输出文件中看不到任何变化。
我找到了一些帖子,但他们根本没有帮助我,例如:
如何解决这个问题?
提前致谢。
使用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);
}
Run Code Online (Sandbox Code Playgroud)
Microsoft Word 中的双向文本还有一些其他方面。 SanjayKumarM写了一篇关于如何在 Microsoft Word 中处理从右到左的文本内容的文章。有关更多信息,请参阅此链接。