How to Add Style to Text in DOCX with C#
IronWordのTextStyleクラスは、.NET開発者がWordドキュメントにプログラムでプロフェッショナルなテキストフォーマット(フォント、色、太字、斜体、下線など)を適用することを可能にします。 レポートの作成、テンプレートの作成、文書作成の自動化など、IronWordはMicrosoft Wordの書式オプションを再現する包括的なスタイリング・ツールを提供します。
-
1Install IronWord with NuGet Package Manager
-
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実際の環境でテストするためにデプロイする
今日プロジェクトで IronWord を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- IronWord C#ライブラリをインストールする
TextContentを含むRunオブジェクトを作成するFontSize、IsBold、カラーなどのプロパティを使用して、RunにTextStyle適用します。AddChildを使用してParagraphにスタイル付きRunを追加する- スタイルを適用してドキュメントを保存
DOCXにテキストスタイルを追加するには?
IronWordでテキストスタイルを適用するには、Runラッパーパターンを使用する必要があります。 まずRunオブジェクトを作成します。 プロパティとしてTextStyleを適用します。
スタイル適用後、Runを追加し、段落を文書に挿入して結果を保存します。 このアプローチでは、一貫したスタイルが要求される自動文書生成シナリオ向けに、テキスト書式をプログラムで制御できます。
IronWordのドキュメント階層は、Document → DocumentSection → Paragraph → Run → TextContentの構造に従います。 スタイルは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");
どのようなアウトプットを生成しますか?

TextFontプロパティ内で設定されます。 TextContentをラップし、IronWordのドキュメント階層パターンに従ってスタイルを保持します。 この例はRunに適用されたとき、複数のスタイリングプロパティが組み合わさってリッチなフォーマットのテキストを作成する方法を示しています。
どのようなスタイルを追加できますか?
テキストの色を変更するにはどうすればよいですか?
IronWord.Models.Colorの事前定義された色またはカスタムの16進値を使用してテキストカラーを設定します。 特定のコンテンツを強調したり、ブランドカラーに合わせたりします。 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");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プロパティでテキスト外観をカスタマイズします。 任意のインストールされたフォント名(例:"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");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テキストは視覚的な階層を作り出し、読みやすさを向上させます。
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")太字はどのように見えますか?

テキストを斜体にするには?
trueに設定します。 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");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の書式オプションを反映した包括的なスタイル設定プロパティを提供します。 これらの特性を組み合わせて、プロフェッショナルな文書基準を満たす複雑なテキストフォーマットを作成します。
| スタイリング方法 | 翻訳内容 | 翻訳例 |
|---|---|---|
テキストフォント | Fontオブジェクトを使用してフォント ファミリを設定し、テキストの外観をカスタマイズします。注: FontSizeはFont内ではなく、 TextStyleレベルで設定されます。 | textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } }; |
カラー | IronWord.Models.Colorからの定義済みの色またはカスタム16進数値を使用してテキストの色を設定します。 | textRun.Style.Color = IronWord.Models.Color.Red; |
IsBold | 見出しや強調によく使用されるtrueに設定するとテキストをBoldにします。 | textRun.Style.IsBold = true; |
IsItalic | 強調またはタイトルに通常使用されるtrueに設定するとテキストにItalicスタイルを適用します。 | textRun.Style.IsItalic = true; |
アンダーライン | さまざまな下線スタイルを持つアンダーラインオブジェクトを使用してテキストにアンダーラインを追加します。 | textRun.Style.Underline = new Underline(); |
ストライク | StrikeValue列挙を使用してストライクまたはDoubleStrikeのテキストに打ち消し線を適用します。 | textRun.Style.Strike = StrikeValue.Strike; |
キャップ | テキストに大文字化効果を適用し、すべての文字を大文字表示に変換します。 | textRun.Style.Caps = true; |
CharacterScale | 文字の幅を通常のサイズに対する割合で調整します。 | textRun.Style.CharacterScale = 150; |
Emboss | テキストにエンボス効果を適用し、盛り上がった外観を作成します。 | textRun.Style.Emboss = true; |
強調 | EmphasisMarkValues列挙値を使用して、スタイル付きテキストに強調マークを追加します。 | textRun.Style.Emphasis = EmphasisMarkValues.Dot; |
行間 | 行間オブジェクトを使用して、読みやすさを向上させるためにテキストの行間を制御します。 | textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; |
概要 | アウトライン効果でテキストをレンダリングし、文字の境界のみを表示します。 | textRun.Style.Outline = true; |
シェーディング | シェーディングオブジェクトを使用して、テキストに背景色やシェーディングを適用します。 | textRun.Style.Shading = new Shading() { Color = Color.Yellow }; |
スモールキャップ | 大文字と小文字の区別を維持しながら、小文字を小文字に変換します。 | textRun.Style.SmallCaps = true; |
縦位置 | ポイント単位で、ベースラインに対するテキストの垂直方向の配置を調整します。 | 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#でプログラム的に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の定義済みの色や、カスタムの16進数値を使用してテキストの色を設定することができます。この機能により、特定のコンテンツを強調したり、ドキュメント内のブランドカラーにマッチさせることができます。
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は、C#、Python、およびウェブ技術に強い基盤を持つフルスタック開発者です。彼はスケーラブルなソフトウェアソリューションの構築に深い関心を持ち、デザインと機能が実際のアプリケーションでどのように融合するかを探求することを楽しんでいます。