如何使用 C# 對齊 Word 中的文字
在數位時代,文件越來越多地成為數位化形式。 在商業環境中,文件通常與Microsoft Word及其基於文件的Word文件相關聯。編寫和編輯Microsoft Word文件已成為大型組織中傳遞資訊的基礎。 然而,手動建立和編輯文件可以是一個痛苦的過程; 要以程式化方式自動生成Word文件也並非易事,因為許多開源庫依賴於Microsoft Office Word作為依賴項。
然而,管理和建立Word文件並不需要這麼困難。 IronWord是一個不依賴於Microsoft Office Word的C# Word程式庫,允許使用者自訂整個文件,同時也可程式化生成文件和建立文件模板。
在今天的教程中,我將簡要說明如何使用IronWord程式化建立Microsoft Word文件,並提供簡單範例。
IronWord:一個C# Word程式庫
IronWord是一個非常可靠且易於使用的C# Docx庫,使開發人員可以使用C#構建和修改Word文件,而不需要傳統的依賴項如Microsoft Office或Word Interop。此外,它擁有廣泛的文件並提供對.NET 8, 7, 6, Framework, Core和Azure的全面支持,使其與大多數應用程式相容。 這種靈活性使其成為您可能處理的任何應用程式的絕佳選擇。
設置環境
在此例中,我們將使用Visual Studio建立一個控制台應用程式,並展示如何建立空白的Word文件並使用IronWord程式庫操作它們。 在進行下一步之前,請確保您已安裝Visual Studio。
首先,我們建立一個新的控制台應用程式。
然後,如下所示提供專案名稱和保存位置。
最後,選擇所需的框架。
安裝C# Word程式庫
建立空白控制台應用程式後,我們將通過NuGet包管理器下載IronWord。
點擊"管理NuGet包",然後在"Browse"選項卡中搜尋IronWord,如下所示。
之後,將它安裝到新建立的專案中。
或者,您也可以在命令行中鍵入以下命令以安裝IronWord。
Install-Package IronWord
現在一切都已設置並準備就緒,我們來看看建立Word文件的範例。
許可金鑰
請記住,IronWord需要一個授權金鑰才能運行。 您可以存取此連結獲取免費試用的金鑰。
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
' Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"
收到試用金鑰後,在專案中設置此變數。
建立新Word文件
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Create a textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Create a textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Create a textrun
Dim textRun As New Text("Sample text")
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
' Create a new Word document with the paragraph
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("document.docx")
End Sub
End Class
Programmatically Create New Word Documents的VS 2022教程:圖6 - 上述範例的輸出
說明
- 首先,我們在範例程式碼中導入IronWord程式庫及其模型。
- 我們建立了一個新的
Text變數。 此變數允許我們向Word文件中新增文字。 - 我們然後將
Paragraph。 這使我們可以通過區分兩者來輕鬆操作文字。 - 我們將
WordDocument類來建立一個新的文件物件。 - 最後,我們將文件儲存為"document.docx"為Word文件。
在前面的範例中,我們建立了一個包含基本文字的Word文件。 讓我們做一個例子,使用更高級的功能,比如自定義文字並新增文字效果。
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY";
// Load an existing docx
WordDocument doc = new WordDocument("document.docx");
// Create sample texts with styles
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
// Create a paragraph and add texts
Paragraph paragraph = new Paragraph();
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph to the document
doc.AddParagraph(paragraph);
// Export the document
doc.SaveAs("save_document.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY";
// Load an existing docx
WordDocument doc = new WordDocument("document.docx");
// Create sample texts with styles
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
// Create a paragraph and add texts
Paragraph paragraph = new Paragraph();
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph to the document
doc.AddParagraph(paragraph);
// Export the document
doc.SaveAs("save_document.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY"
' Load an existing docx
Dim doc As New WordDocument("document.docx")
' Create sample texts with styles
Dim introText As New Text("This is an example paragraph with italic and bold styling.")
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New Text("Italic example sentence.")
italicText.Style = italicStyle
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New Text("Bold example sentence.")
boldText.Style = boldStyle
' Create a paragraph and add texts
Dim paragraph As New Paragraph()
paragraph.AddText(introText)
paragraph.AddText(italicText)
paragraph.AddText(boldText)
' Add paragraph to the document
doc.AddParagraph(paragraph)
' Export the document
doc.SaveAs("save_document.docx")
End Sub
End Class
Programmatically Create New Word Documents的VS 2022教程:圖7 - 上述同樣的範例程式碼輸出
程式碼解釋
- 像上面的範例一樣,我們建立了
Text物件並為文件新增了範例文字。 - 我們然後建立一個新的
true,表示文字應為斜體。 - 我們對
IsBold。 - 我們然後將
Text變數。 - 我們建立一個
AddText方法以新增斜體和粗體文字。 - 我們使用
AddParagraph方法將段落新增到文件。 - 最後,我們將文件儲存為"save_document.docx"。
上述範例展示了開發人員可以使用IronWord使用的字體和樣式效果。 除了斜體和粗體文字效果之外,IronWord還提供其他功能以確保開發人員在各種情景中能夠建立獨特的Word文件。
結論
Programmatically Create New Word Documents的VS 2022教程:圖8 - IronWord授權資訊
我們的演示展示了如何輕鬆使用IronWord程式庫以C#程式化建立Word文件。 程式庫的靈活性和可擴展性使其成為開發人員在現實生活中進行文字、自訂字體和樣式的寶貴工具。 了解Word如何與其他應用程式整合為開發人員提供了解決他們挑戰的額外解決方案。
IronWord提供了一個免費試用授權。
常見問題
我如何在C#中程式化建立Microsoft Word文件?
您可以使用IronWord程式庫在C#中程式化建立Microsoft Word文件。初始化一個WordDocument物件,新增必要的內容,並使用SaveAs方法將其匯出為.docx文件。
使用IronWord相較於使用Microsoft Office Interop來建立Word文件的好處是什麼?
IronWord提供了超過Microsoft Office Interop的優勢,因為它不需要Microsoft Office安裝,提供了更大的靈活性,並易於跨不同平台整合,而不依賴Office。
我如何在Visual Studio中安裝IronWord?
要在Visual Studio中安裝IronWord,請打開NuGet套件管理器,搜索IronWord,並將其安裝到您的專案中。或者,使用命令行Install-Package IronWord。
我可以使用IronWord在Word文件中格式化文字嗎?
是的,您可以使用IronWord在Word文件中格式化文字,通過使用TextStyle物件來應用各種樣式,如斜體和粗體到文字元素。
IronWord與.NET和Azure相容嗎?
是的,IronWord與.NET 8、7、6、Framework、Core和Azure完全相容,使其適用於各種應用。
我如何解決IronWord的安裝問題?
如果您遇到IronWord的安裝問題,請確保您使用的是相容的Visual Studio和.NET版本,並且您的NuGet套件管理器已更新。檢查您的網路連接並重試安裝。
開始使用IronWord建立Word文件需要什麼?
要開始使用IronWord建立Word文件,請將IronWord程式庫及其模型匯入到您的C#專案中。然後初始化一個WordDocument物件以開始構建您的文件。
我如何使用IronWord程式化地向Word文件中新增段落?
您可以使用IronWord程式化地向Word文件中新增段落,通過建立Paragraph物件,新增文字,並將其包含在WordDocument中。



