C#で文書要素を操作する方法

C# Document Element Tutorial

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronWordは.NET C#開発者がアプリケーションにWordおよびDOCXドキュメントの作成、読み取り、編集機能を統合する助けとなる強力なWordドキュメントライブラリです。 Wordドキュメントのコンテキストでは、ドキュメントエレメントとはコンテンツを構成する要素です。

クイックスタート: スタイル付き TextRun を作成し、画像を埋め込む

ここでは、 IronWord を使用して、スタイル設定されたテキストとドキュメントの埋め込み画像を組み合わせてリッチ コンテンツを追加する方法について説明します。 この例は、スタイル付きテキスト用の Run ラッパーパターンを示しています。

  1. IronWord をNuGetパッケージマネージャでインストール

    PM > Install-Package IronWord
  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");
  3. 実際の環境でテストするためにデプロイする

    今日プロジェクトで IronWord を使い始めましょう無料トライアル

    arrow pointer

目次

主要概念

ドキュメント階層

IronWordは、次のような構造化されたドキュメント階層を採用しています:Document → Paragraph → Run → TextContent。 この階層を理解することは、スタイル設定されたテキストを操作する上で不可欠です。

  • WordDocument: ドキュメント全体を表す最上位のコンテナ
  • Paragraph: ドキュメント内のコンテンツブロック
  • Run: TextContent を含むラッパーオブジェクトで、スタイルを適用することができます
  • TextContent: 実際のテキスト文字列

段落にコンテンツを追加する

IronWord、段落にコンテンツを追加するための 2 つの方法が用意されています。

  • AddText(TextContent): スタイルが適用されていないプレーンテキストに使用します。 段落に直接 TextContent を追加します。
  • AddChild(Run): スタイル付きテキストに使用します。 段落に Run オブジェクト(TextContent をラップし、スタイル情報を保持する)を追加します。

フォントサイズ、色、太字などの書式設定を適用する必要がある場合は、AddChildを使用して段落に追加してください。

TextRunsを追加

テキストコンテンツ

Split メソッドは、指定された区切り文字に基づいて、テキストの連続したブロックを複数の小さなテキストブロックに分割するために使用されます。 これにより、ドキュメント内のテキスト情報の編成と操作が可能です。

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.cs
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")
$vbLabelText   $csharpLabel

スタイリング設定

テキストの書式設定を行うには、Run ラッパーパターンを使用する必要があります。 Run を含む TextContent オブジェクトを作成し、TextStyleRunTextContent ではない)に割り当てます。 TextStyle を使用すると、FontSize、色、太字、斜体、取り消し線、下線、上付き文字、下付き文字などの視覚的属性を定義できます。

TextFontプロパティ内で設定される点に注意してください。 スタイル設定が完了したら、AddChild を使用して、段落に Run を追加してください。 これは以下のドキュメント階層に従います:Document → Paragraph → Run → TextContent

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.cs
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")
$vbLabelText   $csharpLabel

テキストの塗りつぶし色を取得

IronWordでは、スタイル設定に加え、既存のドキュメントからRGBAの色値を取得し、スタイル全体の一貫性を保つ方法を提供しています。

:path=/static-assets/word/content-code-examples/tutorials/rgba-color-value.cs
using IronWord;
using IronWord.Models;
using System;

// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");

TextContent content = doc.Paragraphs[0].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.Paragraphs(0).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)
$vbLabelText   $csharpLabel

色値を取得するには、WordDocument を使用してドキュメントを読み込み、Paragraphs コレクションおよび Texts 配列にアクセスして、TextContent オブジェクトを取得します。 Color プロパティは、テキストの既存の色である TextContent 値を返すため、ドキュメント全体で一貫したスタイルを維持することができます。

画像を埋め込む

この機能により、コンテンツ内に画像をシームレスに含めることができ、文書の視覚的な魅力とコミュニケーション能力を高めます。

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-embed-images.cs
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(new Paragraph(textRun));

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models

' Load docx
Private doc As New WordDocument()

' Configure image
Private 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(New Paragraph(textRun))

' Export docx
doc.SaveAs("save_document.docx")
$vbLabelText   $csharpLabel

画像を追加

画像を読み込む

画像を読み込むことは重要なプロセスです。 これは、外部画像ファイルをドキュメントに取り込むことを含みます。 画像を読み込む能力により、関連するビジュアルを含め、より魅力的で情報豊富なドキュメントに貢献します。

:path=/static-assets/word/content-code-examples/tutorials/add-image-load-image.cs
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")
$vbLabelText   $csharpLabel

画像を設定する

設定により画像を最適化します。 これには、テキストの折り返し、寸法、位置、角からの距離といったプロパティの設定が含まれます。 適切な設定により、画像が視覚的に心地よく、文脈に適した方法で表示されます。

:path=/static-assets/word/content-code-examples/tutorials/add-image-configure-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
using IronSoftware.Abstractions.Word;

// Load docx
WordDocument doc = new WordDocument();

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.TextWrapBehavior = new TextWrapSquare();
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
Imports IronSoftware.Abstractions.Word

' Load docx
Dim doc As New WordDocument()

' Configure image
Dim image As New ImageContent("image.jpg")
image.TextWrapBehavior = New TextWrapSquare()
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")
$vbLabelText   $csharpLabel

よくある質問

C#でWord文書にテキストを追加するにはどうすればよいですか?

IronWordを使用して、テキストランのインスタンスを作成し、そこにテキストを追加してドキュメント内の段落に付加できます。

Wordドキュメントでテキストを分割する方法は何ですか?

IronWordはSplitメソッドを提供しており、指定されたデリミタに基づいてテキストランをより小さなセグメントに分割し、テキスト操作を容易にします。

IronWordを使用してWordドキュメントでテキストをスタイルするにはどうすればよいですか?

IronWordを使用すると、フォントサイズや色などの属性を設定し、太字、斜体、取り消し線、下線、上付き文字、下付き文字などのスタイルを適用してテキストをスタイルできます。

C#でWordドキュメントに画像を埋め込むにはどうすればよいですか?

IronWordを使用してWordドキュメントに画像を埋め込むには、ファイルから画像を読み込み、ドキュメント内の段落にインライン画像として追加できます。

Wordドキュメントに画像を読み込む手順は?

IronWordを使用すると、ファイルまたはファイルストリームから画像を読み込み、Wordドキュメントに視覚コンテンツを組み込むことができます。

Wordドキュメントで画像のプロパティを構成するにはどうすればよいですか?

IronWordを使用して、テキストの折り返し、寸法、位置オフセット、角からの距離などの画像プロパティを構成し、ドキュメント内で画像を適切に表示することができます。

Word ドキュメント内のテキストの RGBA カラー値を取得できますか?

はい、IronWordを使用すると、ドキュメント内の既存テキストのRGBAカラーバリューを取得でき、一貫したスタイルを維持できます。

Wordドキュメントを操作するためにIronWordの使用を開始するにはどうすればよいですか?

IronWordを使用するために、.NET C#アプリケーションに統合し、その包括的なライブラリ機能を活用してWordドキュメントを作成、読み取り、編集できます。

Iron SoftwareはIronWordにどのようなサポートを提供していますか?

Iron Softwareは、IronWordの包括的なサポートを提供しており、ドキュメント、チュートリアル、および開発者の実装を支援する顧客サポートがあります。

IronWord に無料トライアルはありますか?

はい、Iron SoftwareはIronWordの無料トライアルを提供しており、開発者が購入決定をする前にその機能と可能性を探求できます。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 44,829 | バージョン: 2026.5 just released
Still Scrolling Icon

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

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