在 Word 文档中,缩进是一种段落格式,用于调整段落正文和页边距之间的距离。它包括左缩进、右缩进、首行缩进和悬挂缩进。左缩进和右缩进可以应用于段落的所有行,而首行缩进只能应用于段落的第一行。至于悬挂缩进,它可以应用于除第一行之外的段落的每一行。本文介绍了如何使用Spire.Doc for .NET以编程方式在 Word 文档中设置段落缩进
Spire.Doc for.NET 最新下载
https://www.evget.com/product/3368/download
为 .NET 安装 Spire.Doc
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。
在 Word 中设置段落缩进
下表列出了一些用于在 Word 文档中设置不同段落缩进的核心类和属性。
| 姓名 | 描述 |
| 段落格式类 | 表示段落的格式。 |
| ParagraphFormat.LeftIndent 属性 | 返回或设置表示段落左缩进的值。 |
| ParagraphFormat.RightIndent 属性 | 返回或设置表示段落右缩进的值。 |
| ParagraphFormat.FirstLineIndent 属性 | 获取或设置第一行或悬挂缩进的值。正值代表首行缩进,负值代表悬挂缩进。 |
详细步骤如下:
【C#】
using Spire.Doc;
using Spire.Doc.Documents;
namespace WordIndent
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a sample Word document
doc.LoadFromFile("sample.docx");
//Get the first paragraph and set left indent
Paragraph para1 = doc.Sections[0].Paragraphs[0];
para1.Format.LeftIndent = 30;
//Get the second paragraph and set right indent
Paragraph para2 = doc.Sections[0].Paragraphs[1];
para2.Format.RightIndent = 30;
//Get the third paragraph and set first line indent
Paragraph para3 = doc.Sections[0].Paragraphs[2];
para3.Format.FirstLineIndent = 30;
//Get the fourth paragraph and set hanging indent
Paragraph para4 = doc.Sections[0].Paragraphs[3];
para4.Format.FirstLineIndent = -30;
//Save the document to file
doc.SaveToFile("Indent.docx", FileFormat.Docx2010);
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace WordIndent
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document instance
Dim doc As New Document()
'Load a sample Word document
doc.LoadFromFile("sample.docx")
'Get the first paragraph and set left indent
Dim para1 As Paragraph = doc.Sections(0).Paragraphs(0)
para1.Format.LeftIndent = 30
'Get the second paragraph and set right indent
Dim para2 As Paragraph = doc.Sections(0).Paragraphs(1)
para2.Format.RightIndent = 30
'Get the third paragraph and set first line indent
Dim para3 As Paragraph = doc.Sections(0).Paragraphs(2)
para3.Format.FirstLineIndent = 30
'Get the fourth paragraph and set hanging indent
Dim para4 As Paragraph = doc.Sections(0).Paragraphs(3)
para4.Format.FirstLineIndent = -30
'Save the document to file
doc.SaveToFile("Indent.docx", FileFormat.Docx2010)
End Sub
End Class
End Namespace
