如何在 C# 中使用模板生成 Word 文件
在GUI應用程式(如Microsoft Word文件)中顯示文字時,文字對齊非常重要。 的確,有很多程式庫可以對齊文字。 不過,IronWord NuGet套件作為一個強大的工具,簡化了這個過程,為您的C#應用程式提供管理格式或對齊的簡單且有效的方法。 開發人員經常需要用不同的方式對齊文字,無論是報告還是控制台應用程式,這些地方都可以方便地使用IronWord NuGet。 現在讓我們深入研究IronWord NuGet套件以及如何使用該套件對齊文字或段落。
如何使用IronWord NuGet在Microsoft Word文件中對齊文字
- 在Microsoft Visual Studio中建立一個新專案。
- 通過NuGet套件管理器安裝IronWord。
- 生成一個具有文字對齊的Word文件。
- 探索IronWord中的文字對齊屬性。
什麼是IronWord?
IronWord是由Iron Software開發的C# NuGet程式庫,用於以程式化方式建立、操作和管理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專案。
在下面的螢幕中選擇控制台應用程式模板。

提供專案名稱和位置。

選擇.NET版本,最好是帶有支持的最新版本,然後點擊建立。

步驟2:使用NuGet套件管理器安裝IronWord
按照下面的步驟從NuGet套件管理器安裝IronWord NuGet包。

或者,請直接使用以下指令在CLI中安裝它。
步驟3:生成具有文字對齊的Word文件
下面的程式碼範例顯示了如何生成具有居中對齊文字的Word文件。
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Set your license key for IronWord
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 to the paragraph
// Set text alignment to center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center;
// Add the first paragraph to the document
document.AddParagraph(paragraph);
// Save the document with the specified filename
document.SaveAs("output.docx");
}
}Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Set your license key for IronWord
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 to the paragraph
' Set text alignment to center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center
' Add the first paragraph to the document
document.AddParagraph(paragraph)
' Save the document with the specified filename
document.SaveAs("output.docx")
End Sub
End Class程式碼解釋
- 初始化一個新的
WordDocument實例。 - 建立一個帶有所需內容的文字物件。
- 將文字新增到段落中。
- 使用
IronWord.Models.Enums.TextAlignment.Center對齊段落。 - 將段落新增到
output.docx。
輸出

步驟4:探索IronWord中的文字對齊屬性
現在讓我們嘗試不同的選項。
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
class Program
{
static void Main()
{
// Set your license key for IronWord
License.LicenseKey = "your key";
// Create a new DOCX document
var doc = new WordDocument();
// Sample text for alignment
Text text = new Text() { Text = "IronWord, From IronSoftware" };
// Add paragraphs with different alignments
AddPara(doc, text, TextAlignment.Center);
AddPara(doc, text, TextAlignment.Left);
AddPara(doc, text, TextAlignment.Right);
AddPara(doc, text, TextAlignment.Justified);
// Save the document with all alignment options
doc.SaveAs("outputAllOptions.docx");
}
// Method to add a paragraph to the document with specified alignment
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()
' Set your license key for IronWord
License.LicenseKey = "your key"
' Create a new DOCX document
Dim doc = New WordDocument()
' Sample text for alignment
Dim text As New Text() With {.Text = "IronWord, From IronSoftware"}
' Add paragraphs with different alignments
AddPara(doc, text, TextAlignment.Center)
AddPara(doc, text, TextAlignment.Left)
AddPara(doc, text, TextAlignment.Right)
AddPara(doc, text, TextAlignment.Justified)
' Save the document with all alignment options
doc.SaveAs("outputAllOptions.docx")
End Sub
' Method to add a paragraph to the document with specified alignment
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實例。 - 建立一個帶有所需內容的文字物件。
- 為每個對齊選項(居中、左、右、兩端對齊)將文字新增到段落中並設置對齊。
- 將段落新增到
WordDocument實例中。 - 將包含所有對齊選項的文件保存到
outputAllOptions.docx。
輸出

IronWord 授權
IronWord程式庫是Iron Suite中Iron Software的產品之一。 它需要許可證才能運行。 使用者可以使用其電子郵件ID從試用許可證頁面下載試用許可證。 資料輸入後,許可證將被發送到使用者的電子郵件ID。 此許可證需要放置在使用者程式碼的開頭,然後才能使用IronWord程式庫,如下所示。
License.LicenseKey = "your Key Here";License.LicenseKey = "your Key Here"結論
IronWord NuGet套件簡化了在.NET應用程式中處理Word文件的過程。 其直觀的API允許開發人員輕鬆操作文字對齊和其他文件元素。 無論您需要將文字對齊到左、居中、右或兩端對齊,IronWord都提供了建立專業且高格式化文件所需的工具。

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章

