在c#中使用OpenXML在word文件中定义具有RTL方向的段落
本文关键字:RTL 段落 方向 文件 OpenXML word 定义 | 更新日期: 2023-09-27 18:04:25
如何在c#中设置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" };
但是在输出文件中没有看到任何变化。
我找到了一些帖子,但是他们对我一点帮助也没有,比如:
更改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写了一篇关于如何从右向左的文本内容的文章是在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
}
};