IRONWORDの使用

.NET Word API(開発者向けの動作方法)

リーガン・パン
リーガン・パン
2024年4月3日
共有:

イントロダクション

.NET Word API は、開発者に対して強力なツールを提供し、Word ドキュメントの変換、MS Word ドキュメントとのやり取り、アプリケーション内での操作を可能にします。 このAPIは、Microsoft Word文書を扱うプロセスを合理化するために設計されており、プログラムによる文書の作成、編集、変換、管理を容易にします。 この記事では、IronWord の機能を探り、Wordドキュメントの操作能力を理解します。

IronWordの紹介

IronWord は、.NET アプリケーションで Microsoft Word ドキュメントを処理する開発者向けに特別に設計された、.NET Word API エコシステム内の .NET Word ライブラリです。 IronWordを使えば、開発者はサーバーやクライアントマシンにマイクロソフト・ワードがインストールされていなくても、簡単にワード文書を読んだり、書いたり、修正したりすることができる。 この機能は、レポートや請求書の作成、メールマージ機能によるパーソナライズされた通信文の作成など、文書処理タスクを自動化する必要があるアプリケーションに特に有益です。

IronWordの特徴

IronWord はWord文書操作の様々な側面に対応する包括的な機能を提供します。 ここでは、「文書構造」と「文書要素」に分類された各機能を、複数の文書の操作や結合をどのように可能にするかに焦点を当てながら、探っていこう。

文書構造

Wordの読み取りと編集: IronWordを使用すると、Wordドキュメントから特定の情報を抽出できます。編集や再利用のためにテキストを抽出したり、他の場所で使用する必要がある画像を取得したりできます。 この機能は、Word文書をマージしたり、既存のDOCXファイルに含まれる情報を処理したりすることを目的としたアプリケーションには不可欠である。

複数のフォーマット: IronWordは幅広いファイルフォーマットをサポートしており、.NETアプリケーション内でのWordドキュメント変換の利便性を高めます。

ページ設定を編集: IronWord を使用すると、Word ドキュメントの物理レイアウトを簡単に調整できます。 さまざまなMS Wordファイルの用紙サイズを標準サイズまたはカスタムサイズに調整したり、文書のセクションごとに向きを変えたり、適切な配置を確保するために余白を設定したり、美的目的のために背景色を変更したり、セクションを強調したりすることもできます。

段落を追加: IronWordは段落内のテキストランの追加や削除を可能にし、大量のテキストの編集やフォーマットに欠かせない機能です。 さらに、画像や図形をテキストに直接挿入したり、デザイン仕様に合わせてスタイルを調整したり、洗練された外観のために整列を設定したりすることで、段落を強化することができます。 また、箇条書きや番号リストを追加する機能も、コンテンツをより効果的に整理するのに役立つ。

