如何在 C# 中为文本添加轮廓效果 | IronWord

How to Add Text Outline Effect to Text

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

A text outline effect adds a visible border around the characters of text, creating a defined outline that enhances readability or visual impact. This effect can be customized in terms of color, thickness, and style to suit design preferences. It's commonly used in graphics, typography, and digital design to make text stand out against backgrounds or to create a stylized appearance.

Quickstart: Apply a Default Text Outline Effect Instantly

With just one line, create a TextStyle that applies a ready-made text outline via TextOutlineEffect.DefaultEffect. It’s a fast way to let developers use IronWord to make text stand out without complex setup.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. Copy and run this code snippet.

    new IronWord.Models.TextStyle { TextEffect = new IronWord.Models.TextEffect { TextOutlineEffect = IronWord.Models.TextOutlineEffect.DefaultEffect } }
  3. Deploy to test on your live environment

    Start using IronWord in your project today with a free trial
    arrow pointer

Add Text Outline Effect

To specify the text outline effect for the text, create the TextStyle object and populate the TextOutlineEffect property with a TextOutlineEffect object. Finally, add new text with the style by assigning the TextStyle object to the TextEffect property.

:path=/static-assets/word/content-code-examples/how-to/text-effect-text-outline-effect.cs
using IronWord;
using IronWord.Models;

// Create new Word document
WordDocument doc = new WordDocument();

// Create and configure text style
TextStyle textStyle = new TextStyle();
textStyle.TextEffect = new TextEffect()
{
    TextOutlineEffect = TextOutlineEffect.DefaultEffect,
};

// Add text with style
doc.AddText("Hello World").Style = textStyle;

// Export new Word document
doc.SaveAs("textOutlineEffect.docx");
Imports IronWord
Imports IronWord.Models

' Create new Word document
Private doc As New WordDocument()

' Create and configure text style
Private textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {.TextOutlineEffect = TextOutlineEffect.DefaultEffect}

' Add text with style
doc.AddText("Hello World").Style = textStyle

' Export new Word document
doc.SaveAs("textOutlineEffect.docx")
$vbLabelText   $csharpLabel
Add text outline effect

Text Outline Effect Properties

The text outline effect offers a variety of customizable properties to suit any design need. Below are the properties along with their descriptions:

  • PenAlignment: Gets or sets the alignment of the pen used for the outline effect.
  • LineCapType: Gets or sets the type of line cap used for the outline effect.
  • LineWidth: Gets or sets the width of the outline effect line. Note: The width is specified in points (1/72 inch).
  • CompoundLineType: Gets or sets the type of compound line used for the outline effect.
  • LineJoin: Gets or sets the stroke join style used for the outline effect.
  • Color: Gets or sets the solid fill color for the outline effect.
  • PresetLineDash: Gets or sets the preset line dash style for the outline effect.
:path=/static-assets/word/content-code-examples/how-to/text-effect-customized-text-outline-effect.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Create new Word document
WordDocument doc = new WordDocument();

// Create and configure text style
TextStyle textStyle = new TextStyle();
textStyle.TextEffect = new TextEffect()
{
    TextOutlineEffect = new TextOutlineEffect()
    {
        Color = IronWord.Models.Color.Red,
        CompoundLineType = CompoundLineValues.Double,
        LineCapType = LineCapValues.Round,
        LineJoin = StrokeJoinStyleValues.Bevel,
        LineWidth = 0.3,
        PenAlignment = PenAlignmentValues.Center,
        presetLineDash = PresetLineDashValues.Solid
    },
};

// Add text with style
doc.AddText("Customized text outline").Style = textStyle;

// Export new Word document
doc.SaveAs("customizedTextOutlineEffect.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Create new Word document
Private doc As New WordDocument()

' Create and configure text style
Private textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {
	.TextOutlineEffect = New TextOutlineEffect() With {
		.Color = IronWord.Models.Color.Red,
		.CompoundLineType = CompoundLineValues.Double,
		.LineCapType = LineCapValues.Round,
		.LineJoin = StrokeJoinStyleValues.Bevel,
		.LineWidth = 0.3,
		.PenAlignment = PenAlignmentValues.Center,
		.presetLineDash = PresetLineDashValues.Solid
	}
}

' Add text with style
doc.AddText("Customized text outline").Style = textStyle

' Export new Word document
doc.SaveAs("customizedTextOutlineEffect.docx")
$vbLabelText   $csharpLabel
Customized text outline effect

常见问题解答

什么是 Word 文档中的文本轮廓效果?

Word 文档中的文本轮廓效果在文本字符周围添加可见的边框,增强其可读性和视觉冲击力。此效果在颜色、厚度和样式方面可定制,以更好地适应设计偏好。

我如何在 C# 中应用文本轮廓效果?

在 C# 中,你可以使用 IronWord 的 TextOutlineEffect 类应用文本轮廓效果。你可以通过配置其属性如颜色、线宽和样式来定制文本效果。

使用 IronWord 添加文本轮廓效果的步骤是什么?

步骤包括下载 IronWord 库,将文本效果应用于新文本或现有文本,配置 TextOutlineEffect 属性,并将编辑后的文档导出为新文件。

我可以在 C# 中自定义文本轮廓效果的属性吗?

是的,你可以通过调整 PenAlignmentLineCapTypeLineWidthCompoundLineTypeLineJoinColorPresetLineDash 等属性来自定义文本轮廓效果,使用 IronWord。

文本轮廓效果的线宽测量单位是什么?

文本轮廓效果的线宽以点为单位测量,其中一个点等于英寸的 1/72。

应用文本轮廓效果后,我如何导出 Word 文档?

在 IronWord 中应用文本轮廓效果后,你可以使用库的导出功能将编辑的 Word 文档保存为新文件。

在设计中使用文本轮廓效果的目的是什么?

文本轮廓效果用于使文本在背景上突出或创建风格化外观,增强数字设计中的可读性和视觉冲击力。

我如何在 C# 中定义具有文本轮廓效果的文本样式?

你可以通过创建一个 TextStyle 对象并填充其 TextOutlineEffect 属性来定义文本样式,将该样式分配给你的文本通过 TextEffect 属性。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 25,807 | 版本: 2025.11 刚刚发布