跳過到頁腳內容
使用 IRONWORD

.NET Word API(對開發人員的工作原理)

The .NET Word API provides developers with robust tools to convert Word documents, and interact with, and manipulate MS Word documents within their applications. This API is designed to streamline the process of working with Microsoft Word documents, making it easier to create, edit, convert, and manage documents programmatically. In this article, we'll explore IronWord to understand its capabilities in manipulating Word documents.

Introduction to IronWord

IronWord is a .NET Word library within the .NET Word API ecosystem, designed specifically for developers who process Microsoft Word documents in their .NET applications. With IronWord, developers can easily read, write, and modify Word documents without needing Microsoft Word installed on the server or client machines. This capability is particularly beneficial for applications that need to automate document processing tasks, such as generating reports, invoices, or personalized correspondence through mail merge functionalities.

Features of IronWord

IronWord provides a comprehensive range of features that cater to various aspects of Word document manipulation. Let's explore each set of features, focusing on how they enable the manipulation and merging of multiple documents, categorized under 'Document Structure' and 'Document Elements'.

Document Structure

Read & Edit Word: With IronWord, you can pull out specific information from your Word documents, such as extracting text for editing or repurposing and retrieving images that may need to be used elsewhere. This capability is vital for applications aimed at merging Word documents and processing the information contained in existing DOCX files.

Multiple Formats: IronWord supports a wide range of file formats, enhancing its utility in converting Word documents within .NET applications.

Edit Page Setup: Tailoring the physical layout of your Word documents is straightforward with IronWord. You can adjust the paper size for various MS Word files to standard or custom dimensions, change the orientation for different sections of your document, set the margins to ensure proper alignment, and even modify the background color for aesthetic purposes or to highlight sections.

Add Paragraphs: IronWord enables the addition and removal of text runs within paragraphs, which is essential for editing and formatting large sections of text. Moreover, you can enhance your paragraphs by inserting images and shapes directly into the text, adjusting the styling to match your design specifications, and setting alignments for a polished look. The ability to add bullets and numbering lists also helps in organizing content more effectively.

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");
    }
}
Imports IronWord
Imports IronWord.Models

Friend Class Program
	Shared Sub Main()
		' Load docx
		Dim doc As New WordDocument()
		' Create and add styled text to a paragraph
		Dim paragraph As 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 With {.IsItalic = True}))

		' Adding bold text
		paragraph.AddTextRun(New TextRun("An example in bold.", New TextStyle With {.IsBold = True}))

		' Add paragraph to the document and export docx
		doc.AddParagraph(paragraph)
		doc.SaveAs("newdocument.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Add Tables: Tables are a critical component of DOCX files and are easily manipulated with IronWord, supporting dynamic document generation. You can add or remove rows and columns, an operation that is key for dynamic document generation where the amount of data can vary. Merging and splitting cells give you the versatility to format complex tables, and customizing borders and layout dimensions allows for a polished, professional look.

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");
    }
}
Imports IronWord
Imports IronWord.Models

Friend Class Program
	Shared Sub Main()
		' Create a table cell with a paragraph containing text
		Dim cell As New TableCell(New Paragraph(New TextRun("Sample text")))

		' Configure a common border style for the table
		Dim 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")
	End Sub
End Class
$vbLabelText   $csharpLabel

.NET Word API (How It Works For Developers): Figure 1 - Outputted PDF with a table

Document Elements

Add TextRuns: This feature focuses on fine-grained control over the text content. You can add, append, and split text runs, which is vital for dynamic document creation. Styling options are extensive, including changing font families, sizes, and colors, as well as adding bold, italics, and other text decorations. You can also embed images within text runs, creating a rich, visually engaging document.

Add Images: IronWord allows for comprehensive image manipulation within Word Documents. You can load images from various sources, wrap text around them seamlessly, and adjust their dimensions to fit your layout. The ability to set the position offset and the distance from the corners of the document ensures that your images will always be perfectly placed.

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");
    }
}
Imports IronWord
Imports IronWord.Models

Friend Class Program
	Shared Sub Main()
		Dim doc As New WordDocument()

		' Load and configure the image
		Dim image As New IronWord.Models.Image("your-image.jpg") With {
			.Width = 200,
			.Height = 200
		}

		' Create paragraph, add image, add paragraph to document, and export
		Dim paragraph As New Paragraph()
		paragraph.AddImage(image)

		' Add paragraph containing the image to the document
		doc.AddParagraph(paragraph)
		doc.SaveAs("save_document.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Add Shapes: Shapes can add significant visual impact to a document, and IronWord gives you the tools to insert and customize them with precision. You can set the shape type (like rectangles, circles, arrows, etc.), determine how text should wrap around the shape, specify exact dimensions and positioning, and even rotate shapes to achieve the desired visual effect.

Compatibility

.NET Versions & Project Types

.NET Word API (How It Works For Developers): Figure 2 - The .NET versions and project types that IronWord is compatible with

IronWord is designed for broad compatibility within the .NET ecosystem, supporting C#, VB.NET, and F# across various .NET versions, including .NET Core, .NET Standard, and .NET Framework. This ensures its utility in both contemporary and legacy applications. The library's versatility extends to project types, accommodating web, mobile, and desktop applications through integration with Blazor, WebForms, Xamarin, MAUI, WPF, and console applications.

App Environments

.NET Word API (How It Works For Developers): Figure 3 - The app environments IronWord can work in

In terms of application environments, IronWord is adaptable to Windows, Linux, iOS, and Android platforms, including specific support for containerization and cloud deployment on Docker, Azure, and AWS. This wide-ranging support facilitates development across different environments.

OS & IDE

.NET Word API (How It Works For Developers): Figure 4 - The OS and IDE's IronWord is compatible with

IronWord is also compatible with major Integrated Development Environments (IDEs) such as Microsoft Visual Studio, ReSharper, and Rider, offering developers flexibility in their choice of tools. Lastly, it supports various operating systems and processor architectures (x64, x86, ARM), ensuring efficient performance across diverse hardware configurations.

Licensing Options

.NET Word API (How It Works For Developers): Figure 5 - IronWord licensing page

IronWord offers various licensing options to accommodate the needs of different developers and organizations. They offer a perpetual license, which means you pay once and there are no recurring fees. Every license includes one year of product support and updates. The licensing tiers are designed based on the number of developers, locations, and projects you have. You can also get a free trial to get hands-on experience before purchasing a license.

Lite License

This option is tailored for individual developers working solo on a project. It is priced at $749 and covers one developer in a single location.

Plus License

Aimed at small teams, this license, for $plusLicense, accommodates up to three developers and is applicable for use in three locations across three projects.

Professional License

For larger teams, the Professional License is priced at $professionalLicense and supports up to ten developers. It's designed to cater to more substantial operations and includes premium support features.

Conclusion

In wrapping up, IronWord emerges as a robust and flexible .NET Word API, offering a range of licensing options to meet the diverse needs of individual developers and teams. Its features enable efficient management and manipulation of Word documents, ensuring compatibility across multiple .NET versions and project types.

常見問題解答

如何在不使用 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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。