C# 文档元素教程

Curtis Chau
Curtis Chau
更新: 2026年8月2日

IronWord 是一个功能强大的 Word 文档库,旨在帮助 .NET C# 开发人员将创建、读取和编辑 Word 和 DOCX 文档的功能集成到他们的应用程序中。 在 Word 文档中,文档元素是构成内容的基本组成部分。

快速入门:创建样式文本并嵌入图像

以下是如何使用IronWord向文档中添加丰富内容 - 将样式化文本与嵌入图像结合起来。 此示例演示了用于样式化文本的Run包装模式。

  1. 1使用 IronWord 安装 NuGet Package Manager

    PM > Install-Package IronWord

  2. 2复制并运行这段代码。

    using IronWord;
    using IronWord.Models;
    
    // Create document
    WordDocument doc = new WordDocument();
    
    // Create styled Run
    Run textRun = new Run(new TextContent("Hello IronWord!"));
    textRun.Style = new TextStyle() { IsBold = true, Color = Color.Blue };
    
    // Create paragraph and add Run
    Paragraph paragraph = new Paragraph();
    paragraph.AddChild(textRun);
    paragraph.AddImage(new ImageContent("pic.png"));
    
    // Add to document and save
    doc.AddParagraph(paragraph);
    doc.SaveAs("output.docx");
  3. 3部署到您的生产环境中进行测试

    今天就用免费试用在您的项目中开始使用IronWord
    arrow pointer

目录

关键概念

文档层级结构

IronWord遵循结构化文档层次:Document → Paragraph → Run → TextContent。 理解这种层级结构对于处理样式文本至关重要:

  • WordDocument:表示整个文档的顶级容器
  • Paragraph:文档中的一块内容
  • Run:一个封装了 TextContent 的包装对象,可以应用样式
  • TextContent:实际的文本字符串

向段落添加内容

IronWord提供了两种向段落添加内容的方法:

  • AddText(TextContent):用于未加样式的纯文本。 将TextContent直接添加到段落中。
  • AddChild(Run):用于样式化文本。 将一个 Run 对象(封装了 TextContent 并持有样式)添加到段落中。

当您需要应用样式如字体大小、颜色或粗体格式时,创建一个 Run 对象,将 TextStyle 赋值给 Run,并使用 AddChild 将其添加到段落中。

添加文本运行

文本内容

Split方法用于根据指定的分隔符将文本运行分成一个小文本运行列表。 这样就可以对文档中的文本信息进行组织和操作。

using IronWord;
using IronWord.Models;

WordDocument doc = new WordDocument();

