IronWord 入門指南
IronWord:適用於 .NET 的 Word 文件程式庫
IronWord 是由 Iron Software 開發的 Word 文件程式庫。 IronWord 擅長為 .NET 應用程式提供強大的 WORD 文件處理功能。
- 載入、編輯及儲存 WORD 和 Docx 文件。
PageSetup:設定紙張尺寸、頁面方向、邊距及背景顏色。TextRun:處理文字內容、樣式、分割、追加文字以及插入圖片。TextStyle:管理字型家族、大小、顏色、粗體、斜體、刪除線、底線、上標及下標。Paragraph:新增文字行、圖片、圖形,以及設定樣式、對齊方式、項目符號和編號清單。Table:操作表格結構,包括新增列、取得與設定儲存格值、刪除列、合併儲存格等。Image:從檔案或串流載入圖片,並設定文字換行、位置偏移、寬度、高度及其他屬性。Shape:設定文字換行、位置偏移、寬度、高度、形狀類型及旋轉角度。
適用於 .NET 的 WORD 文件 C# 函式庫
- 下載用於處理 DOCX 文件的 C# 函式庫
- 建立及修改 WORD 和 DOCX 文件
- 請加入段落、章節和表格等文件結構
- 加入文件元素,例如文字區塊、圖片和圖形
- 輕鬆為文件元素設定樣式
安裝
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"
程式碼範例
讓我們來探索一些程式碼範例及可用的功能。
- 選擇"檔案">"資訊",然後按一下"轉換"。
- 系統將顯示一則訊息,告知您的文件即將升級至最新檔案格式。 點擊"確定"。
建立 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
添加圖片
無法單獨添加圖片; 相反地,應將其加入其中一種文件結構中,例如 TableCell 或 Section。 請使用 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
新增表格
要新增表格,需建立表格、列、行及表格儲存格。 這提供了豐富的配置可能性,因為每個儲存格都可以採用不同的樣式。 範例:
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
提供授權與技術支援
IronWord 是一款付費程式庫; 不過,您可在此取得免費試用授權。
https://ironsoftware.com/如需更多關於 Iron Software 的資訊,請造訪我們的網站:。 如需更多支援或有任何疑問,請聯繫我們的團隊。
Iron Software 提供支援
如需一般支援或技術諮詢,請透過電子郵件聯絡我們: support@ironsoftware.com/support@ironsoftware.com

