How to Add Style to Text in DOCX with C
IronWord 的 TextStyle 類別讓 .NET 開發人員能夠透過程式設計方式,對 Word 文件套用 Professional 文字格式,包括字型、顏色、粗體、斜體、底線等。 無論是生成報告、建立範本,還是自動化文件製作,IronWord 皆提供全面的樣式設定工具,可重現 Microsoft WORD 的格式設定選項。
快速入門:使用 C# 為 DOCX 文件設定文字樣式
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronWord
PM > Install-Package IronWord -
請複製並執行此程式碼片段。
// Quick example using IronWord; using IronWord.Models; // Initialize a new Word document WordDocument doc = new WordDocument(); // Create a Run with styled text Run textRun = new Run(new TextContent("Styled text")); // Apply styling properties to the Run textRun.Style = new TextStyle() { IsBold = true, Color = Color.Red, FontSize = 16, TextFont = new Font() { FontFamily = "Arial" } }; // Create paragraph and add the styled Run Paragraph paragraph = new Paragraph(); paragraph.AddChild(textRun); // Add paragraph to document and save doc.AddParagraph(paragraph); doc.SaveAs("styled.docx"); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronWord
簡化工作流程(5 個步驟)
- 安裝 IronWord C# 程式庫
- 建立一個
Run物件,內容包含TextContent - 申請
TextStyle至Run並具備以下屬性:FontSize,IsBold,顏色 - 請將格式化的
Run至ParagraphusingAddChild - 將已套用樣式的文件儲存
如何在 DOCX 檔案中新增文字樣式?
在 IronWord 中套用文字樣式時,需使用 Run 包覆模式。 建立一個 WordDocument 物件,然後建立一個 Run 物件,並將您的文字 TextContent 放入其中。 請使用 Color 或 FontSize 等屬性,將 TextStyle 套用至 Run(而非 TextContent)。
格式設定完成後,請使用 AddChild 將 Run 嵌入 Paragraph 中,將段落插入文件,並儲存結果。 此方法可對文字格式進行程式化控制,適用於需要一致樣式的自動化文件生成情境。
Document → DocumentSection → Paragraph → Run → TextContent. 樣式應套用至 Run 層級,而非直接套用至 TextContent。)}]:path=/static-assets/word/content-code-examples/how-to/add-style-text-simple.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("sample.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
// Configure text style settings
textRun.Style = new TextStyle()
{
FontSize = 24, // Text Size is 24
TextFont = new Font()
{
FontFamily = "Calibri" // Text Font is "Calibri"
},
Color = Color.Red, // Set text color to red
IsBold = true, // Make text bold
IsItalic = true, // Make text italic
Underline = new Underline(), // Have an underline
Strike = StrikeValue.DoubleStrike, // No strike-through
};
Paragraph paragraph = new Paragraph();
// Add text to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("add-text-style.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("sample.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
' Configure text style settings
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Text Size is 24
.TextFont = New Font() With {
.FontFamily = "Calibri" ' Text Font is "Calibri"
},
.Color = Color.Red, ' Set text color to red
.IsBold = True, ' Make text bold
.IsItalic = True, ' Make text italic
.Underline = New Underline(), ' Have an underline
.Strike = StrikeValue.DoubleStrike ' No strike-through
}
Dim paragraph As New Paragraph()
' Add text to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("add-text-style.docx")
這將產生什麼樣的輸出?
TextStyle 類別提供基本的格式設定選項,包括字型屬性、文字顏色、粗體、斜體及底線。 請注意,TextStyle 是在 TextStyle 層級進行設定(而非位於 Font 內部),而 FontFamily 則是在 TextFont 屬性中設定。 Run 物件包覆 TextContent 並承載樣式,遵循 IronWord 的文件層級結構模式。 此範例展示當多種樣式屬性應用於 Run 時,如何結合以產生格式豐富的文字。
我可以添加哪些特定樣式?
如何變更文字顏色?
Color 中的 TextStyle 屬性可透過 IronWord.Models.Co/lor 中的預設顏色或自訂十六進位值來設定文字顏色。 此處強調特定內容或呼應品牌色彩。 IronWord 支援多種顏色,包括紅色、藍色、綠色、橄欖綠、海軍藍和栗色。
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-text.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add colored text
Run textRun = new Run(new TextContent("This text is olive-colored!"));
textRun.Style = new TextStyle()
{
Color = IronWord.Models.Color.Olive // defining text to be colored olive
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("colored-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add colored text
Dim textRun As New Run(New TextContent("This text is olive-colored!"))
textRun.Style = New TextStyle() With {
.Color = IronWord.Models.Color.Olive ' defining text to be colored olive
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("colored-text.docx")
彩色文字呈現為何種樣貌?
如何設定字型家族與字型大小?
using TextFont 屬性自訂文字外觀。 請將 FontFamily 設定為任何已安裝的字型名稱(例如"Arial"、"Times New Roman"),並將 FontSize 設定為點數。 這能建立視覺層級,並確保在不同裝置與平台上的可讀性。
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-font.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add text with custom font family and size
Run textRun = new Run(new TextContent("This text uses Arial at 24pt!"));
textRun.Style = new TextStyle()
{
FontSize = 24, // Set font size in points
TextFont = new IronWord.Models.Font()
{
FontFamily = "Arial" // Set font family
}
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("font-styled-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add text with custom font family and size
Dim textRun As New Run(New TextContent("This text uses Arial at 24pt!"))
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Set font size in points
.TextFont = New IronWord.Models.Font() With {
.FontFamily = "Arial" ' Set font family
}
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("font-styled-text.docx")
自訂字型樣式呈現為何?
如何將文字設為粗體?
將 IsBold 屬性設定為 true,即可使文字 Bold。 Bold 標籤常用於標題、強調或標示重要資訊。 結合其他樣式屬性,Bold 文字可建立視覺層級並提升可讀性。
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-bold.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add bold text
Run textRun = new Run(new TextContent("this is bold!"));
textRun.Style = new TextStyle()
{
IsBold = true // Make text bold
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("bold-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add bold text
Dim textRun As New Run(New TextContent("this is bold!"))
textRun.Style = New TextStyle() With {
.IsBold = True ' Make text bold
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("bold-text.docx")
粗體文字看起來是什麼樣子?
如何將文字設為斜體?
將 IsItalic 屬性設定為 true,以套用 Italic 樣式。 Italic 標籤通常用於強調、標題、外來詞或技術術語。 此細微的格式設定能區分文字元素,同時避免 Bold 格式所帶來的視覺負擔。
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-italic.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add italic text
Run textRun = new Run(new TextContent("this is italic."));
textRun.Style = new TextStyle()
{
IsItalic = true // Make text italic
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("italic-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add italic text
Dim textRun As New Run(New TextContent("this is italic."))
textRun.Style = New TextStyle() With {
.IsItalic = True ' Make text italic
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("italic-text.docx")
斜體文字看起來是什麼樣子?
有哪些可用的樣式屬性?
IronWord 提供與 Microsoft WORD 格式設定選項相呼應的全面樣式屬性。 這些特性共同構成符合Professional文件標準的複雜文字格式。
| 格式規範 | 描述 | 範例 |
|---|---|---|
TextFont |
使用 `Font` 物件自訂文字外觀,設定字型家族。注意: FontSize 設定於 TextStyle 層級,而非位於 Font. |
textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } }; |
顏色 |
使用 IronWord.Models.Co/lor 中的預定義顏色或自訂十六進位值設定文字顏色。 | textRun.Style.Co/lor = IronWord.Models.Co/lor.Red; |
IsBold |
使文字 Bold 當設定為 true時,通常用於標題或強調。 |
textRun.Style.IsBold = true; |
IsItalic |
套用 Italic 當設定為 true時,會為文字套用樣式,通常用於強調或標題。 |
textRun.Style.IsItalic = true; |
底線 |
使用 底線 ,並透過底線物件套用多種底線樣式。 |
textRun.Style.Underline = new Underline(); |
刪除 |
使用StrikeValue枚舉對文字套用刪除線 (刪除 或 DoubleStrike). |
textRun.Style.Strike = StrikeValue.Strike; |
大寫 |
將大寫效果套用至文字,將所有字元轉換為大寫顯示。 | textRun.Style.Caps = true; |
字元比例 |
將字元的比例寬度調整為其正常大小的百分比。 | textRun.Style.CharacterScale = 150; |
Emboss |
為文字套用浮雕效果,營造出立體凸起的視覺效果。 | textRun.Style.Emboss = true; |
重點 |
使用 `EmphasisMarkValues` 枚舉值為格式化文字添加強調標記。 |
textRun.Style.Emphasis = EmphasisMarkValues.Dot; |
行距 |
使用 `行距` 物件控制文字行間距,以提升可讀性。 |
textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; |
大綱 |
以輪廓效果呈現文字,僅顯示字元邊框。 | textRun.Style.Outline = true; |
陰影 |
使用 `陰影` 物件為文字套用背景顏色或陰影。 |
textRun.Style.Shading = new Shading() { Color = Color.Yellow }; |
SmallCaps |
將小寫字母轉換為首字母大寫,同時保持大小寫區別。 | textRun.Style.SmallCaps = true; |
VerticalPosition |
調整文字相對於其基線的垂直位置,單位以點為計。 | textRun.Style.VerticalPosition = 5.0; |
垂直文字對齊 |
使用 `VerticalPositionValues` 枚舉在容器內垂直對齊文字。 |
textRun.Style.VerticalTextAlignment = VerticalPositionValues.Superscript; |
結合多種風格
IronWord 的文字樣式設定能力,源自於結合多種屬性以實現複雜的格式設定。 以下範例透過結合多種樣式屬性,展示Professional風格的文本呈現:
using IronWord;
using IronWord.Models;
// Create a new document
WordDocument doc = new WordDocument();
// Create richly formatted header text using Run
Run headerRun = new Run(new TextContent("Professional Document Header"));
headerRun.Style = new TextStyle()
{
FontSize = 28,
TextFont = new Font()
{
FontFamily = "Georgia"
},
Color = Color.DarkBlue,
IsBold = true,
SmallCaps = true,
Underline = new Underline(),
CharacterScale = 110, // Slightly expand character width
Shading = new Shading()
{
Color = Color.LightGray // Light background
}
};
// Add header to document using AddChild for styled Run
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddChild(headerRun);
doc.AddParagraph(headerParagraph);
// Create body text with different styling
Run bodyRun = new Run(new TextContent("This is professionally formatted body text with custom styling."));
bodyRun.Style = new TextStyle()
{
FontSize = 11,
TextFont = new Font()
{
FontFamily = "Calibri"
},
Color = Color.Black,
LineSpacing = new LineSpacing() { Value = 1.15 } // Slightly increased line spacing
};
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);
// Save the document
doc.SaveAs("professional-document.docx");
using IronWord;
using IronWord.Models;
// Create a new document
WordDocument doc = new WordDocument();
// Create richly formatted header text using Run
Run headerRun = new Run(new TextContent("Professional Document Header"));
headerRun.Style = new TextStyle()
{
FontSize = 28,
TextFont = new Font()
{
FontFamily = "Georgia"
},
Color = Color.DarkBlue,
IsBold = true,
SmallCaps = true,
Underline = new Underline(),
CharacterScale = 110, // Slightly expand character width
Shading = new Shading()
{
Color = Color.LightGray // Light background
}
};
// Add header to document using AddChild for styled Run
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddChild(headerRun);
doc.AddParagraph(headerParagraph);
// Create body text with different styling
Run bodyRun = new Run(new TextContent("This is professionally formatted body text with custom styling."));
bodyRun.Style = new TextStyle()
{
FontSize = 11,
TextFont = new Font()
{
FontFamily = "Calibri"
},
Color = Color.Black,
LineSpacing = new LineSpacing() { Value = 1.15 } // Slightly increased line spacing
};
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);
// Save the document
doc.SaveAs("professional-document.docx");
Imports IronWord
Imports IronWord.Models
' Create a new document
Dim doc As New WordDocument()
' Create richly formatted header text using Run
Dim headerRun As New Run(New TextContent("Professional Document Header"))
headerRun.Style = New TextStyle() With {
.FontSize = 28,
.TextFont = New Font() With {
.FontFamily = "Georgia"
},
.Color = Color.DarkBlue,
.IsBold = True,
.SmallCaps = True,
.Underline = New Underline(),
.CharacterScale = 110, ' Slightly expand character width
.Shading = New Shading() With {
.Color = Color.LightGray ' Light background
}
}
' Add header to document using AddChild for styled Run
Dim headerParagraph As New Paragraph()
headerParagraph.AddChild(headerRun)
doc.AddParagraph(headerParagraph)
' Create body text with different styling
Dim bodyRun As New Run(New TextContent("This is professionally formatted body text with custom styling."))
bodyRun.Style = New TextStyle() With {
.FontSize = 11,
.TextFont = New Font() With {
.FontFamily = "Calibri"
},
.Color = Color.Black,
.LineSpacing = New LineSpacing() With {.Value = 1.15} ' Slightly increased line spacing
}
Dim bodyParagraph As New Paragraph()
bodyParagraph.AddChild(bodyRun)
doc.AddParagraph(bodyParagraph)
' Save the document
doc.SaveAs("professional-document.docx")
此全面的樣式設定方法,可在應用程式的文件生成過程中,確保所有文件維持一致的品牌形象與專業外觀。
常見問題
如何在 C# 中透過程式設計方式對 WORD 文件套用文字格式?
IronWord 的 TextStyle 類別可讓您套用 Professional 文字格式,包括字型、顏色、粗體、斜體及底線。只需建立一個包含您文字的 TextContent 物件,套用具有所需屬性的 TextStyle,將其加入段落,然後儲存文件即可。
在 DOCX 檔案中格式化文字的基本步驟有哪些?
using IronWord 設定文字樣式的方法:1) 透過 NuGet 安裝 IronWord,2) 建立 WordDocument 物件,3) 建立包含文字的 TextContent,4) 套用文字樣式屬性(如字型、顏色或粗體),5) 將文字加入段落並儲存。
有哪些文字格式設定選項可用?
IronWord 的 TextStyle 類別提供基本的格式設定選項,包括字型屬性(FontFamily 和 FontSize)、文字顏色、粗體、斜體、底線及刪除線。這些選項可相互組合,以建立格式豐富的文字。
如何變更文字的字型家族和大小?
請使用 TextStyle 中的 TextFont 屬性來指定字型家族與大小。將 FontFamily 設定為「Arial」或「Times New Roman」等字型,並將 FontSize 設定為您想要的點數大小,例如 16 點可呈現較大的文字。
我可以同時套用多種文字樣式嗎?
是的,IronWord 允許您將多個樣式屬性整合至單一 TextStyle 物件中。您可以同時套用粗體、斜體、顏色及字型變更,以建立複雜的文字格式。
如何使用 C# 在 WORD 文件中變更文字顏色?
IronWord 的 TextStyle 中的 Color 屬性,可讓您使用 IronWord.Models.Co/lor 中的預定義顏色或自訂十六進位值來設定文字顏色。此功能有助於在文件中突顯特定內容,或配合品牌色調。