using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Create and add styled text to a paragraph
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(new TextRun("Exploring text styles within a document."));
paragraph.AddTextRun(new TextRun("An example in italic.", new TextStyle { IsItalic = true }));
paragraph.AddTextRun(new TextRun("An example in bold.", new TextStyle { IsBold = true }));
// Add paragraph and export docx
doc.AddParagraph(paragraph);
doc.SaveAs("newdocument.docx");
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Create and add styled text to a paragraph
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(new TextRun("Exploring text styles within a document."));
paragraph.AddTextRun(new TextRun("An example in italic.", new TextStyle { IsItalic = true }));
paragraph.AddTextRun(new TextRun("An example in bold.", new TextStyle { IsBold = true }));
// Add paragraph and export docx
doc.AddParagraph(paragraph);
doc.SaveAs("newdocument.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
' Create and add styled text to a paragraph
Private paragraph As New Paragraph()
paragraph.AddTextRun(New TextRun("Exploring text styles within a document."))
paragraph.AddTextRun(New TextRun("An example in italic.", New TextStyle With {.IsItalic = True}))
paragraph.AddTextRun(New TextRun("An example in bold.", New TextStyle With {.IsBold = True}))
' Add paragraph and export docx
doc.AddParagraph(paragraph)
doc.SaveAs("newdocument.docx")
$vbLabelText   $csharpLabel

テーブルの追加テーブルはDOCXファイルの重要な要素であり、IronWordを使用すると簡単に操作でき、動的なドキュメント生成をサポートします。 行や列を追加したり削除したりすることができる。これは、データ量が変化する可能性のあるダイナミック・ドキュメント生成では重要な操作である。 セルの結合と分割により、複雑な表の書式設定にも対応し、ボーダーやレイアウト寸法のカスタマイズにより、洗練されたプロフェッショナルな外観を実現します。

using IronWord;
using IronWord.Models;
// Create a table cell with a paragraph containing text
TableCell cell = new TableCell(new Paragraph(new TextRun("Sample text")));
// Configure a common border style for the table
BorderStyle borderStyle = new BorderStyle
{
    BorderColor = new IronColor(IronSoftware.Drawing.Color.Black),
    BorderValue = IronWord.Models.Enums.BorderValues.Thick,
    BorderSize = 5
};
// Apply the border style to the cell
cell.Borders = new TableBorders
{
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle
};
// Create a table row and add the same cell twice
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);
// Create a table, add the row, then create and export the Word document
Table table = new Table();
table.AddRow(row);
WordDocument doc = new WordDocument(table);
doc.SaveAs("Document.docx");
using IronWord;
using IronWord.Models;
// Create a table cell with a paragraph containing text
TableCell cell = new TableCell(new Paragraph(new TextRun("Sample text")));
// Configure a common border style for the table
BorderStyle borderStyle = new BorderStyle
{
    BorderColor = new IronColor(IronSoftware.Drawing.Color.Black),
    BorderValue = IronWord.Models.Enums.BorderValues.Thick,
    BorderSize = 5
};
// Apply the border style to the cell
cell.Borders = new TableBorders
{
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle
};
// Create a table row and add the same cell twice
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);
// Create a table, add the row, then create and export the Word document
Table table = new Table();
table.AddRow(row);
WordDocument doc = new WordDocument(table);
doc.SaveAs("Document.docx");
Imports IronWord
Imports IronWord.Models
' Create a table cell with a paragraph containing text
Private cell As New TableCell(New Paragraph(New TextRun("Sample text")))
' Configure a common border style for the table
Private borderStyle As New BorderStyle With {
	.BorderColor = New IronColor(IronSoftware.Drawing.Color.Black),
	.BorderValue = IronWord.Models.Enums.BorderValues.Thick,
	.BorderSize = 5
}
' Apply the border style to the cell
cell.Borders = New TableBorders With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}
' Create a table row and add the same cell twice
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)
' Create a table, add the row, then create and export the Word document
Dim table As New Table()
table.AddRow(row)
Dim doc As New WordDocument(table)
doc.SaveAs("Document.docx")
$vbLabelText   $csharpLabel

.NET Word API(開発者向けの仕組み):図1 - テーブルを含む出力されたPDF

ドキュメント要素

テキストランを追加: この機能は、テキストコンテンツに対する詳細な制御に関するものです。 テキストランの追加、追加、分割ができるので、ダイナミックな文書作成には欠かせません。 フォントファミリー、サイズ、色の変更、太字、斜体、その他のテキスト装飾の追加など、スタイリングオプションは多岐にわたる。 また、テキストランの中に画像を埋め込むこともでき、リッチで視覚的に魅力的な文書を作成できます。

画像を追加: IronWord は、Word ドキュメント内での画像操作を可能にします。 さまざまなソースから画像を読み込み、シームレスにテキストを回り込ませ、レイアウトに合わせてサイズを調整できます。 位置のオフセットとドキュメントのコーナーからの距離を設定できるので、画像は常に完璧に配置されます。

using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
IronWord.Models.Image image = new IronWord.Models.Image("your-image.jpg")
{
    Width = 200, // In unit pixel
    Height = 200 // In unit pixel
};
Create paragraph, add image, add paragraph to doc, and export
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image);
doc.AddParagraph(paragraph);
doc.SaveAs("save_document.docx");
using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
IronWord.Models.Image image = new IronWord.Models.Image("your-image.jpg")
{
    Width = 200, // In unit pixel
    Height = 200 // In unit pixel
};
Create paragraph, add image, add paragraph to doc, and export
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image);
doc.AddParagraph(paragraph);
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
Private image As New IronWord.Models.Image("your-image.jpg") With {
	.Width = 200,
	.Height = 200
}
Private paragraph, add, add, [and] As Create
paragraph.AddImage(image)
doc.AddParagraph(paragraph)
doc.SaveAs("save_document.docx")
$vbLabelText   $csharpLabel

