.NET Word API(開發者使用指南)
.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");
}
}新增表格:表格是 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");
}
}文檔元素
新增文字運作:此功能著重於對文字內容進行精細控制。 您可以新增、追加和分割文本,這對於動態建立文件至關重要。 樣式選項非常豐富,包括更改字體系列、大小和顏色,以及添加粗體、斜體和其他文字裝飾。 您也可以將圖像嵌入文字中,從而創建內容豐富、視覺效果引人入勝的文件。
新增圖片: 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");
}
}新增形狀:形狀可以為文件增添顯著的視覺效果,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,最多可供三名開發人員使用,並適用於三個地點的三個項目。
專業執照
對於規模較大的團隊,專業授權的價格為 $professionalLicense,最多可支援十名開發人員。 它旨在滿足更大規模的操作需求,並包含高級支援功能。
結論
綜上所述, IronWord是一款強大且靈活的 .NET Word API,提供一系列授權選項,以滿足個人開發者和團隊的各種需求。 它的功能可以有效率地管理和操作 Word 文檔,確保與多個 .NET 版本和專案類型相容。
常見問題解答
如何在不使用 Office Interop 的情況下,在 .NET 中操作 Word 文件?
您可以使用 IronWord,這是一個 .NET Word 庫,它允許您以程式設計方式操作 Word 文檔,而無需安裝 Microsoft 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 提供多種授權選項,包括 Lite、Plus 和 Professional 許可,旨在滿足個人開發者和各種規模組織的需求。
我可以在購買前試用.NET Word API嗎?
是的,IronWord 提供免費試用,讓開發者在決定購買授權之前可以探索其功能和功能。
IronWord相容於哪些開發環境?
IronWord 與多種開發環境相容,包括 Blazor、WebForms、Xamarin、MAUI、WPF 和控制台應用程序,支援 .NET Core、.NET Standard 和 .NET Framework 上的 C#、VB.NET 和 F#。







