C#ドキュメント要素チュートリアル
IronWordは.NET C#開発者がアプリケーションにWordおよびDOCXドキュメントの作成、読み取り、編集機能を統合する助けとなる強力なWordドキュメントライブラリです。 Wordドキュメントのコンテキストでは、ドキュメントエレメントとはコンテンツを構成する要素です。
クイックスタート: スタイル付き TextRun を作成し、画像を埋め込むIronWord を使用して、スタイル付きテキストと埋め込み画像を組み合わせたリッチコンテンツをドキュメントに追加する方法です。 この例は、Run ラッパーパターンを利用したスタイル付きテキストを示しています。
-
1IronWord を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
using IronWord; using IronWord.Models; // Create document WordDocument doc = new WordDocument(); // Create styled Run Run textRun = new Run(new TextContent("Hello IronWord!")); textRun.Style = new TextStyle() { IsBold = true, Color = Color.Blue }; // Create paragraph and add Run Paragraph paragraph = new Paragraph(); paragraph.AddChild(textRun); paragraph.AddImage(new ImageContent("pic.png")); // Add to document and save doc.AddParagraph(paragraph); doc.SaveAs("output.docx");Imports IronWord Imports IronWord.Models ' Create document Dim doc As New WordDocument() ' Create styled Run Dim textRun As New Run(New TextContent("Hello IronWord!")) textRun.Style = New TextStyle() With {.IsBold = True, .Color = Color.Blue} ' Create paragraph and add Run Dim paragraph As New Paragraph() paragraph.AddChild(textRun) paragraph.AddImage(New ImageContent("pic.png")) ' Add to document and save doc.AddParagraph(paragraph) doc.SaveAs("output.docx")Imports IronWord Imports IronWord.Models ' Create document Dim doc As New WordDocument() ' Create styled Run Dim textRun As New Run(New TextContent("Hello IronWord!")) textRun.Style = New TextStyle() With {.IsBold = True, .Color = Color.Blue} ' Create paragraph and add Run Dim paragraph As New Paragraph() paragraph.AddChild(textRun) paragraph.AddImage(New ImageContent("pic.png")) ' Add to document and save doc.AddParagraph(paragraph) doc.SaveAs("output.docx") -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronWordを使い始めるには、無料トライアルをお試しください。
目次
- テキストを追加
- 画像を追加
- 画像を読み込む (ファイルとファイルストリーム)
- テキストの折り返し設定
- 寸法を設定 (幅と高さ)
- 位置オフセットの設定
- コーナーからの距離設定
主要概念
ドキュメント階層
IronWord は、構造化されたドキュメント階層を維持します:Document → Paragraph → Run → TextContent。 この階層を理解することは、スタイル設定されたテキストを操作する上で不可欠です。
WordDocument:ドキュメント全体を表すトップレベルコンテナParagraph:ドキュメント内のコンテンツブロックRun:TextContentを含み、スタイリングを適用できるラッパーオブジェクトですTextContent:実際のテキスト文字列
段落にコンテンツを追加する
IronWord、段落にコンテンツを追加するための 2 つの方法が用意されています。
AddText(TextContent):スタイルのないプレーンテキストに使用。 段落にTextContentを直接追加します。AddChild(Run):スタイル付きテキストに使用。 段落にRunオブジェクト(TextContentをラップし、スタイリングを保持する)を追加します。
フォントサイズ、色、太字フォーマットのようなスタイリングを適用する必要がある場合、Runオブジェクトを作成し、TextStyleをRunに割り当て、AddChildを使用して段落に追加します。
TextRunsを追加
テキストコンテンツ
Split メソッドは、指定された区切り文字に基づいて、テキストランを小さなテキストランのリストに分割するために利用されます。 これにより、ドキュメント内のテキスト情報の編成と操作が可能です。
using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
// Add text
TextContent addText = new TextContent("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));
// Append text
TextContent appendText = new TextContent("The first text.");
appendText.Append(new TextContent("The second text."));
doc.AddParagraph(new Paragraph(appendText));
// Split text
TextContent splitText = new TextContent("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));
// Export docx
doc.SaveAs("textrun.docx");using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
// Add text
TextContent addText = new TextContent("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));
// Append text
TextContent appendText = new TextContent("The first text.");
appendText.Append(new TextContent("The second text."));
doc.AddParagraph(new Paragraph(appendText));
// Split text
TextContent splitText = new TextContent("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));
// Export docx
doc.SaveAs("textrun.docx");Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
' Add text
Private addText As New TextContent("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))
' Append text
Dim appendText As New TextContent("The first text.")
appendText.Append(New TextContent("The second text."))
doc.AddParagraph(New Paragraph(appendText))
' Split text
Dim splitText As New TextContent("Use split to split the sentence.")
splitText.Split(" ")
doc.AddParagraph(New Paragraph(splitText))
' Export docx
doc.SaveAs("textrun.docx")Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
' Add text
Private addText As New TextContent("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))
' Append text
Dim appendText As New TextContent("The first text.")
appendText.Append(New TextContent("The second text."))
doc.AddParagraph(New Paragraph(appendText))
' Split text
Dim splitText As New TextContent("Use split to split the sentence.")
splitText.Split(" ")
doc.AddParagraph(New Paragraph(splitText))
' Export docx
doc.SaveAs("textrun.docx")スタイリング設定
テキストのスタイル設定には、Run ラッパーパターンを使用する必要があります。 Runオブジェクトを作成し、TextContentを含めた上で、TextStyleをRunに割り当てます(TextContentではありません)。 TextStyle により、FontSize、色、太字、イタリック、取り消し線、下線、上付き、下付きといった視覚属性を定義できます。
FontSize はTextStyle レベルで設定され、FontFamily はTextFontプロパティの内側に設定されていることに注意してください。 スタイルが適用されたら、AddChildを使用してRunを段落に追加します。 これはドキュメント階層に従います:Document → Paragraph → Run → TextContent。
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
FontSize = 72,
TextFont = new Font()
{
FontFamily = "Caveat"
},
Color = Color.Red,
IsBold = true,
IsItalic = true,
Underline = new Underline(),
Strike = StrikeValue.Strike,
};
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddChild(textRun);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
FontSize = 72,
TextFont = new Font()
{
FontFamily = "Caveat"
},
Color = Color.Red,
IsBold = true,
IsItalic = true,
Underline = new Underline(),
Strike = StrikeValue.Strike,
};
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddChild(textRun);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
.FontSize = 72,
.TextFont = New Font() With {
.FontFamily = "Caveat"
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddChild(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
.FontSize = 72,
.TextFont = New Font() With {
.FontFamily = "Caveat"
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddChild(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")テキストの塗りつぶし色を取得
スタイル設定以外にも、IronWord は既存のドキュメントからRGBAカラー値を取得する方法を提供し、スタイル全体の一貫性を維持します。
using IronWord;
using IronWord.Models;
using System;
// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");
TextContent content = doc.Texts[0];
// This will show the R G B A of the themecolor
var filledColor = content.Color;
// Print the filled color variable to the console
Console.WriteLine(filledColor);using IronWord;
using IronWord.Models;
using System;
// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");
TextContent content = doc.Texts[0];
// This will show the R G B A of the themecolor
var filledColor = content.Color;
// Print the filled color variable to the console
Console.WriteLine(filledColor);Imports IronWord
Imports IronWord.Models
Imports System
' Open existing Word
Dim doc As New WordDocument("Accent1TextThemcolor.docx")
Dim content As TextContent = doc.Texts(0)
' This will show the R G B A of the themecolor
Dim filledColor = content.Color
' Print the filled color variable to the console
Console.WriteLine(filledColor)Imports IronWord
Imports IronWord.Models
Imports System
' Open existing Word
Dim doc As New WordDocument("Accent1TextThemcolor.docx")
Dim content As TextContent = doc.Texts(0)
' This will show the R G B A of the themecolor
Dim filledColor = content.Color
' Print the filled color variable to the console
Console.WriteLine(filledColor)色の値を取得するには、WordDocumentを使用してドキュメントを読み込み、TextsコレクションにアクセスしてTextContentオブジェクトを取得します。 Color のプロパティは TextContentのテキストに既存の色のRGBA値を返し、ドキュメント全体で一貫したスタイリングを維持することを可能にします。
画像を埋め込む
この機能により、コンテンツ内に画像を含めることができ、ドキュメント全体の視覚的魅力とコミュニケーション力が向上します。
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
TextContent textRun = new TextContent();
// Add image
Paragraph para = new Paragraph(textRun);
para.AddImage(image);
// Add paragraph
doc.AddParagraph(para);
// Export docx
doc.SaveAs("save_document.docx");using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
TextContent textRun = new TextContent();
// Add image
Paragraph para = new Paragraph(textRun);
para.AddImage(image);
// Add paragraph
doc.AddParagraph(para);
// Export docx
doc.SaveAs("save_document.docx");Imports IronWord
Imports IronWord.Models
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New TextContent()
' Add image
Dim para As New Paragraph(textRun)
para.AddImage(image)
' Add paragraph
doc.AddParagraph(para)
' Export docx
doc.SaveAs("save_document.docx")Imports IronWord
Imports IronWord.Models
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New TextContent()
' Add image
Dim para As New Paragraph(textRun)
para.AddImage(image)
' Add paragraph
doc.AddParagraph(para)
' Export docx
doc.SaveAs("save_document.docx")画像を追加
画像を読み込む
画像を読み込むことは重要なプロセスです。 これは、外部画像ファイルをドキュメントに取り込むことを含みます。 画像を読み込む能力により、関連するビジュアルを含め、より魅力的で情報豊富なドキュメントに貢献します。
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage("image.jpg");
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage("image.jpg");
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
Private paragraph As New Paragraph()
' Add image
paragraph.AddImage("image.jpg")
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
Private paragraph As New Paragraph()
' Add image
paragraph.AddImage("image.jpg")
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")画像を設定する
設定により画像を最適化します。 これには、寸法、位置、コーナーからの距離といったプロパティの設定が含まれます。 適切な設定により、画像が視覚的に心地よく、文脈に適した方法で表示されます。
TextWrapBehaviorプロパティ(タイプITextWrapBehavior)も公開しますが、現在のところ値を構成するための公開クラスやファクトリーは文書化されていないため、この例は製品チームの確認待ちで省略されています。using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 100;
image.Height = 100;
image.DistanceFromTop = 50;
var position = new ElementPosition();
position.X = 50;
position.Y = 50;
image.Position = position;
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 100;
image.Height = 100;
image.DistanceFromTop = 50;
var position = new ElementPosition();
position.X = 50;
position.Y = 50;
image.Position = position;
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position As New ElementPosition()
position.X = 50
position.Y = 50
image.Position = position
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position As New ElementPosition()
position.X = 50
position.Y = 50
image.Position = position
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")よくある質問
IronWordはC#開発で何に使われますか?
IronWordは.NET C#開発者向けに設計された強力なライブラリで、アプリケーション内でWordやDOCXドキュメントの作成、読み取り、編集を支援します。
IronWordを使用してWordドキュメントにスタイル付きテキストを追加するにはどうすればよいですか?
スタイル付きテキストを追加するには、'TextContent'をラッピングする'Run'オブジェクトを作成し、'TextStyle'を適用してから、IronWordの'AddChild'メソッドを使用して段落に追加します。
IronWordにおける'Run'オブジェクトの目的は何ですか?
IronWordの'Run'オブジェクトは、'TextContent'のラッパーとして機能し、フォントサイズや色、その他の属性を設定することでテキストにスタイルを適用できるようにします。
IronWordを使ってドキュメントに画像を埋め込むにはどうすればよいですか?
画像を埋め込むには、'ImageContent'オブジェクトを作成し、'AddImage'メソッドを使って段落に追加します。これによりドキュメントに画像を挿入できます。
IronWordで画像を設定する手順はどのようなものですか?
画像を設定するには、寸法や位置、角からの距離などのプロパティを設定し、ドキュメント内で画像が正しく表示されるようにします。
IronWordは既存のWordドキュメントから色の値を取得できますか?
はい、IronWordは既存のドキュメントから'Color'プロパティを通じてRGBAの色値を取得し、スタイルの一貫性を維持するのに役立ちます。
IronWordはどのような方法でテキストランを分割しますか?
IronWordは'Split'メソッドを使用して、指定されたデリミタに基づいてテキストランを小さなランに分割し、テキストコンテンツの整理や操作を容易にします。
IronWordにおいて文書構造を理解することが重要なのはなぜですか?
IronWordを使用してWordドキュメント内のコンテンツを効果的に管理しスタイリングするためには、文書構造(ドキュメント → 段落 → ラン → TextContent)を理解することが重要です。
IronWordで作成したWordドキュメントを保存するにはどうすればよいですか?
IronWordで作成したWordドキュメントは、'SaveAs'メソッドを使用して、保存するファイル名とパスを指定することで保存できます。
IronWordにおける'AddParagraph'メソッドの機能は何ですか?
IronWordの'AddParagraph'メソッドは、テキストや画像を含む完全に構成された段落をWordドキュメントに追加するために使用されます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。