IronWord入門指南

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

IronWord:適用於 .NET 的 Word 文件庫

IronWord是由Iron Software開發的Word文件庫。 IronWord 在為 .NET 應用程式中處理 Word 文件提供強大的功能方面表現出色。

  • 載入、編輯和儲存 Word 和 Docx 文件。
  • PageSetup: 配置紙張大小、頁面方向、頁邊距和背景顏色。
  • TextRun: 處理文本內容、樣式、分割、附加文本和添加圖片。
  • TextStyle: 管理字型系列、大小、顏色、加粗、斜體、刪除線、底線、上標和下標。
  • Paragraph: 添加文本片段、圖片、形狀,設定樣式、對齊、項目符號和編號清單。
  • Table: 操作表格結構,包括添加行、獲取和設置單元格值、刪除行、合併單元格等。
  • Image: 從文件或流中載入圖片,設定文字環繞、位置偏移、寬度、高度和其他屬性。
  • Shape: 設定文字環繞、位置偏移、寬度、高度、形狀類型和旋轉。

安裝

IronWord圖書館

安裝 IronWord 快速又簡單。 您可以使用 NuGet 透過以下命令安裝該軟體包:

PM > Install-Package IronWord

或者,直接從官方IronWord NuGet網站下載。

安裝完成後,您可以透過將using IronWord;添加到您的C#程式碼文件頂部來開始。

應用許可證密鑰

接下來,請將有效的授權或試用金鑰應用於IronWord,方法是將授權金鑰分配給LicenseKey 類別的 License 屬性。 在導入語句之後、使用任何 IronWord 方法之前,先加入以下程式碼:

using IronWord;

// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";

程式碼範例

讓我們來探討一些代碼範例和可用的功能。

請注意: 由IronWord生成的DOCX文件,在某些版本的Microsoft Word中打開時可能處於相容模式,導致某些樣式不可用。 將Word文檔從相容模式中轉換出
  1. 選擇“檔案”>“資訊”,然後點選“轉換”。
  2. 系統會提示您,您的文件將會升級為最新的文件格式。 點選“確定”。

建立 Word 和 Docx 文檔

透過使用WordDocument類的其中一個構造器來實例化Word文件。 之後,使用SaveAs方法導出Word文件。 範例:

using IronWord;

class Program
{
    static void Main()
    {
        // Create a new Word document
        var document = new WordDocument();

        // Save the document as a .docx file
        document.SaveAs("example.docx");
    }
}

新增圖片

圖片不能單獨添加; 相反地,它應該被添加到文件結構之一,例如 Paragraph、TableCell 或 Section。 使用AddImage方法添加圖片。 範例:

using IronWord;
using IronWord.Models;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = new DocumentSection();
        document.AddSection(section);

        // Add an image to a paragraph
        var paragraph = new Paragraph();
        section.AddParagraph(paragraph);
        paragraph.AddImage(new ImageContent("path/to/image.jpg") { Width = 100, Height = 100 });

        document.SaveAs("example_with_image.docx");
    }
}

新增表格

新增表格需要建立表格、行、列和表格儲存格。 這提供了很大的配置可能性,因為每個單元格都可以有不同的樣式。 範例:

using IronWord;
using IronWord.Models;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = new DocumentSection();
        document.AddSection(section);
        var table = new Table(3, 3); // 3x3 table
        section.AddTable(table);

        // Iterate over cells and set their content
        for (int i = 0; i < table.Rows.Count; i++)
        {
            for (int j = 0; j < table.Rows[i].Cells.Count; j++)
            {
                ((TableCell)table.Rows[i].Cells[j]).AddParagraph(new Paragraph(new TextContent($"Cell {i+1},{j+1}")));
            }
        }

        document.SaveAs("example_with_table.docx");
    }
}

提供許可和支援

IronWord是一個付費庫存; 不過,這裡提供免費試用授權。

如需更多有關Iron Software的資訊,請訪問我們的網站:https://ironsoftware.com/。 如需更多支援和諮詢,請詢問我們的團隊。

Iron Software 提供的支援

如需一般支援和技術詢問,請通過電子郵件與我們聯繫:support@ironsoftware.com

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 63,050版本:2026.11剛剛發布