![](/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.