使用IRONWORD

.NET Word API(开发人员使用方法)

雷根·彭
雷根·彭
2024年四月3日
分享:

介绍

"(《世界人权宣言》).NET Word API该工具为开发人员提供了强大的工具,可在其应用程序中转换 Word 文档、与 MS Word 文档交互并对其进行操作。 该 API 旨在简化 Microsoft Word 文档的处理流程,使以编程方式创建、编辑、转换和管理文档变得更加容易。 在本文中,我们将探讨铁字以了解其操作 Word 文档的能力。

IronWord 简介

铁字是 .NET Word API 生态系统中的一个 .NET Word 库,专为在其 .NET 应用程序中处理 Microsoft Word 文档的开发人员设计。 有了 IronWord,开发人员无需在服务器或客户端机器上安装 Microsoft Word,即可轻松读取、编写和修改 Word 文档。 对于需要自动执行文档处理任务的应用程序,例如通过邮件合并功能生成报告、发票或个性化信函,这种能力尤其有益。

IronWord 的功能

IronWord 提供全面的功能,可满足 Word 文档操作的各个方面。 让我们来探讨每一组功能,重点是它们如何实现多个文档的操作和合并,这些功能归类于 "文档结构 "和 "文档元素"。

文档结构

阅读和编辑 Word:使用 IronWord,您可以从 Word 文档中提取特定信息,例如提取文本进行编辑或重新利用,以及检索可能需要在其他地方使用的图片。 这种能力对于旨在合并 Word 文档和处理现有 DOCX 文件中包含的信息的应用程序至关重要。

多种格式:IronWord 支持多种文件格式,增强了其在 .NET 应用程序中转换 Word 文档的实用性。

编辑页面设置:使用 IronWord 可以直接定制 Word 文档的物理布局。 您可以将各种 MS Word 文件的纸张大小调整为标准或自定义尺寸,改变文档不同部分的方向,设置页边距以确保正确对齐,甚至还可以修改背景颜色以达到美观或突出显示部分的目的。

添加段落:IronWord 可以添加和删除text runs within paragraphs此外,翻译人员还必须使用.NET、Java、Python 或 Node js,这对于编辑和格式化大段文本至关重要。 此外,您还可以通过在文本中直接插入图片和形状、调整样式以符合设计规范以及设置对齐方式来美化段落。 添加子弹和编号列表的功能也有助于更有效地组织内容。

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

添加表格在 IronWord 中,"...... "是 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")
$vbLabelText   $csharpLabel

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

添加形状:形状可以为文档增添显著的视觉效果,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 与操作系统和集成开发环境兼容

IronWord 还与主要的集成开发环境兼容(集成开发环境)这些工具包括 Microsoft Visual Studio、ReSharper 和 Rider 等,为开发人员提供了灵活的工具选择。 最后,它支持各种操作系统和处理器架构(x64,x86,ARM)确保在各种硬件配置下都能高效运行。

许可选项

.NET Word API(开发人员如何使用):图 5 - IronWord 许可页面

IronWord 提供多种许可选项,以满足不同开发人员和组织的需求。 他们提供永久许可证,这意味着您只需支付一次费用,没有经常性费用。 每个许可证都包含一年的产品支持和更新。 许可级别是根据开发人员、地点和项目的数量设计的。 您还可以获得免费试用在购买许可证之前获得实际操作经验。

Lite 许可证

该选项专为在项目中单独工作的个人开发人员量身定制。 它的定价为"$liteLicense",涵盖单个地点的一名开发人员。

Plus License

该 License 面向小型团队,价格为 1,499 美元,最多可容纳三名开发人员,适用于三个项目的三个地点。

专业许可证

对于规模较大的团队,专业 License 的售价为 2999 美元,最多可支持 10 名开发人员。 它旨在满足更多实质性业务的需求,并包含高级支持功能。

结论

总结 铁字作为一个强大而灵活的 .NET Word API,《.NET Word API》提供了一系列 License 选项,以满足个人开发者和团队的不同需求。 它的功能可以有效地管理和操作 Word 文档,确保在多个 .NET 版本和项目类型中的兼容性。

雷根·彭
软件工程师
Regan毕业于雷丁大学,拥有电子工程学士学位。在加入Iron Software之前,他的前工作职位要求他专注于单一任务;他在Iron Software最喜欢的是能进行多种工作,无论是增加销售价值、技术支持、产品开发还是营销。他喜欢了解开发人员如何使用Iron Software的库,并利用这些知识不断改进文档和开发产品。
< 前一页
如何在C#中导出文档到Word
下一步 >
如何执行C# Word自动化

准备开始了吗? 版本: 2025.4 刚刚发布

查看许可证 >