如何在 C# 中处理文档元素

C# Document Element Tutorial

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

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

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

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

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronWord

    PM > Install-Package IronWord
  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. 部署到您的生产环境中进行测试

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

    arrow pointer

目录

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

关键概念

文档层级结构

IronWord遵循结构化的文档层次结构:文档→段落→运行→文本内容。 理解这种层级结构对于处理样式文本至关重要:

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

向段落添加内容

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

  • AddText(TextContent): 用于无样式纯文本。 直接在段落中添加 TextContent
  • AddChild(Run): 用于样式文本。 向段落添加一个 Run 对象(该对象包裹 TextContent 并包含样式)。

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

添加文本运行

文本内容

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

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.cs
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");
$vbLabelText   $csharpLabel

设置样式

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

请注意,FontSize 是在 TextStyle 级别配置的,而 FontFamily 是在 TextFont 属性内设置的。 设置好样式后,使用 AddChildRun 添加到段落中。 这遵循文档层次结构:文档 → 段落 → 运行 → 文本内容。

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.cs
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");
$vbLabelText   $csharpLabel

获取文本填充颜色

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

:path=/static-assets/word/content-code-examples/tutorials/rgba-color-value.cs
using IronWord;
using IronWord.Models;
using System;

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

TextContent content = doc.Paragraphs[0].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);
$vbLabelText   $csharpLabel

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

嵌入图片

此功能可让您在内容中无缝插入图像,从而增强文档的整体视觉吸引力和沟通效果。

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-embed-images.cs
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(new Paragraph(textRun));

// Export docx
doc.SaveAs("save_document.docx");
$vbLabelText   $csharpLabel

添加图片

加载图像

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

:path=/static-assets/word/content-code-examples/tutorials/add-image-load-image.cs
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");
$vbLabelText   $csharpLabel

配置映像

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

:path=/static-assets/word/content-code-examples/tutorials/add-image-configure-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
using IronSoftware.Abstractions.Word;

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

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.TextWrapBehavior = new TextWrapSquare();
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");
$vbLabelText   $csharpLabel

常见问题解答

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 35,581 | 版本: 2026.3 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronWord
运行示例 观看您的数据变成 Word 文档。