在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
在像 Microsoft Word 文件這樣的 GUI 應用程式中,文字對齊非常重要。 確實,外面有許多能對齊文本的函式庫。 仍然,這IronWordNuGet 套件作為一款強大工具脫穎而出,提供了一種簡單有效的方法來管理您的 C# 應用程式中的格式或對齊方式。 開發人員經常需要以不同方式對齊文字,無論是用於報告或是控制台應用程式,在這些地方都可以輕鬆使用 IronWord NuGet。 現在讓我們深入探討IronWord NuGet套件以及如何使用此套件來對齊文本或段落。
在 Microsoft Visual Studio 中創建一個新項目。
通過 NuGet 套件管理器安裝 IronWord。
生成具有文字對齊的Word文件。
IronWord 是來自 C# NuGet 庫Iron Software開發旨在促進以程式方式創建、操作和管理 Microsoft Word 文件。 它允許開發者自動化生成對齊文本的 Word 文件過程,使其更容易在應用程式中動態生成報告、發票、信件及其他類型的文件。
IronWord 支援多種對齊類型,包括左對齊、右對齊、置中對齊和對齊文本。 這種多樣性使開發人員能夠選擇最適合其特定需求的格式。
該套件簡化了文字在欄位對齊的過程,這對於生成報告或以結構化格式顯示數據特別有用。
IronWord 讓開發人員可以自訂文本欄位的寬度,確保對齊的文本能夠整齊地放置在指定的範圍內。
您可以輕鬆地在 Word 文件中插入、替換或刪除文字。
該庫支持各種格式選項,包括字體名稱樣式、字體大小、顏色和對齊方式。
IronWord 允許您在文件中插入和操作表格和圖片。
它與不同版本的Microsoft Word 無縫協作,確保兼容性和使用方便性。
函件和通知:為客戶或員工生成個性化的信函和通知。
IronWord 簡化了在 .NET 應用程式中處理 Word 文件的過程,使其成為開發人員想要自動化文件生成和管理任務時的寶貴工具。
在開始之前,請務必確認您已準備好以下項目:
現在,讓我們開始建立一個新的 Visual Studio 專案。
損壞的圖片 從Pixabay添加,從你的文件中選擇或拖放圖片到這裡。
在下方屏幕中選擇「主控台應用程式」範本。
提供專案名稱和位置。
選擇 .NET 版本(最好是支援的最新版本),然後點擊建立。
在 Visual Studio 中,從 NuGet 套件管理員安裝 IronWord NuGet 套件,如下所示。
或者,請使用以下指令透過 CLI 直接安裝它。
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
以下程式碼範例顯示如何生成帶有文字置中的 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
程式碼解釋
初始化新的 WordDocument 實例。
如有需要,可創建具有樣式的文本。
將文字添加到段落中。
將段落與IronWord.Models.Enums.TextAlignment.Center對齊。
將段落添加到 WordDocument 實例中,並將文件保存為 output.docx。
輸出
現在讓我們嘗試不同的選項。
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
程式碼解釋
初始化新的 WordDocument 實例。
如有需要,可創建具有樣式的文本。
將文字添加到段落中。 將段落與IronWord.Models.Enums.TextAlignment.Center對齊。
將另一段文字添加到段落中。 將段落對齊至 IronWord.Models.Enums.TextAlignment.Right。
將文字添加到段落中。 使用 IronWord.Models.Enums.TextAlignment.Left 對齊段落。
將文字添加到段落中。 將段落與 IronWord.Models.Enums.TextAlignment.Justified 對齊。
將段落添加到 WordDocument 實例中,並將文件保存為 output.docx。
輸出
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"
IronWord NuGet 套件簡化了在 .NET 應用程式中處理 Word 文件的過程。 其直觀的 API 允許開發人員輕鬆操控文本對齊和其他文檔元素。 無論您需要將文本對齊到左側、中央或右側,或進行對齊,IronWord 提供您所需的工具來創建專業且格式良好的文件。