使用 IRONWORD .NET Word API(對開發者而言如何運作) Jordi Bardia 更新:6月 22, 2025 下載 IronWord NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 .NET Word API為開發人員提供了強大的工具,用於轉換 Word 文檔,以及在應用程式中與 MS Word 文件進行互動和操作。 此 API 旨在簡化處理 Microsoft Word 文件的流程,使以程式設計方式建立、編輯、轉換和管理文件變得更加容易。 在本文中,我們將探索IronWord ,以了解其在處理 Word 文件方面的功能。 IronWord簡介 IronWord是 .NET Word API 生態系統中的 .NET Word 程式庫,專為在其 .NET 應用程式中處理 Microsoft Word 文件的開發人員而設計。 使用 IronWord,開發人員可以輕鬆讀取、編寫和修改 Word 文檔,而無需在伺服器或客戶端電腦上安裝 Microsoft Word。 此功能對於需要自動執行文件處理任務的應用程式尤其有利,例如透過郵件合併功能產生報告、發票或個人化信函。 IronWord 的特點 IronWord 提供了一系列全面的功能,可滿足 Word 文件操作的各個方面。 讓我們來探索每一組功能,重點關注它們如何實現對多個文檔的操作和合併,這些文檔按"文檔結構"和"文檔元素"進行分類。 文檔結構 讀取和編輯 Word :使用 IronWord,您可以從 Word 文件中提取特定信息,例如提取文字進行編輯或重新利用,以及檢索可能需要在其他地方使用的圖像。 對於旨在合併 Word 文件和處理現有 DOCX 文件中包含的資訊的應用程式而言,此功能至關重要。 多種格式:IronWord 支援多種文件格式,增強了其在 .NET 應用程式中轉換 Word 文件的實用性。 編輯頁面設定:使用 IronWord 可以輕鬆調整 Word 文件的實體版面。 您可以調整各種 MS Word 文件的紙張尺寸為標準尺寸或自訂尺寸,更改文件不同部分的方向,設定頁邊距以確保正確對齊,甚至可以出於美觀目的或突出顯示部分內容而修改背景顏色。 新增段落:IronWord 允許在段落中新增和刪除文字行,這對於編輯和格式化大段文字至關重要。 此外,您還可以透過在文字中直接插入圖像和形狀來增強段落,調整樣式以匹配您的設計規範,並設定對齊方式以獲得更精緻的外觀。 新增項目符號和編號清單的功能也有助於更有效地組織內容。 using IronWord; using IronWord.Models; class Program { static void Main() { // Load docx WordDocument doc = new WordDocument(); // Create and add styled text to a paragraph Paragraph paragraph = new Paragraph(); // Adding regular text paragraph.AddTextRun(new TextRun("Exploring text styles within a document.")); // Adding italic text paragraph.AddTextRun(new TextRun("An example in italic.", new TextStyle { IsItalic = true })); // Adding bold text paragraph.AddTextRun(new TextRun("An example in bold.", new TextStyle { IsBold = true })); // Add paragraph to the document and export docx doc.AddParagraph(paragraph); doc.SaveAs("newdocument.docx"); } } using IronWord; using IronWord.Models; class Program { static void Main() { // Load docx WordDocument doc = new WordDocument(); // Create and add styled text to a paragraph Paragraph paragraph = new Paragraph(); // Adding regular text paragraph.AddTextRun(new TextRun("Exploring text styles within a document.")); // Adding italic text paragraph.AddTextRun(new TextRun("An example in italic.", new TextStyle { IsItalic = true })); // Adding bold text paragraph.AddTextRun(new TextRun("An example in bold.", new TextStyle { IsBold = true })); // Add paragraph to the document and export docx doc.AddParagraph(paragraph); doc.SaveAs("newdocument.docx"); } } Imports IronWord Imports IronWord.Models Friend Class Program Shared Sub Main() ' Load docx Dim doc As New WordDocument() ' Create and add styled text to a paragraph Dim paragraph As New Paragraph() ' Adding regular text paragraph.AddTextRun(New TextRun("Exploring text styles within a document.")) ' Adding italic text paragraph.AddTextRun(New TextRun("An example in italic.", New TextStyle With {.IsItalic = True})) ' Adding bold text paragraph.AddTextRun(New TextRun("An example in bold.", New TextStyle With {.IsBold = True})) ' Add paragraph to the document and export docx doc.AddParagraph(paragraph) doc.SaveAs("newdocument.docx") End Sub End Class $vbLabelText $csharpLabel 新增表格:表格是 DOCX 檔案的重要組成部分,使用 IronWord 可以輕鬆操作表格,支援動態文件產生。 您可以新增或刪除行和列,這對於資料量可能變化的動態文件產生至關重要。 合併和分割單元格使您能夠靈活地格式化複雜的表格,而自訂邊框和佈局尺寸則可以打造精緻、專業的外觀。 using IronWord; using IronWord.Models; class Program { static void Main() { // Create a table cell with a paragraph containing text TableCell cell = new TableCell(new Paragraph(new TextRun("Sample text"))); // Configure a common border style for the table BorderStyle borderStyle = new BorderStyle { BorderColor = new IronColor(IronSoftware.Drawing.Color.Black), BorderValue = IronWord.Models.Enums.BorderValues.Thick, BorderSize = 5 }; // Apply the border style to the cell cell.Borders = new TableBorders { TopBorder = borderStyle, RightBorder = borderStyle, BottomBorder = borderStyle, LeftBorder = borderStyle }; // Create a table row and add the same cell twice TableRow row = new TableRow(); row.AddCell(cell); row.AddCell(cell); // Create a table, add the row, then create and export the Word document Table table = new Table(); table.AddRow(row); WordDocument doc = new WordDocument(table); doc.SaveAs("Document.docx"); } } using IronWord; using IronWord.Models; class Program { static void Main() { // Create a table cell with a paragraph containing text TableCell cell = new TableCell(new Paragraph(new TextRun("Sample text"))); // Configure a common border style for the table BorderStyle borderStyle = new BorderStyle { BorderColor = new IronColor(IronSoftware.Drawing.Color.Black), BorderValue = IronWord.Models.Enums.BorderValues.Thick, BorderSize = 5 }; // Apply the border style to the cell cell.Borders = new TableBorders { TopBorder = borderStyle, RightBorder = borderStyle, BottomBorder = borderStyle, LeftBorder = borderStyle }; // Create a table row and add the same cell twice TableRow row = new TableRow(); row.AddCell(cell); row.AddCell(cell); // Create a table, add the row, then create and export the Word document Table table = new Table(); table.AddRow(row); WordDocument doc = new WordDocument(table); doc.SaveAs("Document.docx"); } } Imports IronWord Imports IronWord.Models Friend Class Program Shared Sub Main() ' Create a table cell with a paragraph containing text Dim cell As New TableCell(New Paragraph(New TextRun("Sample text"))) ' Configure a common border style for the table Dim borderStyle As New BorderStyle With { .BorderColor = New IronColor(IronSoftware.Drawing.Color.Black), .BorderValue = IronWord.Models.Enums.BorderValues.Thick, .BorderSize = 5 } ' Apply the border style to the cell cell.Borders = New TableBorders With { .TopBorder = borderStyle, .RightBorder = borderStyle, .BottomBorder = borderStyle, .LeftBorder = borderStyle } ' Create a table row and add the same cell twice Dim row As New TableRow() row.AddCell(cell) row.AddCell(cell) ' Create a table, add the row, then create and export the Word document Dim table As New Table() table.AddRow(row) Dim doc As New WordDocument(table) doc.SaveAs("Document.docx") End Sub End Class $vbLabelText $csharpLabel ! .NET Word API(開發者使用指南):圖 1 - 包含表格的輸出 PDF 文檔元素 新增文字運作:此功能著重於對文字內容進行精細控制。 您可以新增、追加和分割文本,這對於動態建立文件至關重要。 樣式選項非常豐富,包括更改字體系列、大小和顏色,以及添加粗體、斜體和其他文字裝飾。 您也可以將圖像嵌入文字中,從而創建內容豐富、視覺效果引人入勝的文件。 新增圖片:IronWord 允許在 Word 文件中進行全面的影像處理。 您可以從各種來源載入圖像,使文字無縫環繞圖像,並調整圖像尺寸以適應您的佈局。 透過設定位置偏移量和與文件邊角的距離,可以確保您的影像始終完美放置。 using IronWord; using IronWord.Models; class Program { static void Main() { WordDocument doc = new WordDocument(); // Load and configure the image IronWord.Models.Image image = new IronWord.Models.Image("your-image.jpg") { Width = 200, // In unit pixels Height = 200 // In unit pixels }; // Create paragraph, add image, add paragraph to document, and export Paragraph paragraph = new Paragraph(); paragraph.AddImage(image); // Add paragraph containing the image to the document doc.AddParagraph(paragraph); doc.SaveAs("save_document.docx"); } } using IronWord; using IronWord.Models; class Program { static void Main() { WordDocument doc = new WordDocument(); // Load and configure the image IronWord.Models.Image image = new IronWord.Models.Image("your-image.jpg") { Width = 200, // In unit pixels Height = 200 // In unit pixels }; // Create paragraph, add image, add paragraph to document, and export Paragraph paragraph = new Paragraph(); paragraph.AddImage(image); // Add paragraph containing the image to the document doc.AddParagraph(paragraph); doc.SaveAs("save_document.docx"); } } Imports IronWord Imports IronWord.Models Friend Class Program Shared Sub Main() Dim doc As New WordDocument() ' Load and configure the image Dim image As New IronWord.Models.Image("your-image.jpg") With { .Width = 200, .Height = 200 } ' Create paragraph, add image, add paragraph to document, and export Dim paragraph As New Paragraph() paragraph.AddImage(image) ' Add paragraph containing the image to the document doc.AddParagraph(paragraph) doc.SaveAs("save_document.docx") End Sub End Class $vbLabelText $csharpLabel 新增形狀:形狀可以為文件增添顯著的視覺效果,IronWord 為您提供了精確插入和自訂形狀的工具。 您可以設定形狀類型(如矩形、圓形、箭頭等),確定文字應如何環繞形狀,指定精確的尺寸和位置,甚至旋轉形狀以達到所需的視覺效果。 相容性 .NET 版本和專案類型 ! .NET Word API(開發者使用指南):圖 2 - IronWord 相容的 .NET 版本和專案類型 IronWord 旨在與 .NET 生態系統廣泛相容,支援各種 .NET 版本(包括 .NET Core、.NET Standard 和 .NET Framework)上的 C#、VB.NET 和 F#。 這確保了它在現代應用和傳統應用中的實用性。 該程式庫的多功能性也體現在專案類型上,透過與 Blazor、WebForms、Xamarin、MAUI、WPF 和控制台應用程式的集成,可滿足 Web、行動和桌面應用程式的需求。 應用環境 ! .NET Word API(開發者使用方法):圖 3 - IronWord 可運行的應用程式環境 在應用環境方面,IronWord 可適應 Windows、Linux、iOS 和 Android 平台,包括對容器化和在 Docker、Azure 和 AWS 上進行雲端部署的特定支援。 這種廣泛的支持有助於在不同環境下的發展。 作業系統和整合開發環境 ! .NET Word API(開發者使用方法):圖 4 - 作業系統與 IDE 的 IronWord 與 IronWord 也相容於主流的整合開發環境 (IDE),例如 Microsoft Visual Studio、ReSharper 和 Rider,為開發人員提供了選擇工具的靈活性。 最後,它支援各種作業系統和處理器架構(x64、x86、ARM),確保在各種硬體配置下都能高效運作。 授權選項 ! .NET Word API(開發者使用指南):圖 5 - IronWord 授權頁面 IronWord 提供多種授權選項,以滿足不同開發者和組織的需求。 他們提供永久授權,這意味著您只需支付一次費用,無需支付任何後續費用。 每個許可證都包含一年的產品支援和更新。 許可證等級是根據您的開發人員數量、地點和專案數量設計的。 您還可以獲得免費試用版,以便在購買許可證之前獲得實際操作經驗。 精簡版許可證 此選項專為單獨開發專案的個人開發人員量身定制。 它的定價為$799 ,適用於單一地點的單一開發者。 Plus 許可證 此許可證面向小型團隊,售價為 $plusLicense,最多可供三名開發人員使用,並適用於三個地點的三個項目。 專業 License 對於規模較大的團隊,專業授權的價格為 $professionalLicense,最多可支援十名開發人員。 它旨在滿足更大規模的操作需求,並包含高級支援功能。 結論 綜上所述, IronWord是一款強大且靈活的 .NET Word API,提供一系列授權選項,以滿足個人開發者和團隊的各種需求。 它的功能可以有效率地管理和操作 Word 文檔,確保與多個 .NET 版本和專案類型相容。 常見問題解答 不使用 Office Interop,如何在 .NET 中操作 Word 文件? 您可以使用 IronWord for .NET 這個 .NET Word 函式庫,它可以讓您在不需要安裝 Microsoft Word 的情況下,以程式化的方式操作 Word 文件。它提供了有效地建立、編輯和轉換 Word 文件的工具。 用於文件處理的 .NET Word API 有哪些主要功能? .NET Word API,特別是 IronWord,提供的功能包括讀取和編輯 Word 文件、支援多種檔案格式、編輯頁面設定,以及新增段落、表格、文字運行、影像和形狀等元素。 如何在 .NET 中自動產生報表和郵件合併? IronWord 是在 .NET 應用程式中自動執行報表生成和郵件合併等任務的理想工具。它允許您以程式化的方式建立和編輯 Word 文件,以簡化這些流程。 .NET Word API 支援哪些平台? IronWord 支援多種平台,包括 Windows、Linux、iOS 和 Android。它也相容於 Docker、Azure 和 AWS 上的雲端部署,使其成為適用於不同環境的多功能工具。 是否可以使用 .NET Word API 修改文件結構? 是的,IronWord 提供修改文件結構的全面工具,包括新增和移除段落、表格和其他元素。它允許對文件佈局進行廣泛的自訂。 .NET Word API 有哪些授權選項? IronWord 提供多種 License 選項,包括 Lite、Plus 和 Professional License,旨在滿足個人開發人員和不同規模組織的需求。 我可以在購買前試用 .NET Word API 嗎? 是的,IronWord 提供免費試用版,讓開發人員在決定購買授權之前,可以探索其特色與功能。 哪些開發環境與 IronWord 相容? IronWord 與一系列開發環境相容,包括 Blazor、WebForms、Xamarin、MAUI、WPF 和主控台應用程式,支援跨 .NET Core、.NET Standard 和 .NET Framework 的 C#、VB.NET 和 F#。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新9月 18, 2025 ASP .NET Core 匯入和匯出 Word 檔案 本指南探討如何匯入現有的 Word 文件、顯示其內容,以及使用 IronWord 函式庫從頭建立文件 閱讀更多 更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多 更新6月 22, 2025 如何使用 C# 在 Word 中對齊文字 讓我們深入瞭解 IronWord NuGet 套件,以及如何使用此套件對齊文字或段落 閱讀更多 如何以 C# 將文件匯出至 Word如何執行 C# Word 自動化
更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多