IRONSOFTWAREHOME
添加样式文本
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 = IronWord.Models.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 = IronWord.Models.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")
NuGet Download
PM > Install-Package IronWord
添加样式文本

添加样式文本

IronWord 中的"添加样式文本"功能允许开发人员在向 DOCX 文档添加内容时应用各种文本样式选项。 它提供对文本外观的细粒度控制,例如指定字体系列、大小、颜色和样式属性,如strike-through。 通过创建包含TextContent对象,开发人员可以定制文档中各个文本元素的呈现,确保个性化的外观和感觉。

此功能可用于动态生成专业格式的文档,例如报告或信函,其中特定部分需要不同的样式。 TextStyle类使得这些属性的操作变得简单,允许在同一个文档中进行简单和复杂的样式设置。

关键点

1.创建样式化跑步

  • 创建了一包含所需文字的Run对象。
  • TextStyle对象以应用格式。 FontSize

2.配置文本样式

  • Font内)以指定文本大小。
  • fontFamilyFontFamily
  • color指定文本颜色。 IronWord.Models.Color
  • italic:用于加粗和斜体格式的布尔属性。 IsBold
  • underline:为文本添加下划线样式。 IsItalic
  • StrikeValue枚举应用删除线格式。 Underline
  • Strike

3.添加到文档

  • 使用Paragraph中。
  • 然后使用AddParagraph将段落添加到文档中。

代码解释

此代码演示了如何使用 IronWord 在 DOCX 文档中创建和设置文本样式。 它首先初始化一个新的WordDocument对象,代表要生成的文档。 创建一个包含字符串"样式文本示例"的Run以配置文本的外观。

bold格式。 这些设置可以自定义文本在最终文档中的显示方式。

Run对象添加到文档中的段落中。 此方法会将样式化的内容以适当的格式插入到 Word 文档中。 最后调用SaveAs方法将文档导出为"styled_document.docx"。 最终生成的 Word 文档中,插入的文本将按照指定的样式进行格式化,并在输出文件中保留所有字体和格式属性。

浏览文档元素教程,获取更多样式技巧
在 GitHub 上查看

准备开始了吗?

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

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 天试用密钥
无需信用卡或创建账户