IRONSOFTWAREHOME

How to Add Style to Text in DOCX with C#

Ahmad Sohail
Ahmad Sohail
Updated: 2026年6月4日

IronWord的TextStyle類別使.NET開發人員能夠以程式化方式將專業的文字格式應用到Word文件中,包括字體、顏色、粗體、斜體、底線等。 無論您是在生成報告、建立範本還是自動化文件建立,IronWord都提供了全面的樣式工具,可以複製Microsoft Word的格式選項。

快速入門:在DOCX中使用C#為文字加樣式
  1. 1Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

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

    // 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");
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronWord,透過免費試用
    arrow pointer

如何為DOCX新增文字樣式?

在IronWord中應用文字樣式需要使用Run包裝模式。 建立一個TextContent。 使用例如TextContent)。

樣式設定完成後,使用Paragraph中,將段落插入文件中,並保存結果。 此方法為自動化文件生產場景提供了一致樣式所需的程式化文字格式控制。

請注意: IronWord的文件層次結構遵循結構:DocumentDocumentSectionParagraphRunTextContent。 樣式應用於TextContent
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");
C#

這會生成什麼輸出?

Microsoft Word介面顯示"版面配置"功能區和應用紅色刪除線文字格式的文件。

TextFont屬性內設定。 TextContent並保持樣式,遵循IronWord的文件層次結構模式。 這個範例演示了如何將多種樣式屬性結合應用於Run以建立豐富格式的文字。


我可以新增哪些特定樣式?

如何更改文字顏色?

IronWord.Models.Color中的預定義顏色或自訂的十六進位值。 這強調了特定內容或匹配品牌顏色。 IronWord支持多種顏色,包括紅色、藍色、綠色、橄欖色、海軍藍和栗色。

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");

有色文字看起來如何?

Microsoft Word顯示橄欖色色調文字格式,首頁標籤功能區中顯示字體和段落工具。

如何設置字體系列和大小?

使用TextFont屬性自訂文字外觀。 將FontFamily設置為任何已安裝的字體名稱(例如,"Arial","Times New Roman")和點數的FontSize。 這建立了視覺層次結構,並確保在不同裝置和平台上的可讀性。

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");

自訂字體樣式如何顯示?

Microsoft Word顯示工具欄中選擇24pt的Arial字體,帶有格式化的範例文字。

如何使文字加粗?

BoldBold文字常用於標題、強調或突出重要資訊。 結合其他樣式屬性,Bold文字建立視覺層次結構,並提高可讀性。

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");

粗體文字怎麼看?

Microsoft Word介面顯示粗體文字格式,"this is bold!"文字顯示在文件中。

如何使文字斜體?

Italic樣式。 Italic文字通常用於強調、標題、外文或技術詞彙。 這種微妙的格式將文字元素區分開來,而不會像Bold格式那樣具視覺負擔。

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");

斜體文字怎樣看?

Word文件顯示斜體文字,首頁標籤功能區介面中顯示格式選項。

有哪些可用的樣式屬性?

IronWord提供了全面的樣式屬性,鏡像Microsoft Word的格式選項。 這些屬性結合起來,建立符合專業文件標準的複雜文字格式。

樣式方法描述範例
TextFont使用Font物件自訂文字外觀,設定字體系列。注意:FontSizeTextStyle層次設置,而不是在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枚舉應用刪除線(StrikeDoubleStrike)設定文字刪除線。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的文字樣式功能來源於結合多種屬性來實現複雜的格式化。 這是一個範例,展示了通過結合各種樣式屬性實現的專業樣式文字:

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");
C#

此全面的樣式方法建立的文件在整個應用程式的文件生成過程中保持一致的品牌和專業外觀。

常見問題

如何在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中的預定義顏色或自定義十六進位值設置文字顏色。此功能有助於強調特定內容或在文件中匹配品牌顏色。

What property is used to italicize text in IronWord?

The IsItalic property is used within the TextStyle class to apply italic formatting to text in IronWord documents.

How do I apply an underline to text using IronWord?

To underline text in an IronWord document, create a new Underline object and assign it to the Underline property in the TextStyle class for the Run object containing your text.

Can I apply complex text styling using multiple properties in IronWord?

Yes, IronWord enables complex text styling by combining multiple TextStyle properties, such as FontSize, Color, IsBold, and Underline, to create richly formatted text in Word documents.

What is the benefit of using IronWord for text styling in DOCX documents?

IronWord offers programmatic control over text styling in DOCX documents, allowing developers to automate document creation while ensuring consistent and professional formatting similar to Microsoft Word.

Ahmad Sohail
全端開發人員

Ahmad是一位擁有C#、Python和網頁技術堅實基礎的全端開發人員。他對構建可擴展的軟體解決方案深感興趣,並喜歡探索設計和功能在現實世界應用中的結合。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 56,401版本:2026.9剛剛發布

即時獲取您的30天試用金鑰
無需信用卡或帳戶建立
C# PDF的NuGet程式庫
通過NuGet安裝

版本: 2026.9

PM > Install-Package IronWord
nuget.org/packages/IronWord/
  1. 在方案總覽中右鍵點擊引用,管理NuGet包
  2. 選擇Browse並搜尋“IronWord”
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronWord到如~/Libs的目錄中
  2. 在Visual Studio方案總覽中右鍵點擊引用。選擇Browse,“IronWord.dll”

授權從$999

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費現場演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
完成以下表單或發郵件至sales@ironsoftware.com
您的詳細資訊將始終保持機密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立