跳至页脚内容
使用 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");
    }
}
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

添加表格表格是 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");
    }
}
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(开发者使用指南):图 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");
    }
}
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

添加形状:形状可以为文档增添显著的视觉效果,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 版本和项目类型兼容。

常见问题解答

如何在.NET中不使用Office Interop操作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 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。