使用IRONWORD

如何使用 C# 在 Word 中对齐文本

发布 2024年十二月15日
分享:

在 Microsoft Word 文档等图形用户界面应用程序中显示文本时,文本对齐至关重要。 事实上,有很多库可以对齐文本。 不过IronWordNuGet 包作为简化这一过程的强大工具出现,为您的 C# 应用程序提供了一种简单有效的格式或对齐管理方式。 开发人员经常会遇到需要以各种方式对齐文本的情况,无论是用于报告还是控制台应用程序,在所有这些地方都可以随时使用 IronWord NuGet。 现在,让我们深入了解 IronWord NuGet 软件包以及如何使用此软件包对齐文本或段落。

如何使用 IronWord NuGet 对齐 Microsoft Word 文档中的文本

  1. 在 Microsoft Visual Studio 中创建一个新项目。

  2. 通过 NuGet 软件包管理器安装 IronWord。

  3. 生成文本对齐的 Word 文档。

  4. 探索 IronWord 中的文本对齐属性。

什么是IronWord?

IronWord 是一个 C# NuGet 库,来自于铁软件Microsoft Word 是为方便以编程方式创建、操作和管理 Microsoft Word 文件而开发的。 它允许开发人员自动生成带有对齐文本的 Word 文档,从而更轻松地在应用程序中动态生成报告、发票、信函和其他类型的文档。

IronWord 的主要功能

1.C#在 Word 中对齐文本

IronWord 支持多种对齐类型,包括左对齐、右对齐、居中对齐和文本对齐。 这种多功能性允许开发人员根据自己的具体要求选择最合适的格式。

2.列对齐

该软件包简化了文本在列中对齐的过程,特别适用于生成报告或以结构化格式显示数据。

3.可定制格式

IronWord 允许开发人员自定义文本字段的宽度,确保对齐的文本在指定的边界内整齐排列。

4.文本处理

您可以在 Word 文档中轻松插入、替换或删除文本。

5.格式

该库支持各种格式选项,包括字体名称样式、字体大小、颜色和对齐方式。

6.表格和图片

IronWord 允许您在文档中插入和处理表格和图片。

7.兼容性

它可以与不同版本的 Microsoft Word 无缝协作,确保兼容性和易用性。

使用案例

  • 报告生成:自动生成带有动态数据和文本对齐方式的详细报告。
  • 创建发票:通过填写客户和交易详情创建专业发票。
  • 合同管理:利用个性化信息自动创建合同。
  • 信件和通知:为客户或员工生成个性化信件和通知。

    IronWord 简化了在 .NET 应用程序中处理 Word 文档的工作,使其成为希望自动完成文档生成和管理任务的开发人员的重要工具。

先决条件

在我们开始之前,请确保您具备以下条件:

  • 您的计算机上安装了 Visual Studio。
  • 已安装最新的 .NET Framework。

第 1 步:在 Microsoft Visual Studio 中创建一个新项目

现在,让我们从创建一个新的 Visual Studio 项目开始。

破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。

在下面的屏幕上选择控制台应用程序模板。

如何使用 C# 在 Word 中对齐文本:图 2 - 选择控制台应用程序

提供项目名称和地点。

如何使用 C# 在 Word 中对齐文本:图 3 - 配置

选择 .NET 版本,最好是支持的最新版本,然后单击 "创建"。

如何使用 C# 在 Word 中对齐文本:图 4 - 目标框架

第 2 步:使用 NuGet 软件包管理器安装 IronWord

在 Visual Studio 中从 NuGet 软件包管理器安装 IronWord NuGet 软件包,如下所示。

如何使用 C# 在 Word 中对齐文本:图 5 - 从 NuGet 软件包管理器中搜索 IronWord

或者,请使用下面的命令直接使用 CLI 安装。

如何使用 C# 在 Word 中对齐文本:图 6 - 复制命令并将其粘贴到 NuGet 控制台应用程序中

dotnet add package IronWord --version 2024.9.1
dotnet add package IronWord --version 2024.9.1
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'dotnet add package IronWord --version 2024.9.1
VB   C#

步骤 3:生成文本对齐的 Word 文档

下面的代码示例展示了如何生成居中对齐文本的 Word 文档。

using IronWord;
using IronWord.Models;
class Program
{
    static void Main()
    {
        License.LicenseKey = "your key";
        // Create a new document
        var document = new WordDocument();
        // Create a paragraph and add text
        Paragraph paragraph = new Paragraph();
        Text text = new Text() { Text = "IronWord, From IronSoftware" };
        paragraph.AddText(text); // add text property
        // Set text alignment to left aligned or center aligned
        paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center;
        // Add the first paragraph to the document
        document.AddParagraph(paragraph);
        // Save the document
        document.SaveAs("output.docx");
    }
}
using IronWord;
using IronWord.Models;
class Program
{
    static void Main()
    {
        License.LicenseKey = "your key";
        // Create a new document
        var document = new WordDocument();
        // Create a paragraph and add text
        Paragraph paragraph = new Paragraph();
        Text text = new Text() { Text = "IronWord, From IronSoftware" };
        paragraph.AddText(text); // add text property
        // Set text alignment to left aligned or center aligned
        paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center;
        // Add the first paragraph to the document
        document.AddParagraph(paragraph);
        // Save the document
        document.SaveAs("output.docx");
    }
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
	Shared Sub Main()
		License.LicenseKey = "your key"
		' Create a new document
		Dim document = New WordDocument()
		' Create a paragraph and add text
		Dim paragraph As New Paragraph()
		Dim text As New Text() With {.Text = "IronWord, From IronSoftware"}
		paragraph.AddText(text) ' add text property
		' Set text alignment to left aligned or center aligned
		paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center
		' Add the first paragraph to the document
		document.AddParagraph(paragraph)
		' Save the document
		document.SaveAs("output.docx")
	End Sub
End Class
VB   C#

代码解释

