Word控件Spire.Doc 【段落处理】教程(十五):在 C# 中为 Word 段落添加制表位

制表位是放置在标尺上的标记,用于定义文本或数字在一行上的对齐方式。要在 Microsoft Word 中为段落添加制表位,我们需要打开制表符对话框,然后设置制表位位置、对齐方式和前导符,如下所示。

Spire.Doc for.NET 最新下载icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

Word控件Spire.Doc 【段落处理】教程(十五):在 C# 中为 Word 段落添加制表位_第1张图片

本文详细介绍了如何使用 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();

截图

Word控件Spire.Doc 【段落处理】教程(十五):在 C# 中为 Word 段落添加制表位_第2张图片

完整代码

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();

}

}
}

你可能感兴趣的:(Spire.Doc,入门教程,c#,javascript,开发语言,spire.doc,制表位)