IRONSOFTWAREHOME
新增樣式文字
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

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

// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
	FontSize = 72,
	TextFont = new Font()
	{
		FontFamily = "Caveat"
	},
	Color = IronWord.Models.Color.Red,
	IsBold = true,
	IsItalic = true,
	Underline = new Underline(),
	Strike = StrikeValue.Strike,
};

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddChild(textRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Load docx
Dim doc As New WordDocument("document.docx")

' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
	.FontSize = 72,
	.TextFont = New Font() With {
		.FontFamily = "Caveat"
	},
	.Color = IronWord.Models.Color.Red,
	.IsBold = True,
	.IsItalic = True,
	.Underline = New Underline(),
	.Strike = StrikeValue.Strike
}

Dim paragraph As New Paragraph()

' Add text
paragraph.AddChild(textRun)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("save_document.docx")
NuGet Download
PM > Install-Package IronWord
新增樣式文字

新增樣式文字

IronWord中的"新增樣式文字"功能允許開發者在向DOCX文件新增內容時應用各種文字樣式選項。 它提供對文字外觀的細緻控制,如指定字體系列、大小、顏色和樣式屬性,如strike-through。 透過建立一個包含Run,開發者可以在文件中的單獨文字元素上量身定制其顯示,確保有一個自定義的外觀和感覺。

此功能對動態生成專業格式的文件非常有用,比如報告或信件,其中的特定部分需要不同的樣式。 TextStyle類別可以輕鬆操縱這些屬性,允許在同一文件中同時進行簡單和複雜的樣式設置。

關鍵點

  1. 建立樣式運行

    • 建立一個TextContent
    • TextStyle物件以進行格式化。 FontSize
  2. 配置TextStyle

    • Font內部)以指定文字大小。
    • fontFamilyFontFamily
    • color指定文字顏色。 IronWord.Models.Color
    • italic:用於粗體和斜體格式的布林屬性。 IsBold
    • underline:為文字新增下劃線樣式。 IsItalic
    • StrikeValue枚舉應用刪除線格式。 Underline
    • Strike
  3. 新增到文件

    • 使用Paragraph
    • 段落然後使用AddParagraph新增到文件中。

程式碼解釋

此程式碼顯示如何使用IronWord在DOCX文件中建立和樣式化文字。 它首先通過初始化一個新的WordDocument物件開始,代表將要生成的文件。 建立一個包含"Styled text example"字串的Run以配置文字的外觀。

bold格式。 這些設置自定義了文字在最終文件中的顯示方式。

Run物件新增到文件中的一個段落。 此方法將格式化內容以適當的格式插入到Word文件中。 最後,呼叫SaveAs方法將文件導出為"styled_document.docx"。 結果是一個Word文件,其中插入的文字根據指定的樣式進行格式化,保存在輸出文件中的所有字體和格式屬性。

瀏覽文件元素教程以獲得更多樣式提示
在GitHub上查看

準備開始了嗎?

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

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天試用金鑰
無需信用卡或帳戶建立