如何在 C# 中新增文字外框效果
在 C# 中為文字加上外框,可在字元周圍形成清楚的邊界,讓文字更醒目、更易讀。使用 IronWord 的 TextOutlineEffect 類別即可自訂外框的顏色、線寬和樣式。這種效果適合用在商務文件和報告中的標頭、浮水印或重要內容。
文字外框可提高文字與複雜背景的對比、建立視覺層次,也能讓簡報和報告看起來更專業。無論是產生發票、製作行銷資料或撰寫技術文件,文字外框都能讓內容更醒目、更易讀。
快速入門:立即套用預設文字外框效果只需一行程式碼,就能建立 TextStyle,透過 TextOutlineEffect.DefaultEffect 套用預設文字外框。這種做法無須繁複設定,適合快速製作原型,或需要立即強調文字而無須微調參數時使用。
-
1使用 IronWord 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
using IronWord; using IronWord.Models; WordDocument doc = new WordDocument(); TextStyle textStyle = new TextStyle(); textStyle.TextEffect = new TextEffect() { TextOutlineEffect = TextOutlineEffect.DefaultEffect }; Paragraph paragraph = new Paragraph(); Run textRun = new Run(new TextContent("Outlined Text")); textRun.Style = textStyle; paragraph.AddChild(textRun); doc.AddParagraph(paragraph); doc.SaveAs("outline.docx");Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.TextOutlineEffect = TextOutlineEffect.DefaultEffect} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Outlined Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("outline.docx")Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.TextOutlineEffect = TextOutlineEffect.DefaultEffect} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Outlined Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("outline.docx") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronWord,並使用 免費試用
如何新增文字外框效果(5 個步驟)
- 安裝 IronWord:
Install-Package IronWord - 建立
TextStyle並使用TextOutlineEffect.DefaultEffect設定TextOutlineEffect - 建立一個包含
TextContent的Run,並將TextStyle指派給該Run - 使用
AddChild將Run加入Paragraph - 儲存文件
如何在 Word 文件中新增文字外框效果?
為什麼要用 TextOutlineEffect 設定文字樣式?
要套用文字外框效果,請建立 TextStyle,使用 TextOutlineEffect 設定其 TextEffect 屬性。接著建立 Paragraph 和包含 TextContent 的 Run。將 TextStyle 指派給 Run(而非 TextContent),再使用 AddChild 將 Run 加入 Paragraph。文件的階層結構為 Document → Paragraph → Run → TextContent。
TextOutlineEffect 類別提供完整的 API,可控制文字外框的各項細節。相較於基本文字格式設定,您可以進一步調整線寬、顏色、畫筆對齊方式和虛線樣式。製作必須符合品牌規範或無障礙要求的專業文件時,這些設定尤其重要。
在不同紙張上列印商務文件,或在不同裝置上檢視時,文字外框可大幅提升可讀性。外框在每個字元周圍形成分界,讓文字在複雜背景或低對比的情況下依然清晰。
新增外框的基本程式碼範例是什麼?
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,
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Hello World"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("textOutlineEffect.docx");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,
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Hello World"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("textOutlineEffect.docx");Imports IronWord
Imports IronWord.Models
' Create new Word document
Dim doc As New WordDocument()
' Create and configure text style
Dim textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {
.TextOutlineEffect = TextOutlineEffect.DefaultEffect
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Hello World"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("textOutlineEffect.docx")Imports IronWord
Imports IronWord.Models
' Create new Word document
Dim doc As New WordDocument()
' Create and configure text style
Dim textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {
.TextOutlineEffect = TextOutlineEffect.DefaultEffect
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Hello World"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("textOutlineEffect.docx")
這個範例示範套用文字外框的基本流程:建立文件、定義含有預設外框效果的文字樣式、套用樣式,最後儲存文件。DefaultEffect 的外框線條較細,配色也能襯托文字,適合多數情境。
我該如何自訂文字外框效果屬性?
什麼屬性控制外框的外觀?
您可以調整下列屬性,使文字外框符合文件的視覺需求和品牌規範:
| 屬性 | 說明 |
|---|---|
PenAlignment | 取得或設定畫筆的對齊方式,決定外框線條是以字元邊緣為中心,還是沿著字元邊緣的內側對齊。 |
LineCapType | 取得或設定外框的線端樣式。平頭、圓頭和方頭會讓線段的端點呈現不同外觀。 |
LineWidth | 取得或設定外框的線寬,以點為單位(1 點等於 1/72 英吋)。常用值介於 0.1 到 2.0。 |
CompoundLineType | 取得或設定外框所用的複合線類型,可建立雙線、三線或其他多線條的外框。 |
LineJoin | 取得或設定外框線條在轉角處的接合樣式,可選擇斜接、圓接或斜角接合。 |
Color | 取得或設定外框的純色填滿色彩,可使用任何有效的色彩值。 |
presetLineDash | 取得或設定外框的預設線條樣式,可選擇實線、虛線、點線或自訂樣式。 |
各項屬性都能調整文字外框的視覺效果。例如,LineWidth 直接影響外框的醒目程度:細線(0.1 到 0.3 點)可低調強調文字,粗線(1.0 到 2.0 點)則能使文字更醒目。Color 有助於維持品牌配色一致,並提供足夠的對比以符合無障礙需求。
如何應用自訂外框屬性?
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
},
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Customized text outline"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("customizedTextOutlineEffect.docx");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
},
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Customized text outline"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("customizedTextOutlineEffect.docx");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Create new Word document
Dim doc As New WordDocument()
' Create and configure text style
Dim 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
}
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Customized text outline"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("customizedTextOutlineEffect.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Create new Word document
Dim doc As New WordDocument()
' Create and configure text style
Dim 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
}
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Customized text outline"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("customizedTextOutlineEffect.docx")
這個進階範例示範如何結合多項屬性,建立醒目的紅色雙線外框。圓形線端和斜角接合讓外框更平順,適合用在標頭和標題。
在正式環境的應用程式中使用自訂外框時,可建立能重複使用的樣式範本。為標頭、副標頭和強調文字等不同元素定義標準外框,再將這些值儲存為常數或組態設定。這樣可讓文件產生流程維持一致,品牌規範變更時也更容易維護。
為了達到最佳效果,請在不同的輸出格式和檢視條件下測試您的外框效果。 在螢幕上看起來不錯的內容,可能需要在列印輸出時進行調整。 在選擇外框寬度和顏色時,請考慮印表機解析度、紙張顏色和檢視距離等因素。 IronWord的TextOutlineEffect類別的靈活性使您能夠根據輸出要求以程式化方式微調這些參數。
常見問題
在C#中,文字輪廓效果的目的是什麼?
文字輪廓效果透過在字元周圍添加邊框來增強可讀性和視覺衝擊力。這對於創建標題、浮水印,以及強調文件中的重要內容非常有用。
如何使用IronWord應用預設文字輪廓效果?
您可以通過將 TextOutlineEffect.DefaultEffect 設定為 TextStyle 物件,並將其分配給文件結構中的 Run 來應用預設文字輪廓效果。
IronWord的文字輪廓效果功能有何獨特之處?
IronWord 的 TextOutlineEffect 類別提供了一個全面的 API,允許對顏色、粗細和風格等屬性進行細緻控制,確保專業和符合品牌規範的文件設計。
可以自訂文字輪廓效果的屬性嗎?
可以,IronWord允許Customization自訂文字輪廓屬性,例如顏色、線條寬度、線條連接樣式以及虛線模式,以符合特定的設計和品牌需求。
為什麼我應在商務文件中使用自訂文字輪廓?
自訂文字輪廓可以增強可讀性、確保品牌一致性,並能適應各種觀看條件,如不同設備和紙張類型,使文件更專業且易於訪問。
`TextStyle`類別在添加文字輪廓中有什麼作用?
IronWord 中的 TextStyle 類別用於配置和應用 TextOutlineEffect 到文件中的文字,決定文字輪廓的視覺外觀。
在Word文件中,`TextOutlineEffect`如何透過IronWord進行整合?
您可以通過建立一個 TextStyle,將其 TextEffect 設定包含輪廓,並將其分配給 Word 文件中 Paragraph 中的 Run,來應用 TextOutlineEffect。
IronWord是否支援快速原型設計的文字輪廓效果?
是的,IronWord支援快速原型設計,允許快速應用預設文字輪廓效果,在不需要複雜配置的情況下立即提升文字。
有哪些使用文字輪廓於專業文件中的最佳實踐?
最佳實踐包括在不同格式中測試輪廓、保持文件風格的一致性,並根據輸出需求調整寬度和顏色等參數,以確保最佳的可讀性和美觀性。
IronWord如何幫助文件設計的一致性?
IronWord允許定義和重用文字輪廓的樣式範本,使得在各種文件元素如標題和強調文字中一致應用文字樣式。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。