![](/img/trans.png)
[英]How to apply style to a paragraph while editing a word document file through c# code using OpenXML?
[英]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" };
但是在输出文件中看不到任何变化。
我找到了一些帖子,但是它们根本没有帮助我,例如:
如何解决这个问题?
提前致谢。
使用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.