如何在 C# | IronWord 中為文字新增文字輪廓效果

如何在 C# 中加入文字輪廓效果

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

文字輪廓效果會在文字字元周圍添加可見的邊框,從而創建清晰的輪廓,增強可讀性或視覺衝擊力。 這種效果可以根據設計偏好,在顏色、粗細和樣式方面進行客製化。 它常用於圖形、排版和數位設計中,使文字在背景中脫穎而出或創造風格化的外觀。

快速入門:立即套用預設文字輪廓效果

只需一行程式碼,即可建立一個TextStyle ,透過TextOutlineEffect.DefaultEffect套用現成的文字輪廓。 這是一種讓開發者能夠快速使用 IronWord 來突出文本,而無需進行複雜設定的方法。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronWord

    PM > Install-Package IronWord

  2. 複製並運行這段程式碼。

    new IronWord.Models.TextStyle { TextEffect = new IronWord.Models.TextEffect { TextOutlineEffect = IronWord.Models.TextOutlineEffect.DefaultEffect } }
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronWord,免費試用!
    arrow pointer

新增文字輪廓效果

若要指定文字的輪廓效果,請建立TextStyle對象,並使用TextOutlineEffect物件填入 TextOutlineEffect 屬性。 最後,透過將TextStyle物件指派給 TextEffect 屬性來新增具有該樣式的新文字。

: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
新增文字輪廓效果

文字輪廓效果屬性

文字輪廓效果提供多種可自訂屬性,以滿足任何設計需求。 以下是各項屬性及其描述:

  • PenAlignment :取得或設定用於輪廓效果的筆的對齊方式。
  • LineCapType :取得或設定用於輪廓效果的線帽類型。
  • LineWidth :取得或設定輪廓線的寬度。注意:寬度以磅(1/72 英吋)為單位指定。
  • CompoundLineType :取得或設定用於輪廓效果的複合線類型。
  • LineJoin :取得或設定用於輪廓效果的描邊連接樣式。 -顏色:取得或設定輪廓效果的純色填滿色彩。
  • PresetLineDash :取得或設定輪廓效果的預設虛線樣式。
: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
自訂文字輪廓效果

常見問題解答

什麼是 Word 文件中的文字大綱效果?

Word 文件中的文字輪廓效果會在文字字元周圍添加可見的邊框,以增強其可讀性和視覺效果。此效果可自訂顏色、粗細和樣式,以更符合設計喜好。

如何在 C# 中套用文字輪廓效果?

在 C# 中,您可以使用 IronWord 中的 TextOutlineEffect 類應用文字輪廓效果。您可以設定其顏色、線寬和樣式等屬性,以自訂文字的效果。

使用 IronWord 新增文字大綱效果的步驟為何?

步驟包括下載 IronWord 函式庫、將文字效果套用至新文字或現有文字、配置 TextOutlineEffect 屬性,以及將編輯後的文件匯出為新檔案。

我可以在 C# 中自訂文字大綱效果的屬性嗎?

是的,您可以使用 IronWord 透過調整 PenAlignment, LineCapType, LineWidth, CompoundLineType, LineJoin, Color, 和 PresetLineDash 等屬性來自訂文字輪廓效果。

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

文字輪廓效果的線寬以點為單位,一點相當於一英吋的 1/72。

套用文字大綱效果後,如何匯出 Word 文件?

在 IronWord 中套用文字大綱效果之後,您可以使用函式庫的匯出功能匯出已編輯的 Word 文件,將您的變更儲存為新檔案。

在設計中使用文字輪廓效果的目的是什麼?

文字輪廓效果可用於使文字在背景中顯得突出,或創造出風格化的外觀,增強數位設計中的可讀性和視覺效果。

如何在 C# 中定義帶有文字輪廓效果的文字樣式?

您可以透過建立 TextStyle 物件,並使用 TextOutlineEffect 物件填充其 TextOutlineEffect 屬性,以定義文字樣式。透過 TextEffect 物件將此樣式指定給文字。

Curtis Chau
技術作家

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

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

準備好開始了嗎?
Nuget 下載 27,129 | Version: 2025.11 剛發表