// Add text
TextContent addText = new TextContent("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));

// Append text
TextContent appendText = new TextContent("The first text.");
appendText.Append(new TextContent("The second text."));
doc.AddParagraph(new Paragraph(appendText));

// Split text
TextContent splitText = new TextContent("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));

// Export docx
doc.SaveAs("textrun.docx");

设置样式

设置文本样式需要使用Run包装模式。 创建一个包含 TextContent 的 Run 对象,然后将 TextStyle 分配给 Run(而不是 TextContent)。 TextStyle 允许您定义视觉属性,如 FontSize、颜色、粗体、斜体、删除线、下划线、上标和下标。

请注意,FontSize 在 TextStyle 级别配置,而 FontFamily 设置在 TextFont 属性内部。 一旦设置样式后,使用 AddChild 将 Run 添加到段落中。 这遵循文档层次:Document → Paragraph → Run → TextContent。

using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

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

// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
    FontSize = 72,
    TextFont = new Font()
    {
        FontFamily = "Caveat"
    },
    Color = Color.Red,
    IsBold = true,
    IsItalic = true,
    Underline = new Underline(),
    Strike = StrikeValue.Strike,
};

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddChild(textRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");

获取文本填充颜色

除了设置样式外,IronWord还提供了从现有文档中获取RGBA颜色值的方法,以保持样式的一致性。

using IronWord;
using IronWord.Models;
using System;

// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");

TextContent content = doc.Texts[0];

// This will show the R G B A of the themecolor
var filledColor = content.Color;

// Print the filled color variable to the console
Console.WriteLine(filledColor);

要检索颜色值,请使用 WordDocument 加载文档,然后访问 Texts 集合以获取 TextContent 对象。 TextContent 的 Color 属性返回文本现有颜色的 RGBA 值,允许您在整个文档中保持一致的样式。

嵌入图片

此功能允许您在内容中加入图像,增强整个文档的视觉吸引力和交流能力。

using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument();

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
TextContent textRun = new TextContent();

// Add image
Paragraph para = new Paragraph(textRun);
para.AddImage(image);

// Add paragraph
doc.AddParagraph(para);

// Export docx
doc.SaveAs("save_document.docx");

添加图片

加载图像

图片加载是一个至关重要的过程。 这涉及到将外部图像文件导入到文档中。 加载图像的功能有助于添加相关的视觉元素,从而使文档更具吸引力和信息量。

using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument();

Paragraph paragraph = new Paragraph();

// Add image
paragraph.AddImage("image.jpg");

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("document.docx");

配置映像

使用可配置设置优化图像。 这包括设置诸如尺寸、位置和与角距等属性。 正确的配置可确保图像以赏心悦目且符合上下文的方式显示。

请注意: 图像还公开了一个 TextWrapBehavior 属性(类型 ITextWrapBehavior)用于控制图像周围的文本环绕,但目前没有记录公共实现类或工厂来构造值 - 本例因等待产品团队确认而省略。
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Load docx
WordDocument doc = new WordDocument();

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 100;
image.Height = 100;
image.DistanceFromTop = 50;

var position = new ElementPosition();
position.X = 50;
position.Y = 50;
image.Position = position;

Paragraph paragraph = new Paragraph();

// Add image
paragraph.AddImage(image);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("document.docx");

常见问题

IronWord在C#开发中有什么用?

IronWord是一个为.NET C#开发人员设计的强大库,旨在帮助在他们的应用程序中创建、读取和编辑Word和DOCX文档。

如何使用IronWord向Word文档添加样式文本?

您可以通过创建一个包装‘TextContent’的‘Run’对象来添加样式文本,然后应用一个‘TextStyle’到它,并使用IronWord中的‘AddChild’方法将其添加到段落中。

‘Run’对象在IronWord中的作用是什么?

IronWord中的‘Run’对象充当‘TextContent’的包装器,允许为文本设置样式,例如设置字体大小、颜色和其他属性。

如何使用IronWord将图像嵌入到文档中?

为了嵌入图像,创建一个‘ImageContent’对象并使用‘AddImage’方法将其添加到段落中。这使您可以将图像插入文档中。

在IronWord中配置图像涉及哪些步骤?

配置图像涉及设置属性,如尺寸、位置和距离拐角的距离,以确保图像在文档中正确显示。

IronWord可以从现有的Word文档中检索颜色值吗?

可以,IronWord可以通过‘TextContent’的‘Color’属性从现有文档中检索RGBA颜色值,帮助保持样式一致性。

IronWord使用哪种方法来拆分文本运行?

IronWord使用‘Split’方法根据指定的分隔符将文本运行分为较小的运行,以实现更好的文本内容组织和操作。

为什么在IronWord中理解文档层次结构很重要?

理解文档层次结构(Document → Paragraph → Run → TextContent)对于有效管理和样式化使用IronWord的Word文档内容至关重要。

如何保存使用IronWord创建的Word文档?

使用IronWord创建的Word文档可以通过‘SaveAs’方法保存,指定保存该文档的文件名和路径。

‘AddParagraph’方法在IronWord中的功能是什么?

IronWord中的‘AddParagraph’方法用于向Word文档添加完全配置的段落,包含文本和图像。

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

阅读更多

准备开始了吗?

NuGet 下载量 63,050版本:2026.11刚刚发布