开始使用 IronWord

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronWord:适用于.NET的Word文档库

IronWord是Iron Software公司开发的Word文档库。IronWord 可在 .NET 应用程序中为处理 Word 文档提供强大的功能。

  • 加载、操作和保存 Word 和 Docx 文档。
  • 页面设置配置纸张大小、页面方向、页边距和背景颜色。
  • TextRun:处理文本内容、样式、分割、附加文本和添加图像。
  • 文本样式管理字体家族、大小、颜色、粗体、斜体、删除线、下划线、上标和下标。
  • 段落添加文本行、图像、形状、设置样式、对齐方式、子弹和编号列表。
  • 表格操作表格结构,包括添加行、获取和设置单元格值、删除行、合并单元格等。
  • 图像从文件或数据流中加载图像,设置换行文字、位置偏移、宽度、高度及其他属性。
  • 形状设置换行文字、位置偏移、宽度、高度、形状类型和旋转。

安装

IronWord 图书馆

安装 IronWord 非常简单快捷,请像这样安装软件包:

Install-Package IronWord

或者直接从 IronWord NuGet 官方网站.

安装完成后,只需在 C# 代码顶部添加 "using IronWord; "即可开始使用。

申请许可证密钥

接下来,通过将许可证密钥分配给 "License "类的 "LicenseKey "属性,将有效的许可证或试用密钥应用到 IronWord 中。在使用任何 IronWord 方法之前,在导入语句之后加入以下代码:

:path=/static-assets/word/content-code-examples/get-started/get-started-license.cs
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01";
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01"
VB   C#

代码示例

IronWord 生成的 DOCX 文件在特定版本的 Microsoft Word 中打开时,可能处于兼容模式,导致某些样式不可用。要将 Word 文档转换为非兼容模式,请执行以下操作

1.选择 "文件">"信息",然后点击 "转换"。

2.系统将提示您,您的文档将升级到最新的文件格式。点击 "确定"。

创建 Word 和 Docx 文档

使用WordDocument类的一个构造函数实例化该类,创建 Word 文档。然后,使用 SaveAs 方法导出 Word 文档。

:path=/static-assets/word/content-code-examples/get-started/get-started-1.cs
using IronWord;
using IronWord.Models;

// Create textrun
Text textRun = new Text("Sample text");

Paragraph paragraph = new Paragraph();
paragraph.AddText(textRun);

// Create a new Word document
WordDocument doc = new WordDocument(paragraph);

// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models

' Create textrun
Private textRun As New Text("Sample text")

Private paragraph As New Paragraph()
paragraph.AddText(textRun)

' Create a new Word document
Dim doc As New WordDocument(paragraph)

' Export docx
doc.SaveAs("document.docx")
VB   C#

添加图片

图像不能单独添加,而应添加到文档结构中,如段落表格单元格章节。使用 AddImage 方法添加图像。

:path=/static-assets/word/content-code-examples/get-started/get-started-2.cs
using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument("document.docx");

// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("image.jpg");
image.Width = 250; // In unit pixel
image.Height = 200; // In unit pixel
Paragraph paragraph = new Paragraph();

// Add image
paragraph.AddImage(image);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models

' Load docx
Private doc As New WordDocument("document.docx")

' Configure image
Private image As New IronWord.Models.Image("image.jpg")
image.Width = 250 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim paragraph As New Paragraph()

' Add image
paragraph.AddImage(image)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("save_document.docx")
VB   C#

添加表格

添加表格需要更多的工作,因为必须创建表格、行、列和表格单元格。不过,有了这种设置,就有了大量的配置机会。每个单元格都可以有不同的样式。探索各种边框样式,有 24 种类型可供选择。

:path=/static-assets/word/content-code-examples/get-started/get-started-3.cs
using IronWord;
using IronWord.Models;

// Create table cell
TableCell cell = new TableCell();

Text textRun = new Text();
textRun.Text = "Sample text";

// Add textrun to the cell
cell.AddChild(new Paragraph(textRun));

// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = Color.Black;
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;

// Configure table border
TableBorders tableBorders = new TableBorders() {
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};

cell.Borders = tableBorders;

// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);

// Create table and add row
Table table = new Table();
table.AddRow(row);

// Create new Word document from the table
WordDocument doc = new WordDocument(table);

// Export Word document
doc.SaveAs("Document.docx");
Imports IronWord
Imports IronWord.Models

' Create table cell
Private cell As New TableCell()

Private textRun As New Text()
textRun.Text = "Sample text"

' Add textrun to the cell
cell.AddChild(New Paragraph(textRun))

' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = Color.Black
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick
borderStyle.BorderSize = 5

' Configure table border
Dim tableBorders As New TableBorders() With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}

cell.Borders = tableBorders

' Create row and add cell
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)

' Create table and add row
Dim table As New Table()
table.AddRow(row)

' Create new Word document from the table
Dim doc As New WordDocument(table)

' Export Word document
doc.SaveAs("Document.docx")
VB   C#

提供许可证和支持

IronWord是一个付费库,但也提供免费试用许可 这里.

有关 Iron Software 的更多信息,请访问我们的网站:https://ironsoftware.com/ 如需更多支持和咨询,请联系 询问我们的团队.

来自 Iron Software 的支持

如需一般支持和技术咨询,请发送电子邮件至support@ironsoftware.com