在 Word 中設(shè)置段落縮進
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.Doc for.NET 最新下載
在 Word 文檔中,縮進是一種段落格式,用于調(diào)整段落正文和頁邊距之間的距離。它包括左縮進、右縮進、首行縮進和懸掛縮進。左縮進和右縮進可以應(yīng)用于段落的所有行,而首行縮進只能應(yīng)用于段落的第一行。至于懸掛縮進,它可以應(yīng)用于除第一行之外的段落的每一行。本文介紹了如何使用 Spire.Doc for .NET以編程方式在 Word 文檔中設(shè)置段落縮進
為 .NET 安裝 Spire.Doc
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
在 Word 中設(shè)置段落縮進
下表列出了一些用于在 Word 文檔中設(shè)置不同段落縮進的核心類和屬性。
| 姓名 | 描述 | 
| 段落格式類 | 表示段落的格式。 | 
| ParagraphFormat.LeftIndent 屬性 | 返回或設(shè)置表示段落左縮進的值。 | 
| ParagraphFormat.RightIndent 屬性 | 返回或設(shè)置表示段落右縮進的值。 | 
| ParagraphFormat.FirstLineIndent 屬性 | 獲取或設(shè)置第一行或懸掛縮進的值。正值代表首行縮進,負(fù)值代表懸掛縮進。 | 
詳細步驟如下:
- 創(chuàng)建一個文檔實例。
- 使用Document.LoadFromFile()方法加載示例 Word 文檔。
- 使用Document.Section[]屬性獲取指定部分。
- 使用Section.Paragraphs[]屬性獲取指定段落。
- 使用Paragraph.Format屬性獲取段落格式,然后使用ParagraphFormat類的上面列出的屬性設(shè)置段落縮進。
- 使用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
	 
 
歡迎下載|體驗更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)


 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線