  1. 初始化新的 WordDocument 实例。

  2. 如有需要,请创建具有风格的文本。

  3. 将文本添加到段落中。

  4. 使用 IronWord.Models.Enums.TextAlignment.Center 对齐段落。

  5. 将段落添加到 WordDocument 实例中,并将文档保存为 output.docx。

    输出

    如何使用 C# 在 Word 中对齐文本:图 7 - 输出

第 4 步:探索 IronWord 中的文本对齐属性

现在,让我们尝试不同的方案。

using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
class Program
{
    static void Main()
    {
        License.LicenseKey = "your key";
        // Create a new DOCX document
        var doc = new WordDocument();
        Text text = new Text() { Text = "IronWord, From IronSoftware" };
        // Create a paragraph and add text left aligned
        AddPara(doc, text, TextAlignment.Center);
        // Create a paragraph and add text left aligned
        AddPara(doc, text, TextAlignment.Left);
        // Create a paragraph and add text right aligned
        AddPara(doc, text, TextAlignment.Right);
        // Create a paragraph and add text justified aligned
        AddPara(doc, text, TextAlignment.Justified);
        // Save the document
        doc.SaveAs("outputAllOptions.docx");
    }
    private static void AddPara(WordDocument doc, Text text, TextAlignment alignment)
    {
        Paragraph paragraph = new Paragraph();
        paragraph.AddText(text);
        paragraph.Alignment = alignment;
        doc.AddParagraph(paragraph);
    }
}
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
class Program
{
    static void Main()
    {
        License.LicenseKey = "your key";
        // Create a new DOCX document
        var doc = new WordDocument();
        Text text = new Text() { Text = "IronWord, From IronSoftware" };
        // Create a paragraph and add text left aligned
        AddPara(doc, text, TextAlignment.Center);
        // Create a paragraph and add text left aligned
        AddPara(doc, text, TextAlignment.Left);
        // Create a paragraph and add text right aligned
        AddPara(doc, text, TextAlignment.Right);
        // Create a paragraph and add text justified aligned
        AddPara(doc, text, TextAlignment.Justified);
        // Save the document
        doc.SaveAs("outputAllOptions.docx");
    }
    private static void AddPara(WordDocument doc, Text text, TextAlignment alignment)
    {
        Paragraph paragraph = new Paragraph();
        paragraph.AddText(text);
        paragraph.Alignment = alignment;
        doc.AddParagraph(paragraph);
    }
}
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Friend Class Program
	Shared Sub Main()
		License.LicenseKey = "your key"
		' Create a new DOCX document
		Dim doc = New WordDocument()
		Dim text As New Text() With {.Text = "IronWord, From IronSoftware"}
		' Create a paragraph and add text left aligned
		AddPara(doc, text, TextAlignment.Center)
		' Create a paragraph and add text left aligned
		AddPara(doc, text, TextAlignment.Left)
		' Create a paragraph and add text right aligned
		AddPara(doc, text, TextAlignment.Right)
		' Create a paragraph and add text justified aligned
		AddPara(doc, text, TextAlignment.Justified)
		' Save the document
		doc.SaveAs("outputAllOptions.docx")
	End Sub
	Private Shared Sub AddPara(ByVal doc As WordDocument, ByVal text As Text, ByVal alignment As TextAlignment)
		Dim paragraph As New Paragraph()
		paragraph.AddText(text)
		paragraph.Alignment = alignment
		doc.AddParagraph(paragraph)
	End Sub
End Class
VB   C#

代码解释

  1. 初始化新的 WordDocument 实例。

  2. 如有需要,请创建具有风格的文本。

  3. 将文本添加到段落中。 使用 IronWord.Models.Enums.TextAlignment.Center 对齐段落。

  4. 在段落中添加另一段文字。 使用 IronWord.Models.Enums.TextAlignment.Right 对齐段落。

  5. 将文本添加到段落中。 使用 IronWord.Models.Enums.TextAlignment.Left 对齐段落。

  6. 将文本添加到段落中。 使用 IronWord.Models.Enums.TextAlignment.Justified 对齐段落。

  7. 将段落添加到 WordDocument 实例中,并将文档保存为 output.docx。

    输出

    如何使用 C# 在 Word 中对齐文本:图 8 - 对齐输出

IronWord 许可

IronWord 库是 Iron Software 的 Iron Suite 产品之一。 它需要许可证才能运行。 用户可以使用自己的电子邮件 ID 从以下地址下载试用许可证试用许可证页面. 输入数据后,许可证将发送到用户的电子邮件 ID。 该许可证需要放在用户代码的开头,在使用 IronWord 库之前,如下所示。

License.LicenseKey = "your Key Here"
License.LicenseKey = "your Key Here"
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'License.LicenseKey = "your Key Here"
VB   C#

结论

IronWord NuGet 软件包简化了在 .NET 应用程序中处理 Word 文档的过程。 其直观的 API 允许开发人员轻松操作文本对齐方式和其他文档元素。 无论您需要将文本向左对齐、居中对齐、向右对齐还是对齐,IronWord 都能为您提供所需的工具,帮助您创建专业且格式规范的文档。

< 前一页
VS 2022 以编程方式创建新 Word 文档(教程)
下一步 >
如何使用 C# 中的 Word 模板生成 Word 文档

准备开始了吗? 版本: 2024.12 刚刚发布

免费NuGet下载 总下载量: 9,027 查看许可证 >