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

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

快速入門:使用 C# 設定 DOCX 中的文字樣式

  1. 透過 NuGet 安裝 IronWord: Install-Package IronWord
  2. 建立一個WordDocument對象
  3. 使用您的文字建立TextContent
  4. 應用所需的TextStyle屬性(字型、顏色、粗體等)。
  5. 在段落中新增文字並儲存文檔

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronWord

    PM > Install-Package IronWord

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

    // Quick example
    using IronWord;
    using IronWord.Models;
    
    // Initialize a new Word document
    WordDocument doc = new WordDocument();
    
    // Create styled text content
    TextContent text = new TextContent("Styled text");
    
    // Apply styling properties
    text.Style = new TextStyle() 
    { 
        IsBold = true, 
        Color = Color.Red,
        TextFont = new Font()
        {
            FontFamily = "Arial",
            FontSize = 16
        }
    };
    
    // Create paragraph and add the styled text
    Paragraph paragraph = new Paragraph();
    paragraph.AddText(text);
    
    // Add paragraph to document and save
    doc.AddParagraph(paragraph);
    doc.SaveAs("styled.docx");
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronWord,免費試用!
    arrow pointer

開始使用 IronWord


如何為 DOCX 檔案新增文字樣式?

在 IronWord 中套用文字樣式非常簡單。 建立一個WordDocument對象,然後建立一個TextContent物件並傳入你的文字。 使用IsBoldColorTextFont等屬性套用TextStyle ,並使用底線或刪除線等選項增強樣式。

設定好樣式後,向Paragraph新增文本,將段落插入文檔,然後儲存結果。 這種方法為需要一致樣式的自動文件產生場景提供了對文字格式的程式化控制。

:path=/static-assets/word/content-code-examples/how-to/add-style-text-simple.cs
using IronWord;

// Load docx
WordDocument doc = new WordDocument("sample.docx");

// Configure text
TextContent text = new TextContent();
text.Text = "Add text using IronWord";

// Configure text style settings
text.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Calibri", // Text Font is "Calibri"
        FontSize = 24, // Text Size is 24
    },
    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.AddText(text);

// Add paragraph to document
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("add-text-style.docx");
$vbLabelText   $csharpLabel

這將產生什麼輸出?

微軟 Word 介面顯示了

TextStyle類別提供了基本的格式設定選項,包括字型屬性、文字顏色、粗體、斜體和底線。範例示範如何組合多種樣式屬性來建立格式豐富的文字。


我可以添加哪些特定樣式?

如何更改文字顏色?

