使用IRONWORD

如何使用 C# 在 Word 中對齊文本

發佈 2024年12月15日
分享:

在像 Microsoft Word 文件這樣的 GUI 應用程式中,文字對齊非常重要。 確實,外面有許多能對齊文本的函式庫。 仍然,這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 庫Iron Software開發旨在促進以程式方式創建、操作和管理 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。

第一步:在 Microsoft Visual Studio 中創建一個新項目

現在,讓我們開始建立一個新的 Visual Studio 專案。

損壞的圖片 從Pixabay添加,從你的文件中選擇或拖放圖片到這裡。

在下方屏幕中選擇「主控台應用程式」範本。

如何使用 C# 在 Word 中對齊文本:圖 2 - 選擇控制台應用程式

提供專案名稱和位置。

如何使用 C# 對齊 Word 中的文字:圖 3 - 配置

選擇 .NET 版本(最好是支援的最新版本),然後點擊建立。

如何在 Word 中使用 C# 對齊文本:圖 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 下載試用授權從試用許可證頁面. 一旦輸入數據,許可證將發送至用戶的電子郵件地址。 在使用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 文件