文档元素教程

查克尼特·宾
查克尼特·宾
2024年一月9日
更新 2025年一月9日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

IronWord是一个功能强大的Word文档库,旨在帮助.NET C#开发者将创建、阅读和编辑Word及DOCX文档的能力整合到他们的应用程序中。 在Word文档的上下文中,文档元素是构成内容的基本构件。

目录


添加文本运行

文本内容

Split方法用于根据指定的分隔符将文本运行分割成较小的TextRuns列表。 这允许在文档中组织和操纵文本信息。

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

设置样式

设置TextRuns的样式可以让您定义文本的视觉呈现。 这包括指定字体大小、颜色、样式、删除线、下划线、上标和下标等属性。 配置样式可以增强文档中文本的整体外观。

: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
TextContent textRun = new TextContent();
textRun.Text = "Add text using IronWord";
textRun.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Caveat",
        FontSize = 72,
    },
    Color = Color.Red,
    IsBold = true,
    IsItalic = true,
    Underline = new Underline(),
    Strike = StrikeValue.Strike,
};

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddText(textRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Load docx
Private doc As New WordDocument("document.docx")

' Configure text
Private textRun As New TextContent()
textRun.Text = "Add text using IronWord"
textRun.Style = New TextStyle() With {
	.TextFont = New Font() With {
		.FontFamily = "Caveat",
		.FontSize = 72
	},
	.Color = Color.Red,
	.IsBold = True,
	.IsItalic = True,
	.Underline = New Underline(),
	.Strike = StrikeValue.Strike
}

Dim paragraph As New Paragraph()

' Add text
paragraph.AddText(textRun)

' Add paragraph
doc.AddParagraph(paragraph)

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

嵌入图像

此功能允许您无缝地在内容中包含图像,增强文档的整体视觉吸引力和沟通力。

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

配置图像

使用可配置的设置优化图像。 这包括设置文本环绕、尺寸、位置和与角落的距离等属性。 正确的配置确保图像以视觉上令人愉悦且语境恰当的方式显示。

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

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

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.WrapText = WrapText.Square;
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

' Load docx
Private doc As New WordDocument()

' Configure image
Private image As New ImageContent("image.jpg")
image.WrapText = WrapText.Square
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50

Dim position = 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")
$vbLabelText   $csharpLabel
查克尼特·宾
软件工程师
Chaknith 负责 IronXL 和 IronBarcode 的工作。他在 C# 和 .NET 方面拥有深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的洞察力,有助于提升产品、文档和整体体验。