• 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

  • 相关阅读:
    Linux系统下的Swift与Ceph分布式存储解决方案
    洛谷 P1343 地震逃生(最大流dinic算法)
    Linux 系统调用IO口,利用光标偏移实现文件复制
    Kubernetes 审计(Auditing)
    刷题整理(持续更新~)
    常见python脚本集合
    基于多模态脑机接口的智能小车自动驾驶系统
    代理模式(CGLIB和JDK)
    Task04 吃瓜教程——第五章 神经网络
    19c pdb克隆单机到rac,日志出现:WARNING报警处理
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126359219