跳過到頁腳內容
使用 IRONWORD

.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");
    }
}
$vbLabelText   $csharpLabel

新增表格:表格是 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");
    }
}
$vbLabelText   $csharpLabel

 .NET Word API(開發者使用方法):圖 1 - 包含表格的輸出 PDF

文檔元素

新增文字運作:此功能著重於對文字內容進行精細控制。 您可以新增、追加和分割文本,這對於動態建立文件至關重要。 樣式選項非常豐富,包括更改字體系列、大小和顏色,以及添加粗體、斜體和其他文字裝飾。 您也可以將圖像嵌入文字中,從而創建內容豐富、視覺效果引人入勝的文件。

新增圖片: 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");
    }
}
$vbLabelText   $csharpLabel

增加形狀:形狀可以為文件增添顯著的視覺效果, 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 庫,允許您不需要安裝 Microsoft Word 即可編程操作 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 和控制台應用程序,支持 C#、VB.NET 和 F#,適用於 .NET Core、.NET Standard 和 .NET Framework。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我