C# Document Element Tutorial
IronWord 是一个功能强大的 Word 文档库,旨在帮助 .NET C# 开发人员将创建、读取和编辑 Word 和 DOCX 文档的功能集成到他们的应用程序中。 在 Word 文档中,文档元素是构成内容的基本组成部分。
快速入门:创建样式文本并嵌入图像
以下是如何使用IronWord添加富文本——将样式化的文本和嵌入式图像组合到文档中。 此示例演示了用于样式化文本的 Run 包装器模式。
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronWord
PM > Install-Package IronWord -
复制并运行这段代码。
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"); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronWord
目录
-添加文本 -文本内容(添加、追加和拆分) -设置样式(字体和字号、颜色、粗体和斜体、删除线、下划线、上标和下标) -嵌入图片 -添加图片 -加载图像(文件和文件流) -设置自动换行 -设置尺寸(宽度和高度) -设置位置偏移 -设置与角落的距离
关键概念
文档层级结构
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");
Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
' Add text
Private addText As New TextContent("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))
' Append text
Dim appendText As New TextContent("The first text.")
appendText.Append(New TextContent("The second text."))
doc.AddParagraph(New Paragraph(appendText))
' Split text
Dim splitText As New TextContent("Use split to split the sentence.")
splitText.Split(" ")
doc.AddParagraph(New Paragraph(splitText))
' Export docx
doc.SaveAs("textrun.docx")
设置样式
设置文本样式时需使用 Run 包裹模式。 创建一个包含 Run 的 TextContent 对象,然后将 TextStyle 赋值给 Run(而非 TextContent)。 TextStyle 允许您定义视觉属性,例如 FontSize、颜色、加粗、斜体、删除线、下划线、上标和下标。
请注意,FontSize 在 TextStyle 级别进行配置,而 FontFamily 则设置在 TextFont 属性中。 格式化完成后,请使用 AddChild 将 Run 添加到段落中。 这遵循文档层次结构:文档 → 段落 → 运行 → 文本内容。
: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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
.FontSize = 72,
.TextFont = New Font() With {
.FontFamily = "Caveat"
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddChild(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")
获取文本填充颜色
除了设置样式外, 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);
Imports IronWord
Imports IronWord.Models
Imports System
' Open existing Word
Dim doc As New WordDocument("Accent1TextThemcolor.docx")
Dim content As TextContent = doc.Paragraphs(0).Texts(0)
' This will show the R G B A of the themecolor
Dim filledColor = content.Color
' Print the filled color variable to the console
Console.WriteLine(filledColor)
要获取颜色值,请使用 WordDocument 加载文档,然后访问 Paragraphs 集合和 Texts 数组以获取 TextContent 对象。 Color 属性返回文本当前颜色的 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");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
' Configure image
Private image As New ImageContent("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New TextContent()
' Add image
Dim para As New Paragraph(textRun)
para.AddImage(image)
' Add paragraph
doc.AddParagraph(New Paragraph(textRun))
' Export docx
doc.SaveAs("save_document.docx")
添加图片
加载图像
图片加载是一个至关重要的过程。 这涉及到将外部图像文件导入到文档中。 加载图像的功能有助于添加相关的视觉元素,从而使文档更具吸引力和信息量。
: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");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
Private paragraph As New Paragraph()
' Add image
paragraph.AddImage("image.jpg")
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")
配置映像
使用可配置设置优化图像。 这包括设置文本环绕、尺寸、位置和距角点的距离等属性。 正确的配置可确保图像以赏心悦目且符合上下文的方式显示。
: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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Imports IronSoftware.Abstractions.Word
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.TextWrapBehavior = New TextWrapSquare()
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position As New ElementPosition()
position.X = 50
position.Y = 50
image.Position = position
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")
常见问题解答
我如何使用 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 文档,利用其完整的库功能。

