使用IRONWORD

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

发布 2024年四月3日
分享:

简介

袠薪褎芯褉屑邪褑懈芯薪薪褘泄 .NET Word API 为开发人员提供了强大的工具,以便在其应用程序中转换 Word 文档、与 MS Word 文档交互并对其进行操作。该 API 旨在简化 Microsoft Word 文档的处理流程,使以编程方式创建、编辑、转换和管理文档变得更加容易。在本文中,我们将探讨 铁字 以了解其操作 Word 文档的能力。

IronWord 简介

铁字 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这对于编辑和格式化大段文本至关重要。此外,你还可以直接在文本中插入图片和形状,调整样式以符合你的设计规范,并设置对齐方式以获得完美的外观,从而增强段落效果。添加子弹和编号列表的功能也有助于更有效地组织内容。

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 上的容器化和云部署的特定支持。这种广泛的支持有助于跨不同环境进行开发。

OS & IDE

.NET Word API(如何为开发人员工作):图 4 - IronWord 与操作系统和集成开发环境兼容

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

许可选项

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

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

简易许可证

此选项专为单独开发项目的个人开发人员定制。它的价格为 $749,涵盖单一地点的一名开发人员。

Plus License

该许可证面向小型团队,售价 1499 美元,最多可容纳三名开发人员,适用于三个项目的三个地点。

专业执照

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

结论

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

< 前一页
如何在C#中导出文档到Word
下一步 >
如何执行C# Word自动化

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

免费NuGet下载 总下载量: 4,816 查看许可证 >