スタイルテキストを追加

IronWord の"スタイルテキストの追加"機能により、開発者は DOCX ドキュメントにコンテンツを追加しながらさまざまなテキストスタイル オプションを適用できます。 これは、フォント ファミリ、サイズ、色、および太字、斜体、下線、取り消し線などのスタイル属性を指定することで、テキストの外観に対する細かい制御を提供します。 開発者は、Run オブジェクトに TextContent を含め、TextStyleRun に割り当てることで、ドキュメント内の個々のテキスト要素のプレゼンテーションをカスタマイズし、個別の見た目と雰囲気を確保することができます。

この機能は、特定のセクションが異なるスタイルを必要とするレポートや手紙など、プロフェッショナルにフォーマットされたドキュメントを動的に生成するのに便利です。 TextStyle クラスは、これらの属性を簡単に操作でき、同じドキュメント内でシンプルから複雑なスタイリングを可能にします。

重要なポイント

1.スタイルランの作成:

  • A Run object is created containing TextContent with the desired text.
  • The Style property of the Run is assigned a TextStyle object to apply formatting.
  1. TextStyle の設定:
    • FontSize: Set at the TextStyle level (not inside Font) to specify text size.
    • TextFont: Contains font properties including FontFamily for font selection.
    • Color: Specifies the text color using IronWord.Models.Color.
    • IsBold and IsItalic: Boolean properties for bold and italic formatting.
    • Underline: Adds underline styling to the text.
    • Strike: Applies strikethrough formatting using StrikeValue enum.

3.ドキュメントへの追加:

  • Use AddChild to add the styled Run to a Paragraph.
  • The paragraph is then added to the document with AddParagraph.

コードの説明

このコードは、IronWord を使用して DOCX ドキュメント内のテキストを作成およびスタイル設定する方法を示しています。 It begins by initializing a new WordDocument object, representing the document to be generated. A Run object is created containing TextContent with the string "Styled text example" and a TextStyle is applied to the Run to configure the appearance of the text.

The TextStyle includes settings for font size set at the TextStyle level (not inside Font), font family configured via TextFont, text color, and bold formatting. これらの設定は、最終的なドキュメントでテキストがどのように表示されるかをカスタマイズします。

After the Run is styled, the AddChild method adds the Run object to a paragraph in the document. このメソッドは、スタイル付きコンテンツを適切な形式で Word ドキュメントに挿入します。 Finally, the SaveAs method is called to export the document as "styled_document.docx". その結果、指定されたスタイルに従って挿入されたテキストがフォーマットされ、すべてのフォントとフォーマットのプロパティが出力ファイル内で保持されます。

ドキュメント要素のチュートリアルで、スタイリングのヒントをさらにご覧ください。

準備はできましたか?
Nuget ダウンロード 36,374 | バージョン: 2026.3 リリース
Still Scrolling Icon

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

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