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ドキュメントにプロフェッショナルなテキストフォーマットを適用することを可能にします。フォント、色、太字、斜体、下線などを含みます。 レポートの作成、テンプレートの作成、文書作成の自動化など、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ラッパーパターンを使用する必要があります。 最初にRunオブジェクトを作成します。 プロパティTextStyleを適用します。

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

ご注意IronWordの文書階層は構造に従います:文書 → 文書セクション → 段落 → 実行 → テキストコンテンツ。 スタイルは、実行レベルで適用され、テキストコンテンツには直接適用されません。

: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");
$vbLabelText   $csharpLabel

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

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

TextFontプロパティ内で設定されます。 TextContentをラップし、IronWordの文書階層パターンに従ってスタイルを保持します。 この例は、Runに適用されたときに複数のスタイリングプロパティがどのように組み合わさって豊かにフォーマットされたテキストを作成するかを示しています。


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

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

IronWord.Models.Colorからの定義済みの色やカスタムの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");
$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");
$vbLabelText   $csharpLabel

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

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

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

trueに設定してテキストを太字にします。 太字は一般的に、見出し、強調、重要な情報の強調に使用されます。 他のスタイリングプロパティと組み合わせることで、太字テキストは視覚的な階層を作り出し、読みやすさを向上させます。

: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");
$vbLabelText   $csharpLabel

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

これは太字です!

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

trueに設定して斜体スタイルを適用します。 斜体のテキストは通常、強調、タイトル、外国語、または技術用語に使用されます。 この微妙な書式設定は、太字書式の視覚的な重みを伴わずにテキスト要素を区別します。

: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");
$vbLabelText   $csharpLabel

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

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

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

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`に設定するとテキストが太字になり、見出しや強調によく使用されます。 `textRun.Style.IsBold = true;`
`IsItalic` `true`に設定すると、テキストに斜体スタイルが適用されます。通常は強調やタイトルに使用されます。 `textRun.Style.IsItalic = true;`
`アンダーライン` さまざまな下線スタイルを持つ **`アンダーライン`オブジェクト** を使用して、テキストに下線を追加します。 `textRun.Style.Underline = new Underline();`
`ストライク` **`StrikeValue`列挙型**(Strikeまたは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");
$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 ダウンロード 36,374 | バージョン: 2026.3 リリース
Still Scrolling Icon

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

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