IronWord 入門指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

IronWord 是由 Iron Software 開發的 Word 文件程式庫。 IronWord 擅長為 .NET 應用程式提供強大的 WORD 文件處理功能。

  • 載入、編輯及儲存 WORD 和 Docx 文件。
  • PageSetup:設定紙張尺寸、頁面方向、邊距及背景顏色。
  • TextRun:處理文字內容、樣式、分割、追加文字以及插入圖片。
  • TextStyle:管理字型家族、大小、顏色、粗體、斜體、刪除線、底線、上標及下標。
  • Paragraph:新增文字行、圖片、圖形,以及設定樣式、對齊方式、項目符號和編號清單。
  • Table:操作表格結構,包括新增列、取得與設定儲存格值、刪除列、合併儲存格等。
  • Image:從檔案或串流載入圖片,並設定文字換行、位置偏移、寬度、高度及其他屬性。
  • Shape:設定文字換行、位置偏移、寬度、高度、形狀類型及旋轉角度。

安裝

IronWord程式庫

安裝 IronWord 既快速又簡單。 您可以使用 NuGet 並透過以下指令安裝此套件:

Install-Package IronWord

您亦可直接從 IronWord 官方 NuGet 網站下載。

安裝完成後,您只需在 C# 程式碼檔案頂端加入 using IronWord; 即可開始使用。

套用授權金鑰

接著,請將有效的授權金鑰或試用授權套用至 IronWord,方法是將授權金鑰指派給 License 類別的 LicenseKey 屬性。 請在 import 語句之後、使用任何 IronWord 方法之前,加入以下程式碼:

using IronWord;

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

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

' Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
$vbLabelText   $csharpLabel

程式碼範例

讓我們來探索一些程式碼範例及可用的功能。

請注意由 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");
    }
}
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");
    }
}
Imports IronWord

Friend Class Program
	Shared Sub Main()
		' Create a new Word document
		Dim document = New WordDocument()

		' Save the document as a .docx file
		document.SaveAs("example.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

添加圖片

無法單獨添加圖片; 相反地,應將其加入其中一種文件結構中,例如 TableCellSection。 請使用 AddImage 方法插入圖片。 範例:

using IronWord;
using System.Drawing;

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

        // Add an image to a paragraph
        var paragraph = section.Paragraphs.Add();
        paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));

        document.SaveAs("example_with_image.docx");
    }
}
using IronWord;
using System.Drawing;

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

        // Add an image to a paragraph
        var paragraph = section.Paragraphs.Add();
        paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));

        document.SaveAs("example_with_image.docx");
    }
}
Imports IronWord
Imports System.Drawing

Friend Class Program
	Shared Sub Main()
		Dim document = New WordDocument()
		Dim section = document.Sections.Add()

		' Add an image to a paragraph
		Dim paragraph = section.Paragraphs.Add()
		paragraph.AddImage("path/to/image.jpg", New Rectangle(0, 0, 100, 100))

		document.SaveAs("example_with_image.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

新增表格

要新增表格,需建立表格、列、行及表格儲存格。 這提供了豐富的配置可能性,因為每個儲存格都可以採用不同的樣式。 範例:

using IronWord;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();
        var table = section.Tables.Add(3, 3); // 3x3 table

        // Iterate over cells and set their content
        for (int i = 0; i < table.Rows.Co/unt; i++)
        {
            for (int j = 0; j < table.Rows[i].Cells.Co/unt; j++)
            {
                table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
            }
        }

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

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();
        var table = section.Tables.Add(3, 3); // 3x3 table

        // Iterate over cells and set their content
        for (int i = 0; i < table.Rows.Co/unt; i++)
        {
            for (int j = 0; j < table.Rows[i].Cells.Co/unt; j++)
            {
                table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
            }
        }

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

Module Program
    Sub Main()
        Dim document = New WordDocument()
        Dim section = document.Sections.Add()
        Dim table = section.Tables.Add(3, 3) ' 3x3 table

        ' Iterate over cells and set their content
        For i As Integer = 0 To table.Rows.Count - 1
            For j As Integer = 0 To table.Rows(i).Cells.Count - 1
                table.Rows(i).Cells(j).Paragraphs.Add().AppendText($"Cell {i + 1},{j + 1}")
            Next
        Next

        document.SaveAs("example_with_table.docx")
    End Sub
End Module
$vbLabelText   $csharpLabel

提供授權與技術支援

IronWord 是一款付費程式庫; 不過,您可在此取得免費試用授權。

https://ironsoftware.com/如需更多關於 Iron Software 的資訊,請造訪我們的網站:。 如需更多支援或有任何疑問,請聯繫我們的團隊

Iron Software 提供支援

如需一般支援或技術諮詢,請透過電子郵件聯絡我們: support@ironsoftware.com/support@ironsoftware.com

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 44,829 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronWord
執行範例 觀看您的資料轉為 WORD 文件。