為 Word 段落添加制表位
Spire.Doc for .NET是一款專門對 Word 文檔進(jìn)行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團(tuán)隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.Doc for.NET 最新下載
技術(shù)交流Q群(767755948)
制表位是放置在標(biāo)尺上的標(biāo)記,用于定義文本或數(shù)字在一行上的對齊方式。要在 Microsoft Word 中為段落添加制表位,我們需要打開制表符對話框,然后設(shè)置制表位位置、對齊方式和前導(dǎo)符,如下所示。
本文詳細(xì)介紹了如何使用 Spire.Doc 以編程方式將制表位添加到 Word 文檔中的段落。
詳細(xì)步驟:
第 1 步:實例化一個 Document 對象并向其添加一個部分。
Document document = new Document(); Section section = document.AddSection();
第 2 步:將第 1 段添加到該部分。
Paragraph paragraph1 = section.AddParagraph();
第 3 步:將制表位添加到第 1 段。
//Add tab and set its position (in points) Tab tab = paragraph1.Format.Tabs.AddTab(28); //Set tab alignment tab.Justification = TabJustification.Left; //move to next tab and append text paragraph1.AppendText("\tWashing Machine"); //Add another tab and set its position (in points) tab = paragraph1.Format.Tabs.AddTab(280); //Set tab alignment tab.Justification = TabJustification.Left; //Specify tab leader type tab.TabLeader = TabLeader.Dotted; //move to next tab and append text paragraph1.AppendText("\t$650");
第 4 步:將第 2 段添加到該部分。
Paragraph paragraph2 = section.AddParagraph();
第 5 步:將制表位添加到第 2 段。
//Add tab and set its position (in points) tab = paragraph2.Format.Tabs.AddTab(28); //Set tab alignment tab.Justification = TabJustification.Left; //move to next tab and append text paragraph2.AppendText("\tRefrigerator"); //Add another tab and set its position (in points) tab = paragraph2.Format.Tabs.AddTab(280); //Set tab alignment tab.Justification = TabJustification.Left; //Specify tab leader type tab.TabLeader = TabLeader.NoLeader; //move to next tab and append text paragraph2.AppendText("\t$800");
第 6 步:保存并關(guān)閉文檔對象。
document.SaveToFile("Tab.docx", FileFormat.Docx2013); document.Close();
截圖:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; namespace AddTapStops { class Program { static void Main(string[] args) { //Instantiate a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Add paragraph 1 Paragraph paragraph1 = section.AddParagraph(); //Add tab and set its position (in points) Tab tab = paragraph1.Format.Tabs.AddTab(28); //Set tab alignment tab.Justification = TabJustification.Left; //move to next tab and append text paragraph1.AppendText("\tWashing Machine"); //Add another tab and set its position (in points) tab = paragraph1.Format.Tabs.AddTab(280); //Set tab alignment tab.Justification = TabJustification.Left; //Specify tab leader type tab.TabLeader = TabLeader.Dotted; //move to next tab and append text paragraph1.AppendText("\t$650"); //Add paragraph 2 Paragraph paragraph2 = section.AddParagraph(); //Add tab and set its position (in points) tab = paragraph2.Format.Tabs.AddTab(28); //Set tab alignment tab.Justification = TabJustification.Left; //move to next tab and append text paragraph2.AppendText("\tRefrigerator"); //move to next tab and append text //Add another tab and set its position (in points) tab = paragraph2.Format.Tabs.AddTab(280); //Set tab alignment tab.Justification = TabJustification.Left; //Specify tab leader type tab.TabLeader = TabLeader.NoLeader; //move to next tab and append text paragraph2.AppendText("\t$800"); //Save and close the document object document.SaveToFile("Tab.docx", FileFormat.Docx2013); document.Close(); } } }
歡迎下載|體驗更多E-iceblue產(chǎn)品
如需獲取更多產(chǎn)品相關(guān)信息請咨詢慧都在線客服