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 文档。
  • PageSetup :配置纸张尺寸、页面方向、页边距和背景颜色。
  • TextRun :处理文本内容、样式、拆分、追加文本和添加图像。
  • TextStyle :管理字体系列、大小、颜色、粗体、斜体、删除线、下划线、上标和下标。
  • Paragraph :添加文本行、图像、形状、设置样式、对齐方式、项目符号和编号列表。
  • Table :操作表格结构,包括添加行、获取和设置单元格值、删除行、合并单元格等。
  • Image :从文件或流加载图像,设置换行文本、位置偏移、宽度、高度和其他属性。
  • Shape :设置文本环绕、位置偏移、宽度、高度、形状类型和旋转。

安装

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

代码示例

让我们来看一些代码示例和可用功能。

请注意IronWord 生成的 DOCX 文件在特定版本的 Microsoft Word 中打开时,可能处于兼容模式,导致某些样式不可用。 要将 Word 文档从兼容模式转换出去

  1. 选择"文件">"信息",然后点击"转换"。
  2. 系统会提示您,您的文档将被升级为最新的文件格式。 点击"确定"。

创建 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
$vbLabelText   $csharpLabel

添加图片

图片不能单独添加; 相反,应该将其添加到文档结构中,例如ParagraphTableCellSection 。 使用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
$vbLabelText   $csharpLabel

添加表格

添加表格需要创建表格、行、列和表格单元格。 这提供了很大的配置可能性,因为每个单元格都可以有不同的样式。 例:

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

可用的许可和支持

IronWord是一个付费库; 不过,您可以在这里获得免费试用许可证。

有关 Iron Software 的更多信息,请访问我们的网站:https://ironsoftware.com/ 。 如需更多帮助或有任何疑问,请联系我们的团队

Iron Software 提供的支持

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

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 27,129 | Version: 2025.11 刚刚发布