図形の追加: 図形はドキュメントに大きな視覚的な影響を与えることができ、IronWordはそれを挿入しカスタマイズするための精度の高いツールを提供します。 図形のタイプ(長方形、円、矢印など)を設定し、テキストが図形の周りにどのように回り込むかを決定し、正確な寸法と位置を指定し、さらに視覚効果を達成するために図形を回転させることができます。

互換性

.NET バージョンとプロジェクトの種類

.NET Word API (開発者向けの仕組み): 図2 - IronWordが対応する.NETバージョンとプロジェクトタイプ

IronWordは.NETエコシステム内で幅広い互換性を持つように設計されており、.NETコア、.NETスタンダード、.NETフレームワークなど、さまざまな.NETバージョンでC#、VB.NET、F#をサポートしています。 これにより、現代的なアプリケーションでもレガシー・アプリケーションでも、その有用性が保証される。 Blazor、WebForms、Xamarin、MAUI、WPF、コンソール・アプリケーションとの統合により、Web、モバイル、デスクトップ・アプリケーションに対応します。

アプリ環境

.NET Word API(開発者のための仕組み):図3 - IronWordが動作できるアプリ環境

アプリケーション環境に関しては、IronWordはWindows、Linux、iOS、Androidの各プラットフォームに対応し、Docker、Azure、AWSでのコンテナ化やクラウド展開もサポートしている。 この幅広いサポートにより、さまざまな環境での開発が容易になる。

OSとIDE

.NET Word API(開発者向けの動作方法):図4 - IronWordが互換性のあるOSとIDE

IronWordは、Microsoft Visual Studio、ReSharper、Riderなどの主要な統合開発環境(IDE)とも互換性があり、開発者にツールの選択における柔軟性を提供します。 最後に、さまざまなオペレーティングシステムとプロセッサアーキテクチャ(x64、x86、ARM)をサポートしており、多様なハードウェア構成にわたって効率的なパフォーマンスを保証します。

ライセンス・オプション

.NET Word API(開発者向けの仕組み):図5 - IronWord ライセンシングページ

IronWordは、さまざまな開発者や組織のニーズに対応するために、さまざまなライセンスオプションを提供しています。 永久ライセンスを提供しているため、支払いは一度だけで、継続費用はかからない。 すべてのライセンスには、1年間の製品サポートとアップデートが含まれています。 ライセンシングの段階は、開発者の数、場所、プロジェクトの数に基づいて設計されています。 無料トライアルも利用でき、ライセンスを購入する前に実際に体験することができます。

Lite License

このオプションは、プロジェクトに単独で取り組む個人開発者向けに調整されている。 $749で価格設定されており、1か所の1名の開発者を対象としています。

Plus License

小規模チームを対象としたこのライセンスは1,499ドルで、最大3人の開発者を収容し、3つのプロジェクトで3つの場所で使用できる。

Professional License

大規模チーム向けのプロフェッショナル・ライセンスは2,999ドルで、最大10人の開発者をサポートする。 より大規模な業務に対応するように設計されており、プレミアムサポート機能が含まれている。

結論

まとめとして、 IronWord は、個々の開発者やチームの多様なニーズに応えるために、さまざまなライセンスオプションを提供する堅牢で柔軟な .NET の Word API として浮上しています。 その機能は、Word文書の効率的な管理と操作を可能にし、複数の.NETバージョンとプロジェクトタイプにまたがる互換性を保証します。

リーガン・パン
ソフトウェアエンジニア
レーガンはリーディング大学で電子工学の学士号を取得しました。Iron Softwareに入社する前の仕事では、一つのタスクに集中して取り組んでいました。Iron Softwareでは、営業、技術サポート、製品開発、マーケティングのいずれにおいても広範な業務に携わることが最も楽しいと感じています。彼は、Iron Softwareライブラリを開発者がどのように使用しているかを理解し、その知識を使ってドキュメントを継続的に改善し、製品を開発することを楽しんでいます。
< 以前
C#でドキュメントをWordにエクスポートする方法
次へ >
C# で Word 自動化を実行する方法