为 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产品