一般情況下在Word中輸入的文字都是橫向的,今天給大家分享兩種方法來(lái)設(shè)置/更改一個(gè)section內(nèi)的所有文本的方向及部分文本的方向,有興趣的朋友可以試下。
首先,從https://visualstudiogallery.msdn.microsoft.com/d3a38f74-3490-42da-bdb0-37fa5acebc36下載免費(fèi)版.NET Word類庫(kù)并安裝,然后創(chuàng)建一個(gè)C# 控制臺(tái)應(yīng)用程序,添加引用及命名空間并參考以下步驟。
步驟1:創(chuàng)建一個(gè)新的Document對(duì)象并加載Word文檔。
Document document = new Document();document.LoadFromFile("示例.docx"); 步驟2:為一個(gè)section內(nèi)的所有文本設(shè)置文本方向。
//獲取第一個(gè)section并為其設(shè)置文本方向Section section = document.Sections[0];section.TextDirection = TextDirection.RightToLeftRotated;
如果要設(shè)置部分文本的文本方向,可以將該文本放在table中然后再設(shè)置文本方向,如以下步驟:
步驟3:添加一個(gè)新的section和一個(gè)table,獲取目標(biāo)單元格并設(shè)置文本方向,然后將文本添加到單元格。
//添加一個(gè)新的section到文檔Section sec = document.AddSection();//添加一個(gè)table到該sectionTable table = sec.AddTable();//添加一行和一列到tabletable.ResetCells(1, 1);//獲取單元格TableCell cell = table.Rows[0].Cells[0];table.Rows[0].Height = 50;table.Rows[0].Cells[0].Width = 5;//設(shè)置單元格的文本方向并添加文本到該單元格cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;cell.AddParagraph().AppendText("你好"); 添加一個(gè)新的段落來(lái)檢測(cè)以上方法是否會(huì)影響該section內(nèi)的其他文本的文本方向:
sec.AddParagraph().AppendText("新段落"); 步驟4:保存文檔。
document.SaveToFile("文本方向.docx", FileFormat.Docx);
運(yùn)行結(jié)果:
設(shè)置一個(gè)section內(nèi)的所有文本的文本方向:
設(shè)置部分文本的文本方向:
全部代碼:
using Spire.Doc;using Spire.Doc.Documents;namespace Set_text_direction_in_Word{class Program{static void Main(string[] args){Document document = new Document();document.LoadFromFile("示例.docx");//設(shè)置一個(gè)section內(nèi)的所有文本的文本方向Section section = document.Sections[0];section.TextDirection = TextDirection.RightToLeftRotated; //設(shè)置部分文本的文本方向Section sec = document.AddSection();Table table = sec.AddTable();table.ResetCells(1, 1);TableCell cell = table.Rows[0].Cells[0];table.Rows[0].Height = 50;table.Rows[0].Cells[0].Width = 5;cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;cell.AddParagraph().AppendText("你好");sec.AddParagraph().AppendText("新段落");//保存文檔document.SaveToFile("文本方向.docx", FileFormat.Docx);}}}以上所述是小編給大家介紹的C# 向Word中設(shè)置/更改文本方向的方法(兩種),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注