スタイルテキストを追加
IronWord の"スタイルテキストの追加"機能により、開発者は DOCX ドキュメントにコンテンツを追加しながらさまざまなテキストスタイル オプションを適用できます。 これは、フォント ファミリ、サイズ、色、および太字、斜体、下線、取り消し線などのスタイル属性を指定することで、テキストの外観に対する細かい制御を提供します。 開発者は、Run オブジェクトに TextContent を含め、TextStyle を Run に割り当てることで、ドキュメント内の個々のテキスト要素のプレゼンテーションをカスタマイズし、個別の見た目と雰囲気を確保することができます。
この機能は、特定のセクションが異なるスタイルを必要とするレポートや手紙など、プロフェッショナルにフォーマットされたドキュメントを動的に生成するのに便利です。 TextStyle クラスは、これらの属性を簡単に操作でき、同じドキュメント内でシンプルから複雑なスタイリングを可能にします。
重要なポイント
1.スタイルランの作成:
- A
Runobject is created containingTextContentwith the desired text. - The
Styleproperty of theRunis assigned aTextStyleobject to apply formatting.
- TextStyle の設定:
FontSize: Set at theTextStylelevel (not insideFont) to specify text size.TextFont: Contains font properties includingFontFamilyfor font selection.Color: Specifies the text color usingIronWord.Models.Color.IsBoldandIsItalic: Boolean properties for bold and italic formatting.Underline: Adds underline styling to the text.Strike: Applies strikethrough formatting usingStrikeValueenum.
3.ドキュメントへの追加:
- Use
AddChildto add the styledRunto aParagraph. - 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". その結果、指定されたスタイルに従って挿入されたテキストがフォーマットされ、すべてのフォントとフォーマットのプロパティが出力ファイル内で保持されます。