TextStyle中的Color屬性使用IronWord.Models.Color中的預設顏色或自訂十六進位值來設定文字顏色。 這可以突出特定內容或與品牌顏色相匹配。 IronWord 支援多種顏色,包括紅色、藍色、綠色、橄欖綠、海軍藍和栗色。

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-text.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add colored text
TextContent text = new TextContent("This text is olive-colored!");
text.Style = new TextStyle()
{
    Color = IronWord.Models.Color.Olive // defining text to be colored olive
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("colored-text.docx");
$vbLabelText   $csharpLabel

彩色文字看起來是什麼樣的?

Microsoft Word 顯示橄欖綠色的文字格式,開始標籤功能區顯示字型和段落工具。

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

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

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-font.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add text with custom font family and size
TextContent text = new TextContent("This text uses Arial at 24pt!");
text.Style = new TextStyle()
{
    TextFont = new IronWord.Models.Font()
    {
        FontFamily = "Arial",  // Set font family
        FontSize = 24          // Set font size in points
    }
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("font-styled-text.docx");
$vbLabelText   $csharpLabel

自訂字體樣式是什麼樣的?

Microsoft Word 工具列中已選取 24 磅的 Arial 字體,並顯示了已格式化的範例文字。

如何將文字加粗?

IsBold屬性設為true可使文字加粗。 粗體字通常用於標題、強調或突出重要訊息。 與其他樣式屬性結合使用,粗體文字可以創建視覺層次結構並提高可讀性。

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-bold.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add bold text
TextContent text = new TextContent("this is bold!");
text.Style = new TextStyle()
{
    IsBold = true  // Make text bold
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("bold-text.docx");
$vbLabelText   $csharpLabel

粗體文字是什麼樣子的?

微軟 Word 介面顯示粗體文字格式,文件中會顯示

如何將文字設為斜體?

IsItalic屬性設為true啟用斜體樣式。 斜體字通常用於強調、標題、外來語或技術術語。 這種微妙的格式調整可以在不增加粗體格式視覺衝擊的情況下區分文字元素。

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-italic.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add italic text
TextContent text = new TextContent("this is italic.");
text.Style = new TextStyle()
{
    IsItalic = true  // Make text italic
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("italic-text.docx");
$vbLabelText   $csharpLabel

斜體文本是什麼樣子的?

Word 文件中顯示了斜體文本,功能區介面中

有哪些可用的樣式屬性?

IronWord 提供全面的樣式屬性,與 Microsoft Word 的格式設定選項相呼應。 這些特性結合起來,可以創造出符合專業文件標準的複雜文字格式。

造型方法 描述 例子
TextFont 使用Font物件自訂文字外觀,設定字體系列和磅數大小。 text.Style.TextFont = new Font() { FontFamily = "Calibri", FontSize = 24 };
Color 使用IronWord.Models.Color中的預設顏色或自訂十六進位值來設定文字顏色。 text.Style.Color = IronWord.Models.Color.Red;
IsBold 當設定為true時,文字會加粗,常用於標題或強調。 text.Style.IsBold = true;
IsItalic 當設定為true時,文字會套用斜體樣式,通常用於強調或標題。 text.Style.IsItalic = true;
Underline 使用具有各種下劃線樣式的Underline物件為文字新增底線。 text.Style.Underline = new Underline();
Strike 使用StrikeValue枚舉(Strike 或 DoubleStrike)為文字新增刪除線。 text.Style.Strike = StrikeValue.Strike;
Caps 對文字套用大小寫效果,將所有字元轉換為大寫顯示。 text.Style.Caps = true;
CharacterScale 調整字元的寬度比例,使其達到正常大小的百分比。 text.Style.CharacterScale = 150;
Emboss 為文字添加浮雕效果,使其呈現凸起外觀。 text.Style.Emboss = true;
Emphasis 使用EmphasisMarkValues枚舉值為樣式文字新增強調標記。 text.Style.Emphasis = EmphasisMarkValues.Dot;
LineSpacing 使用LineSpacing物件控製文字行之間的間距,以提高可讀性。 text.Style.LineSpacing = new LineSpacing() { Value = 1.5 };
Outline 以輪廓效果渲染文本,僅顯示字元邊框。 text.Style.Outline = true;
Shading 使用Shading物件為文字套用背景顏色或陰影。 text.Style.Shading = new Shading() { Color = Color.Yellow };
SmallCaps 將小寫字母轉換為小型大寫字母,同時保持大小寫差異。 text.Style.SmallCaps = true;
VerticalPosition 調整文字相對於其基線的垂直位置,以磅為單位進行測量。 text.Style.VerticalPosition = 5.0;
VerticalTextAlignment 使用VerticalPositionValues列舉在容器內垂直定位文字。 text.Style.VerticalTextAlignment = VerticalPositionValues.Superscript;

多種風格的融合

IronWord 的文字樣式功能強大,它透過組合多個屬性來實現複雜的格式設定。 以下範例透過結合各種樣式屬性,示範如何建立專業風格的文字:

using IronWord;
using IronWord.Models;

// Create a new document
WordDocument doc = new WordDocument();

// Create richly formatted text
TextContent headerText = new TextContent("Professional Document Header");
headerText.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Georgia",
        FontSize = 28
    },
    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
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddText(headerText);
doc.AddParagraph(headerParagraph);

// Create body text with different styling
TextContent bodyText = new TextContent("This is professionally formatted body text with custom styling.");
bodyText.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Calibri",
        FontSize = 11
    },
    Color = Color.Black,
    LineSpacing = new LineSpacing() { Value = 1.15 }  // Slightly increased line spacing
};

Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddText(bodyText);
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 text
TextContent headerText = new TextContent("Professional Document Header");
headerText.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Georgia",
        FontSize = 28
    },
    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
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddText(headerText);
doc.AddParagraph(headerParagraph);

// Create body text with different styling
TextContent bodyText = new TextContent("This is professionally formatted body text with custom styling.");
bodyText.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Calibri",
        FontSize = 11
    },
    Color = Color.Black,
    LineSpacing = new LineSpacing() { Value = 1.15 }  // Slightly increased line spacing
};

Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddText(bodyText);
doc.AddParagraph(bodyParagraph);

// Save the document
doc.SaveAs("professional-document.docx");
$vbLabelText   $csharpLabel

這種全面的樣式設計方法可以建立在整個應用程式文件生成過程中保持一致的品牌形象和專業外觀的文檔。

常見問題解答

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

艾哈邁德·索海爾
全端開發工程師

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

在加入 Iron Software 團隊之前,Ahmad 曾從事自動化專案和 API 整合工作,專注於提高效能和開發者體驗。

在空閒時間,他喜歡嘗試 UI/UX 設計理念,為開源工具做出貢獻,偶爾還會涉足技術寫作和文件編寫,使複雜的主題更容易理解。

準備好開始了嗎?
Nuget 下載 29,594 | 版本: 2025.12 剛剛發布