簡體   English   中英

如何使用Novacode DocX在代碼中為表格中的文本設置垂直方向?

[英]How to set vertical orientation for text in table in code with Novacode DocX?

使用Novacode.DocX可以輕松地將表創建和插入到文檔中。

// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add a Table to this document.
Table t = document.AddTable(2, 3);
// Specify some properties for this Table.
t.Alignment = Alignment.center;
t.Design = TableDesign.MediumGrid1Accent2;
// Add content to this Table.
t.Rows[0].Cells[0].Paragraphs.First().Append("A");
t.Rows[0].Cells[1].Paragraphs.First().Append("B");
t.Rows[0].Cells[2].Paragraphs.First().Append("C");
t.Rows[1].Cells[0].Paragraphs.First().Append("D");
t.Rows[1].Cells[1].Paragraphs.First().Append("E");
t.Rows[1].Cells[2].Paragraphs.First().Append("F");
// Insert the Table into the document.
document.InsertTable(t);
document.Save();
}// Release this document from memory.

上面的代碼將創建一個如下圖所示的文檔

而且,如何使用DocX為表格中的文本設置垂直方向? 文本方向僅從右到左進行,反之亦然。

tablePlan.Rows[0].Cells[1].Paragraphs.First().Direction = Direction.LeftToRight;

以及如何從頭開始?

在此處輸入圖片說明

此解決方案是一種變通方法,並且僅在您已經事先知道Table要具有的列數的情況下才有效。 首先,您需要創建一個包含表格的文檔,該表格具有您要查找的屬性(文本方向)。 然后,您可以根據需要從其他模板文檔中獲取Tables

// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
    Table t;
    using (DocX document2 = DocX.Load(@"Test2.docx"))
    {
        t = document2.Tables[0];
    }        
    // Specify some properties for this Table.
    t.Alignment = Alignment.center;
    t.Design = TableDesign.MediumGrid1Accent2;
    // Add content to this Table.
    t.Rows[0].Cells[0].Paragraphs.First().Append("A");
    t.Rows[0].Cells[1].Paragraphs.First().Append("B");
    t.Rows[0].Cells[2].Paragraphs.First().Append("C");
    t.Rows[1].Cells[0].Paragraphs.First().Append("D");
    t.Rows[1].Cells[1].Paragraphs.First().Append("E");
    t.Rows[1].Cells[2].Paragraphs.First().Append("F");
    // Insert the Table into the document.
    document.InsertTable(t);
    document.Save();
}// Release this document from memory.

document2包含具有所需屬性的表。

您可以通過添加以下內容來實現:

t.Rows[0].Cells[0].TextDirection = TextDirection.btLr;
t.Rows[0].Cells[1].TextDirection = TextDirection.btLr;
t.Rows[0].Cells[2].TextDirection = TextDirection.btLr;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM