How to Add Style to Text in DOCX with C

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

IronWordのTextStyleクラスを使用すると、.NET開発者は、フォント、色、太字、斜体、下線など、WORD文書に対してプログラムからProfessionalなテキスト書式設定を適用できます。 レポートの作成、テンプレートの作成、文書作成の自動化など、IronWordはMicrosoft Wordの書式オプションを再現する包括的なスタイリング・ツールを提供します。

クイックスタート: C# を使用して DOCX 内のテキストにスタイルを設定する

  1. IronWord をNuGetパッケージマネージャでインストール

    PM > Install-Package IronWord
  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. 実際の環境でテストするためにデプロイする

    今日プロジェクトで IronWord を使い始めましょう無料トライアル

    arrow pointer

DOCXにテキストスタイルを追加するには?

IronWordでテキストスタイルを適用するには、Runというラッパーパターンを使用する必要があります。 WordDocument オブジェクトを作成し、次に、テキストを含む TextContent を格納した Run オブジェクトを作成します。 TextStyleTextContent ではない)に適用し、Color、または FontSize などのプロパティを使用して、TextStyleTextContent ではない)に適用してください。

スタイル設定が完了したら、AddChildを使用して追加し、その段落をドキュメントに挿入して、結果を保存してください。 このアプローチでは、一貫したスタイルが要求される自動文書生成シナリオ向けに、テキスト書式をプログラムで制御できます。

ご注意IronWordのドキュメント階層は、以下の構造に従います:DocumentDocumentSectionParagraphRunTextContent。 スタイルは Run レベルで適用され、TextContent には直接適用されません。)}]

:path=/static-assets/word/content-code-examples/how-to/add-style-text-simple.cs
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, // No 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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

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

' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))

' Configure text style settings
textRun.Style = New TextStyle() With {
    .FontSize = 24, ' Text Size is 24
    .TextFont = New Font() With {
        .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 ' No strike-through
}

Dim paragraph As New Paragraph()

' Add text to paragraph
paragraph.AddChild(textRun)

' Add paragraph to document
doc.AddParagraph(paragraph)

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

どのようなアウトプットを生成しますか?

Microsoft Wordのインターフェイスで、レイアウトリボンと赤の取り消し線付きテキスト書式が適用されたドキュメントを示す。

TextStyle クラスは、フォントのプロパティ、テキストの色、太字、斜体、下線など、基本的な書式設定オプションを提供します。 FontSizeTextStyle レベル(Font 内ではなく)で設定されるのに対し、FontFamilyTextFont プロパティ内で設定される点に注意してください。 Run オブジェクトは TextContent をラップし、IronWord のドキュメント階層パターンに従ってスタイルを保持します。 この例は、Runに適用された際に、複数のスタイルプロパティが組み合わさって、リッチな書式設定が施されたテキストが生成される様子を示しています。


どのようなスタイルを追加できますか?

テキストの色を変更するにはどうすればよいですか?

Color プロパティは、TextStyle から定義済みの色またはカスタム 16 進数値を使用して、テキストの色を設定します。 特定のコンテンツを強調したり、ブランドカラーに合わせたりします。 IronWordはレッド、ブルー、グリーン、オリーブ、ネイビー、マルーンなど幅広い色をサポートしています。

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-text.cs
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")
$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;
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")
$vbLabelText   $csharpLabel

カスタム フォント スタイリングはどのように見えますか?

Microsoft Wordのツールバーで選択された24ptのArialフォントと表示された書式付きサンプルテキスト

テキストを太字にするにはどうすればよいですか?

IsBold プロパティを true に設定すると、テキストが Bold になります。 Bold テキストは、見出し、強調、または重要な情報のハイライトによく使用されます。 Bold テキストは、他のスタイルプロパティと組み合わせることで、視覚的な階層構造を作り出し、可読性を向上させます。

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-bold.cs
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")
$vbLabelText   $csharpLabel

太字はどのように見えますか?

これは太字です!

テキストを斜体にするには?

IsItalic プロパティを true に設定すると、Italic のようなスタイルになります。 Italic テキストは通常、強調、見出し、外来語、または専門用語に使用されます。 この微妙な書式設定により、Bold 形式のような視覚的な重さを感じさせることなく、テキスト要素を区別しています。

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-italic.cs
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")
$vbLabelText   $csharpLabel

斜体のテキストはどのように見えますか?

ホームタブの書式設定オプションがリボンインターフェースに表示されているイタリック体のテキストを示すWord文書

利用可能なスタイリング プロパティは何ですか?

IronWordはMicrosoft Wordの書式オプションを反映した包括的なスタイル設定プロパティを提供します。 これらの特性を組み合わせて、プロフェッショナルな文書基準を満たす複雑なテキストフォーマットを作成します。

スタイリング方法 翻訳内容 翻訳例
テキストフォント Fontオブジェクトを使用してフォント ファミリを設定し、テキストの外観をカスタマイズします。注: FontSizeFont内ではなく、 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,
    LineSpacing = new LineSpacing() { Value = 1.15 }  // Slightly increased line spacing
};

Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
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 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,
    LineSpacing = new LineSpacing() { Value = 1.15 }  // Slightly increased line spacing
};

Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);

// Save the document
doc.SaveAs("professional-document.docx");
Imports IronWord
Imports IronWord.Models

' Create a new document
Dim doc As New WordDocument()

' Create richly formatted header text using Run
Dim headerRun As New Run(New TextContent("Professional Document Header"))
headerRun.Style = New TextStyle() With {
    .FontSize = 28,
    .TextFont = New Font() With {
        .FontFamily = "Georgia"
    },
    .Color = Color.DarkBlue,
    .IsBold = True,
    .SmallCaps = True,
    .Underline = New Underline(),
    .CharacterScale = 110,  ' Slightly expand character width
    .Shading = New Shading() With {
        .Color = Color.LightGray  ' Light background
    }
}

' Add header to document using AddChild for styled Run
Dim headerParagraph As New Paragraph()
headerParagraph.AddChild(headerRun)
doc.AddParagraph(headerParagraph)

' Create body text with different styling
Dim bodyRun As New Run(New TextContent("This is professionally formatted body text with custom styling."))
bodyRun.Style = New TextStyle() With {
    .FontSize = 11,
    .TextFont = New Font() With {
        .FontFamily = "Calibri"
    },
    .Color = Color.Black,
    .LineSpacing = New LineSpacing() With {.Value = 1.15}  ' Slightly increased line spacing
}

Dim bodyParagraph As New Paragraph()
bodyParagraph.AddChild(bodyRun)
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の定義済みの色や、カスタムの16進数値を使用してテキストの色を設定することができます。この機能により、特定のコンテンツを強調したり、ドキュメント内のブランドカラーにマッチさせることができます。

Ahmad Sohail
フルスタックデベロッパー

Ahmadは、C#、Python、およびウェブ技術に強い基盤を持つフルスタック開発者です。彼はスケーラブルなソフトウェアソリューションの構築に深い関心を持ち、デザインと機能が実際のアプリケーションでどのように融合するかを探求することを楽しんでいます。

Iron Softwareチームに参加する前、Ahmadは自動化プロジェクトやAPI統合に取り組み、パフォーマンスの向上と開発者の体験向上に注力してきました。

彼の自由時間には、UI/UXのアイデアを試したり、オープンソースツールに貢献したり、時折テクニカルライティングやドキュメンテーションに取り組んで、複雑なトピックを理解しやすくすることを目指しています。

準備はできましたか?
Nuget ダウンロード 44,829 | バージョン: 2026.5 just released
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package IronWord
サンプルを実行する あなたのデータが Word ドキュメントになるのを見る。