How to Add Style to Text in DOCX with C
IronWord的TextStyle類別使.NET開發人員能夠以程式化方式將專業的文字格式應用到Word文件中,包括字體、顏色、粗體、斜體、底線等。 無論您是在生成報告、建立範本還是自動化文件建立,IronWord都提供了全面的樣式工具,可以複製Microsoft Word的格式選項。
快速入門:在DOCX中使用C#為文字加樣式
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/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步驟)
- Install the IronWord C# library
- 建立包含
TextContent的Run物件 - 使用
FontSize、IsBold、Color等屬性將TextStyle應用於Run - 使用
AddChild將帶樣式的Run新增到Paragraph - 保存應用樣式的文件
如何為DOCX新增文字樣式?
在IronWord中應用文字樣式需要使用Run包裝模式。 建立一個TextContent。 使用例如TextContent)。
樣式設定完成後,使用Paragraph中,將段落插入文件中,並保存結果。 此方法為自動化文件生產場景提供了一致樣式所需的程式化文字格式控制。
Document → DocumentSection → Paragraph → Run → TextContent。 樣式應用於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, // Double 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 ' Double 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")
這會生成什麼輸出?
TextFont屬性內設定。 TextContent並保持樣式,遵循IronWord的文件層次結構模式。 這個範例演示了如何將多種樣式屬性結合應用於Run以建立豐富格式的文字。
我可以新增哪些特定樣式?
如何更改文字顏色?
IronWord.Models.Color中的預定義顏色或自訂的十六進位值。 這強調了特定內容或匹配品牌顏色。 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")
有色文字看起來如何?
如何設置字體系列和大小?
使用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")
自訂字體樣式如何顯示?
如何使文字加粗?
將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")
粗體文字怎麼看?
如何使文字斜體?
將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的格式選項。 這些屬性結合起來,建立符合專業文件標準的複雜文字格式。
| 樣式方法 | 描述 | 範例 |
|---|---|---|
TextFont |
使用Font物件自訂文字外觀,設定字體系列。注意:FontSize在TextStyle層次設置,而不是在Font內。 |
textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } }; |
Color |
使用IronWord.Models.Color中的預定義顏色或自訂的十六進位值設定文字顏色。 | textRun.Style.Color = IronWord.Models.Color.Red; |
IsBold |
將文字設為Bold當設置為true時,通常用於標題或強調。 |
textRun.Style.IsBold = true; |
IsItalic |
當設置為true時,給文字應用Italic樣式,通常用於強調或標題。 |
textRun.Style.IsItalic = true; |
Underline |
使用強調的Underline物件為文字新增底線,具有各種下劃線樣式。 |
textRun.Style.Underline = new Underline(); |
Strike |
透過使用StrikeValue枚舉應用刪除線(Strike或DoubleStrike)設定文字刪除線。 |
textRun.Style.Strike = StrikeValue.Strike; |
Caps |
應用大寫效果到文字,將所有字元轉換為大寫顯示。 | textRun.Style.Caps = true; |
CharacterScale |
調整字元的比例寬度,作為其正常大小的百分比。 | textRun.Style.CharacterScale = 150; |
Emboss |
為文字應用浮雕效果,創造出凸起的外觀。 | textRun.Style.Emboss = true; |
Emphasis |
透過使用EmphasisMarkValues枚舉值,向已樣式的文字新增強調標記。 |
textRun.Style.Emphasis = EmphasisMarkValues.Dot; |
LineSpacing |
使用LineSpacing物件控制行文字之間的間距以提高可讀性。 |
textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; |
Outline |
將文字渲染為帶有外框效果,僅顯示字元邊界。 | textRun.Style.Outline = true; |
Shading |
使用強調的Shading物件將背景色或底紋應用於文字。 |
textRun.Style.Shading = new Shading() { Color = Color.Yellow }; |
SmallCaps |
將小寫字母轉換為小型大寫字母,同時維持大小寫區別。 | textRun.Style.SmallCaps = true; |
VerticalPosition |
調整文字相對於基線的垂直位置,以點為單位測量。 | textRun.Style.VerticalPosition = 5.0; |
VerticalTextAlignment |
使用強調的VerticalPositionValues枚舉在其容器中垂直定位文字。 |
textRun.Style.VerticalTextAlignment = VerticalPositionValues.Superscript; |
結合多種樣式
IronWord的文字樣式功能來源於結合多種屬性來實現複雜的格式化。 這是一個範例,展示了通過結合各種樣式屬性實現的專業樣式文字:
:path=/static-assets/word/content-code-examples/how-to/add-style-text-7.cs
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
};
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
}
Dim bodyParagraph As New Paragraph()
bodyParagraph.AddChild(bodyRun)
doc.AddParagraph(bodyParagraph)
' Save the document
doc.SaveAs("professional-document.docx")
此全面的樣式方法建立的文件在整個應用程式的文件生成過程中保持一致的品牌和專業外觀。
常見問題
如何在C#中以編程方式將文字格式應用於Word文件?
IronWord的TextStyle類別讓您可以應用專業的文字格式,包括字體、顏色、粗體、斜體和下劃線。只需建立一個帶有您文字的TextContent物件,應用包含所需屬性的TextStyle,將其新增到段落並保存文件。
在DOCX文件中設計文字的基本步驟是什麼?
使用IronWord設計文字:1)通過NuGet安裝IronWord,2)建立一個WordDocument物件,3)使用您的文字建立TextContent,4)應用TextStyle屬性,如字體、顏色或粗體,5)將文字新增到段落並保存。
有哪些文字格式選項可用?
IronWord的TextStyle類別提供基本的格式選項,包括字體屬性(FontFamily和FontSize)、文字顏色、粗體、斜體、下劃線及刪除線。這些選項可以結合建立豐富格式的文字。
如何更改文字的字型系列和大小?
在TextStyle中使用TextFont屬性指定字型系列和大小。將FontFamily設置為“Arial”或“Times New Roman”等字體,並將FontSize設置為您想要的點數,例如16點來表示較大的文字。
我可以同時應用多個文字樣式嗎?
是的,IronWord允許您在單個TextStyle物件中結合多種樣式屬性。您可以一次性應用粗體、斜體、顏色和字體更改,以建立復雜的文字格式。
如何使用C#在Word文件中更改文字顏色?
IronWord的TextStyle中的Color屬性允許您使用IronWord.Models.Color中的預定義顏色或自定義十六進位值設置文字顏色。此功能有助於強調特定內容或在文件中匹配品牌顏色。

