使用IRONWORD

.NET Word API(其工作原理)

發佈 2024年4月3日
分享:

介紹 .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;
// Load docx
WordDocument doc = new WordDocument();
// Create and add styled text to a paragraph
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(new TextRun("Exploring text styles within a document."));
paragraph.AddTextRun(new TextRun("An example in italic.", new TextStyle { IsItalic = true }));
paragraph.AddTextRun(new TextRun("An example in bold.", new TextStyle { IsBold = true }));
// Add paragraph and export docx
doc.AddParagraph(paragraph);
doc.SaveAs("newdocument.docx");
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Create and add styled text to a paragraph
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(new TextRun("Exploring text styles within a document."));
paragraph.AddTextRun(new TextRun("An example in italic.", new TextStyle { IsItalic = true }));
paragraph.AddTextRun(new TextRun("An example in bold.", new TextStyle { IsBold = true }));
// Add paragraph and export docx
doc.AddParagraph(paragraph);
doc.SaveAs("newdocument.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
' Create and add styled text to a paragraph
Private paragraph As New Paragraph()
paragraph.AddTextRun(New TextRun("Exploring text styles within a document."))
paragraph.AddTextRun(New TextRun("An example in italic.", New TextStyle With {.IsItalic = True}))
paragraph.AddTextRun(New TextRun("An example in bold.", New TextStyle With {.IsBold = True}))
' Add paragraph and export docx
doc.AddParagraph(paragraph)
doc.SaveAs("newdocument.docx")
VB   C#

添加表格: 表格 是 DOCX 文件的關鍵組成部分,並且可以使用 IronWord 輕鬆操作,支援動態文檔生成。您可以添加或刪除行和列,這對於資料量可能變動的動態文檔生成至關重要。合併和拆分單元格使您可以靈活地格式化複雜的表格,自訂邊框和佈局尺寸則允許文檔看起來更精美、專業。

using IronWord;
using IronWord.Models;
// 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;
// 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");
Imports IronWord
Imports IronWord.Models
' Create a table cell with a paragraph containing text
Private cell As New TableCell(New Paragraph(New TextRun("Sample text")))
' Configure a common border style for the table
Private borderStyle As New BorderStyle With {
	.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 With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}
' Create a table row and add the same cell twice
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)
' Create a table, add the row, then create and export the Word document
Dim table As New Table()
table.AddRow(row)
Dim doc As New WordDocument(table)
doc.SaveAs("Document.docx")
VB   C#

.NET Word API(開發者使用說明):圖1 - 含有表格的輸出PDF

文件元素

添加文本运行:此功能關於對文本內容的細粒度控制。您可以添加、附加和拆分文本運行,這對於動態文檔建立至關重要。樣式選項豐富,包括更改字體系列、大小和顏色,還可以添加粗體、斜體和其他文本裝飾。您還可以在文本運行中嵌入圖像,創建豐富、視覺吸引力的文檔。

添加圖像:IronWord 允許完全 圖像處理 在Word文件中。您可以從各種來源加載圖像,將文字無縫地環繞在圖像周圍,並調整其尺寸以適應您的佈局。設置位置偏移和距離文件邊角的能力意味著您的圖像將始終完美放置。

using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
IronWord.Models.Image image = new IronWord.Models.Image("your-image.jpg")
{
    Width = 200, // In unit pixel
    Height = 200 // In unit pixel
};
Create paragraph, add image, add paragraph to doc, and export
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image);
doc.AddParagraph(paragraph);
doc.SaveAs("save_document.docx");
using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
IronWord.Models.Image image = new IronWord.Models.Image("your-image.jpg")
{
    Width = 200, // In unit pixel
    Height = 200 // In unit pixel
};
Create paragraph, add image, add paragraph to doc, and export
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image);
doc.AddParagraph(paragraph);
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
Private image As New IronWord.Models.Image("your-image.jpg") With {
	.Width = 200,
	.Height = 200
}
Private paragraph, add, add, [and] As Create
paragraph.AddImage(image)
doc.AddParagraph(paragraph)
doc.SaveAs("save_document.docx")
VB   C#

新增形狀: 形狀可以為文檔增添顯著的視覺效果,IronWord 提供您插入和精確自訂形狀的工具。您可以設置形狀類型 (像矩形、圓形、箭頭等。), 確定文字應如何環繞形狀,指定精確的尺寸和定位,甚至旋轉形狀以達到所需的視覺效果。

相容性

.NET 版本與專案類型

.NET Word API(對開發者如何運作):圖 2 - IronWord 相容的 .NET 版本和專案類型

IronWord 專為 .NET 生態系統內的廣泛兼容性而設計, 支援各種 .NET 版本中的 C#、VB.NET 和 F#, 包括 .NET Core、.NET Standard 和 .NET Framework。這確保了它在現代和傳統應用中的實用性。 該庫的多功能性延伸到項目類型, 通過與 Blazor、WebForms、Xamarin、MAUI、WPF 和控制台應用整合來適應網頁、移動和桌面應用。

應用環境

.NET Word API(開發人員使用情況):圖3 - IronWord 可以工作的應用環境

在應用環境方面,IronWord 能夠適應 Windows、Linux、iOS 和 Android 平台,包括對 Docker、Azure 和 AWS 上的容器化和雲部署的特定支持。這種廣泛的支持促進了不同環境下的開發。

作業系統與整合開發環境

.NET Word API (如何為開發人員工作):圖 4 - 與 IronWord 相容的作業系統和 IDE

IronWord 也兼容主要的集成開發環境 (集成開發環境) 例如 Microsoft Visual Studio、ReSharper 和 Rider,提供開發人員在選擇工具上的靈活性。最後,它支援各種作業系統和處理器架構。 (x64、x86、ARM),確保在不同的硬體配置下高效運行。

授權選項

.NET Word API(如何為開發人員工作):圖 5 - IronWord 許可證頁面

IronWord 提供多種授權選項,以滿足不同開發者和組織的需求。他們提供永久許可,也就是說您只需支付一次費用,沒有任何經常性費用。每個許可包括一年產品支援和更新。授權級別是根據開發者數量、地點和項目數量設計的。您還可以獲得 免費試用 在購買授權之前獲得實際操作經驗。

Lite License

此選項專為單獨在專案上工作的個人開發人員量身定制。價格為 $749,涵蓋同一地點的一名開發人員。

Plus License

專為小型團隊設計,此授權以$1,499出售,最多可供三名開發人員使用,適用於三個地點和三個專案中。

專業授權

對於較大的團隊,專業授權的價格為 $2,999,支持最多十位開發者。它旨在滿足更大規模的操作需求,並包含高級支援功能。

結論

總結來說, IronWord 作為一個強大且靈活的 .NET Word API,它提供了一系列的許可選項,以滿足個人開發者和團隊的多樣化需求。其功能可以有效管理和操作 Word 文件,保證在多個 .NET 版本和項目類型之間的兼容性。

< 上一頁
如何將文件導出為 Word 在 C# 中
下一個 >
如何執行C# Word自動化

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 5,614 查看許可證 >