C#でDOCXのテキストにスタイルを追加する方法

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

IronWordのTextStyleクラスを使うと、.NET開発者は、Word文書のフォント、色、太字、斜体、下線などの書式をプログラムで設定できます。IronWordにはMicrosoft Wordの書式設定に相当する機能がそろっており、レポートやテンプレートの作成、文書生成の自動化に利用できます。

クイックスタート:C#でDOCXのテキストに書式を設定
  1. 1IronWord を 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オブジェクトを作成し、続いてテキストを格納したTextContentを含むRunオブジェクトを作成します。IsBold、Color、FontSizeなどを指定したTextStyleは、TextContentではなくRunに適用します。

書式を設定したら、AddChildを使ってRunをParagraphに追加し、その段落を文書に挿入して保存します。書式をプログラムで制御できるため、自動生成する文書の書式を統一できます。

注意してください: IronWordのドキュメント階層は、次の構造に従います: Document → (オプションのDocumentSection) → Paragraph → Run → TextContent。 段落はこのページの例で示されているように、明示的なDocumentSectionなしでWordDocumentにAddParagraphを通じて直接追加できます。 スタイルは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クラスでは、フォント、テキストの色、太字、斜体、下線など、基本的な書式を指定できます。FontSizeはFontの内部ではなくTextStyleで指定し、FontFamilyはTextFontプロパティ内で設定します。IronWordの文書階層では、RunオブジェクトがTextContentを内包し、書式を保持します。この例では、Runに複数の書式プロパティを組み合わせて適用しています。


設定できる書式

テキストの色をどのように変更しますか?

TextStyleのColorプロパティでは、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");

テキストに色を付けた例

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");

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

太字の「this is bold!」が文書内に表示された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の書式設定に相当するプロパティがそろっています。これらを組み合わせると、体裁を整えた文書に必要な複雑な書式も設定できます。

書式設定プロパティ説明例
TextFontFontオブジェクトを使用してフォント ファミリを設定し、テキストの外観をカスタマイズします。注: FontSizeはFont内ではなく、 TextStyleレベルで設定されます。textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } };
ColorIronWord.Models.Colorからの定義済みの色またはカスタム16進数値を使用してテキストの色を設定します。textRun.Style.Color = IronWord.Models.Color.Red;
IsBoldtrueに設定すると、テキストが太字(Bold)になります。見出しや強調によく使われます。textRun.Style.IsBold = true;
IsItalictrueに設定すると、テキストが斜体(Italic)になります。強調やタイトルによく使われます。textRun.Style.IsItalic = true;
Underlineさまざまな下線スタイルを持つUnderlineオブジェクトを使用してテキストにUnderlineを追加します。textRun.Style.Underline = new Underline();
StrikeStrikeValue列挙型のStrikeまたはDoubleStrikeを指定して、テキストに一重または二重の取り消し線を引きます。textRun.Style.Strike = StrikeValue.Strike;
Capsテキストに大文字化効果を適用し、すべての文字を大文字表示に変換します。textRun.Style.Caps = true;
CharacterScale文字の幅を通常のサイズに対する割合で調整します。textRun.Style.CharacterScale = 150;
Embossテキストにエンボス効果を適用し、盛り上がった外観を作成します。textRun.Style.Emboss = true;
EmphasisEmphasisMarkValues列挙型の値を使って、書式を設定したテキストに圏点を付けます。textRun.Style.Emphasis = EmphasisMarkValues.Dot;
LineSpacingLineSpacingオブジェクトで行間を調整し、テキストを読みやすくします。Valueは行の高さに対する倍率です。textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 };
Outlineアウトライン効果でテキストをレンダリングし、文字の境界のみを表示します。textRun.Style.Outline = true;
ShadingShading オブジェクトを使用して、テキストに背景色やシェーディングを適用します。textRun.Style.Shading = new Shading() { Color = Color.Yellow };
SmallCaps大文字と小文字の区別を保ったまま、小文字を小型大文字で表示します。textRun.Style.SmallCaps = true;
VerticalPositionポイント単位で、ベースラインに対するテキストの垂直方向の配置を調整します。textRun.Style.VerticalPosition = 5.0;
VerticalTextAlignmentVerticalPositionValues列挙型を使って、コンテナー内でのテキストの垂直位置を指定します。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の定義済みの色や特定のカラー要件のためのカスタム16進値をセットします。

IronWordでフォントファミリーとサイズの両方を設定できますか?

はい、IronWordではテキストのフォントファミリーとサイズの両方を設定できます。TextFontプロパティを使ってFontFamilyを指定し、TextStyleのFontSizeプロパティでサイズをポイントで設定できます。

IronWordでRunオブジェクトの目的は何ですか?

IronWordのRunオブジェクトはTextContentのラッパーとして機能し、段落内でフォント、色、装飾などのテキストスタイルを細かいレベルで適用可能にします。

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

IronWordドキュメントでテキストを太字にするためには、TextStyleクラスのIsBoldプロパティをtrueに設定し、目的のテキストが含まれるRunオブジェクトに適用します。

IronWordでテキストをイタリックにするために使われるプロパティは何ですか?

IsItalicプロパティは、IronWordドキュメント内のテキストにイタリックフォーマットを適用するためにTextStyleクラス内で使用されます。

IronWordを使用してテキストに下線を引くにはどうすればよいですか?

IronWordドキュメントでテキストに下線を引くには、新しいUnderlineオブジェクトを作成し、テキストを含むRunオブジェクトのためにTextStyleクラスのUnderlineプロパティに割り当てます。

IronWordで複雑なテキストスタイリングを複数のプロパティを使って適用できますか?

はい、IronWordはTextStyleの複数のプロパティを組み合わせて複雑なテキストスタイリングを可能にし、Word文書内でリッチにフォーマットされたテキストを作成します。

DOCX文書でのテキストスタイリングにIronWordを使う利点は何ですか?

IronWordは、DOCX文書でのテキストスタイリングに対するプログラム的な制御を提供し、開発者が文書の作成を自動化しながら、Microsoft Wordと同様の一貫したプロフェッショナルなフォーマットを確保できます。

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

もっと読む

準備はできましたか?

NuGetダウンロード数 63,050バージョン:2026.11ちょうどリリースされました