如何使用 C# 為 DOCX 中的文字套用樣式

Ahmad Sohail
Ahmad Sohail
已更新: 2026年7月21日

IronWord 的 TextStyle 類別讓 .NET 開發者能以程式為 Word 文件中的文字套用專業格式,包括字型、顏色、粗體、斜體和底線等。無論是產生報表、建立範本或自動建立文件,IronWord 都提供與 Microsoft Word 格式選項相仿的樣式設定工具。

快速入門:使用 C# 在 DOCX 中格式化文字
  1. 1使用 IronWord 安裝 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");
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronWord,並使用 免費試用
    arrow pointer

如何為 DOCX 中的文字套用樣式?

在 IronWord 中,文字樣式由包裝文字內容的 Run 管理。先建立 WordDocument 物件,再建立 Run 物件,並以 TextContent 放入您的文字。將 TextStyle 套用至 Run(而非 TextContent),並設定 IsBold、Color 或 FontSize 等屬性。

設定樣式後,使用 AddChild 將 Run 加入 Paragraph,再將段落插入文件並儲存。這種方式可用程式控制文字格式,適合需要維持樣式一致的自動化文件產生流程。

請注意: IronWord的文件層級結構如下:Document → (可選DocumentSection)→ Paragraph → Run → TextContent。 可以通過AddParagraph將段落直接添加到WordDocument,而不需要明確的DocumentSection,如本頁範例所示。 樣式應用於Run層級,而不是直接在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");

輸出結果會是什麼樣子?

Microsoft Word 介面,顯示版面功能區及已套用紅色刪除線文字格式的文件

TextStyle 類別提供基本的格式選項,包括字型屬性、文字顏色、粗體、斜體和底線。請在 TextStyle 層級設定 FontSize(不在 Font 內設定),並在 TextFont 屬性中設定 FontFamily。依照 IronWord 的文件階層,Run 物件包裝 TextContent 並保存樣式。此範例示範如何結合多個樣式屬性,為 Run 中的文字套用多種格式。


可套用哪些文字樣式?

如何更改文字顏色?

TextStyle 的 Color 屬性可使用 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 字型,並顯示格式化的範例文字

如何將文字設為粗體?

將 IsBold 屬性設為 true,即可套用 Bold(粗體)。Bold 常用於標題或強調重要資訊。搭配其他樣式屬性,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 介面顯示粗體文字格式,並在文件中顯示「這是粗體!」文字

如何將文字設為斜體?

將 IsItalic 屬性設為 true,即可套用 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 物件設定字型家族,自訂文字外觀。請注意,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設為 true 時套用 Bold 粗體,常用於標題或強調文字。textRun.Style.IsBold = true;
IsItalic設為 true 時套用 Italic 斜體,常用於強調文字或標題。textRun.Style.IsItalic = true;
Underline使用支援多種底線樣式的 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 物件控制文字的行距,以提高可讀性;Value 是行高的倍數。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# 在 DOCX 文件中設置文字樣式?

您可以透過 IronWord 程式庫創建包含 TextContent 的 Run 物件,並對該 Run 應用 TextStyle,以在 DOCX 文件中設置文字樣式。TextStyle 的屬性如 FontSize、Color 和 IsBold 允許您以程序化方式自訂文字外觀。

IronWord 中的 TextStyle 類別用於什麼?

IronWord 中的 TextStyle 類別用於對 Word 文件中的文字應用各種格式設定選項,如字體、顏色、粗體、斜體和底線。

如何使用 IronWord 更改文字的字體顏色?

要使用 IronWord 更改文字字體的顏色,請將 TextStyle 類別中的 Color 屬性設定為來自 IronWord.Models.Color 的預定義顏色,或者使用客製化的十六進位值以滿足特定顏色需求。

我可以在 IronWord 中同時設置字體家族和大小嗎?

可以,IronWord 允許您為文字設置字體家族和大小。您可以使用 TextFont 屬性指定 FontFamily,並在 TextStyle 中使用 FontSize 屬性設置點數大小。

IronWord 中的 Run 物件有什麼用途?

IronWord 中的 Run 物件作為 TextContent 的包裝器,能夠在段落中以細粒度地應用字體、顏色和裝飾等文字樣式。

如何在 IronWord 文件中將文字設為粗體?

要在 IronWord 文件中將文字設為粗體,請將 TextStyle 類別的 IsBold 屬性設定為 true,然後將其應用於包含您文字的 Run 物件。

在 IronWord 中使用哪個屬性來將文字設為斜體?

使用 TextStyle 類別中的 IsItalic 屬性來對 IronWord 文件中的文字應用斜體格式。

如何使用 IronWord 為文字添加底線?

要在 IronWord 文件中為文字添加底線,請創建一個新的 Underline 物件,並將其分配給 TextStyle 類別中包含您文字的 Run 物件的 Underline 屬性。

我可以在 IronWord 中使用多個屬性應用複雜的文字樣式嗎?

可以,IronWord 允許結合多個 TextStyle 屬性,如 FontSize、Color、IsBold 和 Underline,創建豐富格式化的 Word 文件文字。

使用 IronWord 為 DOCX 文件設置文字樣式有什麼好處?

IronWord 提供了對 DOCX 文件中文字樣式的程式化控制,允許開發者自動化文件創建,並確保類似於 Microsoft Word 的一致和專業的格式設定。

Ahmad 是一位全端開發人員,精通 C#、Python 和 Web 技術。他對建立可擴展的軟體解決方案有著濃厚的興趣,並樂於探索如何在實際應用中實現設計與功能的完美結合。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 63,050版本:2026.11剛剛發布