Word控件Spire.Doc 【段落处理】教程(二):C#/VB.NET:在 Word 中设置段落缩进

在 Word 文档中,缩进是一种段落格式,用于调整段落正文和页边距之间的距离。它包括左缩进、右缩进、首行缩进和悬挂缩进。左缩进和右缩进可以应用于段落的所有行,而首行缩进只能应用于段落的第一行。至于悬挂缩进,它可以应用于除第一行之外的段落的每一行。本文介绍了如何使用Spire.Doc for .NET以编程方式在 Word 文档中设置段落缩进

Spire.Doc for.NET 最新下载icon-default.png?t=M666https://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 属性获取或设置第一行或悬挂缩进的值。正值代表首行缩进,负值代表悬挂缩进。

详细步骤如下:

  • 创建一个文档实例。
  • 使用Document.LoadFromFile()方法加载示例 Word 文档。
  • 使用Document.Sections[]属性获取指定部分。
  • 使用Section.Paragraphs[]属性获取指定段落。
  • 使用Paragraph.Format属性获取段落格式,然后使用ParagraphFormat类的上面列出的属性设置段落缩进。
  • 使用Document.SaveToFile()方法将文档保存到另一个文件。

【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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值