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;來開始使用。
應用許可證密鑰
接下來,透過將授權金鑰指派給License類別的LicenseKey屬性,將有效的授權金鑰或試用金鑰套用至 IronWord。 在導入語句之後、使用任何 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新增圖片
圖片不能單獨添加; 相反,應將其添加到文件結構中,例如Paragraph 、 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.Count; i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count; 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.Count; i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count; j++)
{
table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
}
}
document.SaveAs("example_with_table.docx");
}
}Imports IronWord
Friend Class Program
Shared 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
Dim j As Integer = 0
Do While j < table.Rows(i).Cells.Count
table.Rows(i).Cells(j).Paragraphs.Add().AppendText($"Cell {i+1},{j+1}")
j += 1
Loop
Next i
document.SaveAs("example_with_table.docx")
End Sub
End Class提供許可和支持
IronWord是一個付費庫存; 不過,您可以在這裡獲得免費試用許可證。
有關 Iron Software 的更多信息,請訪問我們的網站:https://ironsoftware.com/ 。 如需更多協助或有任何疑問,請聯絡我們的團隊。
Iron Software 提供的支持
如需一般支援和技術諮詢,請發送電子郵件至:support@ironsoftware.com






