為 Word 段落添加制表位
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.Doc for.NET 最新下載
技術交流Q群(767755948)
制表位是放置在標尺上的標記,用于定義文本或數字在一行上的對齊方式。要在 Microsoft Word 中為段落添加制表位,我們需要打開制表符對話框,然后設置制表位位置、對齊方式和前導符,如下所示。
本文詳細介紹了如何使用 Spire.Doc 以編程方式將制表位添加到 Word 文檔中的段落。
詳細步驟:
第 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 步:保存并關閉文檔對象。
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產品