• Word控件Spire.Doc 【段落处理】教程(一):C#/VB.NET:在 Word 中对齐文本


    文本对齐是一个段落格式属性,它决定了整个段落中文本的外观。Microsoft 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安装。

    PM> Install-Package Spire.Doc

    在 Word 中对齐文本

    • 创建一个文档实例。
    • 使用Document.LoadFromFile()方法加载示例 Word 文档。
    • 使用Document.Sections[]属性获取指定部分。
    • 使用Section.Paragraphs[]属性获取指定段落。
    • 使用Paragraph.Format属性获取段落格式
    • 使用ParagraphFormat.HorizontalAlignment属性设置指定段落的文本对齐方式。
    • 使用Document.SaveToFile()方法将文档保存到另一个文件。

    [C#]

    using Spire.Doc;
    using Spire.Doc.Documents;
    
    namespace AlignText
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Create a Document instance
    Document doc = new Document();
    
    //Load a sample Word document
    doc.LoadFromFile(@"D:\Files\sample.docx");
    
    //Get the first section
    Section section = doc.Sections[0];
    
    //Get the first paragraph and make it center-aligned
    Paragraph p = section.Paragraphs[0];
    p.Format.HorizontalAlignment = HorizontalAlignment.Center;
    
    //Get the second paragraph and make it left-aligned
    Paragraph p1 = section.Paragraphs[1];
    p1.Format.HorizontalAlignment = HorizontalAlignment.Left;
    
    //Get the third paragraph and make it right-aligned
    Paragraph p2 = section.Paragraphs[2];
    p2.Format.HorizontalAlignment = HorizontalAlignment.Right;
    
    //Get the fourth paragraph and make it justified
    Paragraph p3 = section.Paragraphs[3];
    p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;
    
    //Save the document
    doc.SaveToFile("WordAlignment.docx", FileFormat.Docx);
    }
    }
    }
    

    [VB.NET]

    Imports Spire.Doc
    Imports Spire.Doc.Documents
    
    Namespace AlignText
    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("E:\Work\Documents\WordDocuments\Humor Them.docx")
    
    'Get the first section
    Dim section As Section = doc.Sections(0)
    
    'Get the first paragraph and make it center-aligned
    Dim p As Paragraph = section.Paragraphs(0)
    p.Format.HorizontalAlignment = HorizontalAlignment.Center
    
    'Get the second paragraph and make it left-aligned
    Dim p1 As Paragraph = section.Paragraphs(1)
    p1.Format.HorizontalAlignment = HorizontalAlignment.Left
    
    'Get the third paragraph and make it right-aligned
    Dim p2 As Paragraph = section.Paragraphs(2)
    p2.Format.HorizontalAlignment = HorizontalAlignment.Right
    
    'Get the fourth paragraph and make it justified
    Dim p3 As Paragraph = section.Paragraphs(3)
    p3.Format.HorizontalAlignment = HorizontalAlignment.Justify
    
    'Save the document
    doc.SaveToFile("WordAlignment.docx", FileFormat.Docx)
    End Sub
    End Class
    End Namespace

       欢迎下载|体验更多E-iceblue产品 技术交流Q群(767755948)   

  • 相关阅读:
    大数据专业毕业后职业前景如何?
    Android GridLayoutManager Glide批量加载Bitmap绘制Canvas画在RecyclerView,Kotlin(a)
    对象存储服务中对象业务的非标接口
    多线程&JUC
    01. 板载硬件资源和开发环境
    WM CJC8988多功能Codec芯片性能及应用介绍
    从零开始使用git
    目标检测YOLO实战应用案例100讲-基于YOLOv7的番茄采摘机械手场景感知及试验(中)
    kafka简述
    MATLAB常用命令大全,非常详细(持续更新中)
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126340734