如何在 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# 中自定義文字輪廓效果的屬性嗎?

是的,您可以通過調整 IronWord 的屬性如 PenAlignmentLineCapTypeLineWidthCompoundLineTypeLineJoinColorPresetLineDash 自定義文字輪廓效果。

文字輪廓效果中的線寬的測量單位是什麼?

文字輪廓效果的線寬是用點數來測量的,其中一點相當於 1/72 英寸。

如何在應用文字輪廓效果後匯出 Word 文件?

在 IronWord 中應用文字輪廓效果後,您可以使用庫的匯出功能將編輯後的 Word 文件保存為新文件。

在設計中使用文字輪廓效果的目的何在?

文字輪廓效果是用來讓文本在背景上突出,或者創建一種風格化的外觀,增強數字設計中的可讀性和視覺效果。

如何在 C# 中定義具有文字輪廓效果的文體?

您可以通過創建一個 TextStyle 對象並用 TextOutlineEffect 對象填充其 TextOutlineEffect 屬性來定義文本樣式。通過 TextEffect 屬性將此樣式分配給文本。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 25,807 | 版本: 2025.11 剛剛發布