IronWord入门指南
IronWord:适用于 .NET 的 Word 文档库
IronWord是由Iron Software开发的Word文档库。 IronWord 在为 .NET 应用程序中处理 Word 文档提供强大的功能方面表现出色。
- 加载、编辑和保存 Word 和 Docx 文档。
PageSetup:配置纸张尺寸、页面方向、页边距和背景颜色。TextRun:处理文本内容、样式、拆分、追加文本和添加图像。TextStyle:管理字体系列、大小、颜色、粗体、斜体、删除线、下划线、上标和下标。Paragraph:添加文本行、图像、形状、设置样式、对齐方式、项目符号和编号列表。Table:操作表格结构,包括添加行、获取和设置单元格值、删除行、合并单元格等。Image:从文件或流加载图像,设置换行文本、位置偏移、宽度、高度和其他属性。Shape:设置文本环绕、位置偏移、宽度、高度、形状类型和旋转。
适用于 .NET 的 Word 文档 C# 库
- 下载用于处理 DOCX 文档的 C# 库
- 创建和修改 Word 和 DOCX 文档
- 添加文档结构,例如段落、章节和表格
- 添加文档元素,例如文本行、图像和形状
- 轻松设置文档元素样式
安装
IronWord图书馆
安装 IronWord 既快捷又简单。 您可以使用 NuGet 通过以下命令安装该软件包:
Install-Package IronWord
或者,直接从IronWord NuGet 官方网站下载。
安装完成后,您可以通过在 C# 代码文件的顶部添加using IronWord;来开始使用。
应用许可证密钥
接下来,通过将许可证密钥分配给License类的LicenseKey属性,将有效的许可证密钥或试用密钥应用于 IronWord。 在导入语句之后、使用任何 IronWord 方法之前,添加以下代码:
using IronWord;
// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";using IronWord;
// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";Imports IronWord
' Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE"代码示例
让我们来看一些代码示例和可用功能。
- 选择"文件">"信息",然后点击"转换"。
- 系统会提示您,您的文档将被升级为最新的文件格式。 点击"确定"。
创建 Word 和 Docx 文档
使用WordDocument类的构造函数之一实例化该类,即可创建 Word 文档。 之后,使用SaveAs方法导出 Word 文档。 例:
using IronWord;
class Program
{
static void Main()
{
// Create a new Word document
var document = new WordDocument();
// Save the document as a .docx file
document.SaveAs("example.docx");
}
}using IronWord;
class Program
{
static void Main()
{
// Create a new Word document
var document = new WordDocument();
// Save the document as a .docx file
document.SaveAs("example.docx");
}
}Imports IronWord
Friend Class Program
Shared Sub Main()
' Create a new Word document
Dim document = New WordDocument()
' Save the document as a .docx file
document.SaveAs("example.docx")
End Sub
End Class添加图片
图片不能单独添加; 相反,应该将其添加到文档结构中,例如Paragraph 、 TableCell或Section 。 使用AddImage方法添加图像。 例:
using IronWord;
using System.Drawing;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
// Add an image to a paragraph
var paragraph = section.Paragraphs.Add();
paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));
document.SaveAs("example_with_image.docx");
}
}using IronWord;
using System.Drawing;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
// Add an image to a paragraph
var paragraph = section.Paragraphs.Add();
paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));
document.SaveAs("example_with_image.docx");
}
}Imports IronWord
Imports System.Drawing
Friend Class Program
Shared Sub Main()
Dim document = New WordDocument()
Dim section = document.Sections.Add()
' Add an image to a paragraph
Dim paragraph = section.Paragraphs.Add()
paragraph.AddImage("path/to/image.jpg", New Rectangle(0, 0, 100, 100))
document.SaveAs("example_with_image.docx")
End Sub
End Class添加表格
添加表格需要创建表格、行、列和表格单元格。 这提供了很大的配置可能性,因为每个单元格都可以有不同的样式。 例:
using IronWord;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
var table = section.Tables.Add(3, 3); // 3x3 table
// Iterate over cells and set their content
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count; j++)
{
table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
}
}
document.SaveAs("example_with_table.docx");
}
}using IronWord;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
var table = section.Tables.Add(3, 3); // 3x3 table
// Iterate over cells and set their content
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count; j++)
{
table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
}
}
document.SaveAs("example_with_table.docx");
}
}Imports IronWord
Friend Class Program
Shared Sub Main()
Dim document = New WordDocument()
Dim section = document.Sections.Add()
Dim table = section.Tables.Add(3, 3) ' 3x3 table
' Iterate over cells and set their content
For i As Integer = 0 To table.Rows.Count - 1
Dim j As Integer = 0
Do While j < table.Rows(i).Cells.Count
table.Rows(i).Cells(j).Paragraphs.Add().AppendText($"Cell {i+1},{j+1}")
j += 1
Loop
Next i
document.SaveAs("example_with_table.docx")
End Sub
End Class可用的许可和支持
IronWord是一个付费库; 不过,您可以在这里获得免费试用许可证。
有关 Iron Software 的更多信息,请访问我们的网站:https://ironsoftware.com/ 。 如需更多帮助或有任何疑问,请联系我们的团队。
Iron Software 提供的支持
如需一般支持和技术咨询,请发送电子邮件至:support@ironsoftware.com






