IRONSOFTWAREHOME

C# Document Element Tutorial

Curtis Chau
Curtis Chau
Updated: 2026年5月9日

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

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

以下是如何使用IronWord添加富文本——将样式化的文本和嵌入式图像组合到文档中。 这个示例演示了Run包装器模式用于样式化文本。

  1. 1Install IronWord with 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");
    C#
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronWord
    arrow pointer

目录

-添加文本 -文本内容(添加、追加和拆分) -设置样式(字体和字号、颜色、粗体和斜体、删除线、下划线、上标和下标) -嵌入图片 -添加图片 -加载图像(文件和文件流) -设置自动换行 -设置尺寸(宽度和高度) -设置位置偏移 -设置与角落的距离

关键概念

文档层级结构

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

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

向段落添加内容

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

  • AddText(TextContent):用于无样式的纯文本。 直接将TextContent添加到段落中。
  • AddChild(Run):用于有样式的文本。 添加一个TextContent并保持样式)到段落中。

当您需要应用诸如字体大小、颜色或粗体格式等样式时,创建一个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)。 FontSize、颜色、粗体、斜体、删除线、下划线、上标和下标。

请注意,TextFont属性内。 一旦设置了样式,使用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);
C#

要检索颜色值,请使用TextContent对象。 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");
C#

添加图片

加载图像

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

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");

配置映像

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

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");
C#

常见问题解答

我如何使用 C# 向 Word 文档添加文本?

您可以使用 IronWord 向 Word 文档添加文本,创建一个文本段,然后将其附加到文档中的段落。

将文本拆分为 Word 文档的方法是什么?

IronWord 提供了 Split 方法,使您可以根据指定的分隔符将文本段分成更小的部分,从而更轻松地操作文本。

如何使用 IronWord 在 Word 文档中设置文本样式?

您可以通过设置字体大小、颜色及粗体、斜体、删除线、下划线、上标和下标样式等各种属性,使用 IronWord 为文本设置样式。

如何使用 C# 在 Word 文档中嵌入图像?

要在 Word 文档中嵌入图像,可以使用 IronWord 从文件中加载图像并将其作为内嵌图像添加到文档内的段落中。

将图像加载到 Word 文档中的步骤是什么?

using IronWord,您可以从文件或文件流中加载图像,从而将视觉内容纳入 Word 文档中。

如何配置 Word 文档中的图像属性?

using IronWord,您可以配置图像属性,如文本环绕、尺寸、位置偏移以及与角落的距离,以确保图像在文档中正确显示。

我可以检索 Word 文档中文本的 RGBA 颜色值吗?

是的,IronWord 允许您获取文档中现有文本的 RGBA 颜色值,以保持一致的样式。

如何开始使用 IronWord 操作 Word 文档?

要开始使用 IronWord,您可以将其集成到您的 .NET C# 应用程序中,以创建、读取和编辑 Word 文档,利用其完整的库功能。

How do you save a Word document created with IronWord?

A Word document created with IronWord can be saved using the 'SaveAs' method, specifying the filename and path for saving the document.

What is the function of the 'AddParagraph' method in IronWord?

The 'AddParagraph' method in IronWord is used to add fully configured paragraphs, with text and images, to a Word document.

Curtis Chau
技术作家

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

...
阅读更多

准备开始了吗?

Nuget Downloads 56,401版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

bullet_checked无需信用卡或创建账户
bullet_test在生产环境中测试
且无水印
bullet_calendar30天完全
功能性产品
bullet_support试用期间提供
24/5技术支持
立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronWord
nuget.org/packages/IronWord/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索“IronWord”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并解压IronWord到你的解决方案目录中的~/Libs等位置
  2. 在Visual Studio解决方案资源管理器中,右键单击引用。选择浏览,“IronWord.dll”

许可证价格从$999

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户