IronWordを使い始める
IronWord: .NET のための Word ドキュメント ライブラリ
IronWord は Iron Software によって開発された Word ドキュメント ライブラリです。 IronWord は .NET アプリケーションで Word ドキュメントを扱うための強力な機能を提供します。
- Word および Docx ドキュメントの読み込み、操作、保存。
PageSetup: 用紙サイズ、ページの向き、余白、背景色を構成します。TextRun: テキストの内容、スタイル、分割、テキストの追加、および画像の追加を処理します。TextStyle: フォント ファミリ、サイズ、色、太字、斜体、取り消し線、下線、上付き文字、下付き文字を管理します。Paragraph: テキスト ラン、画像、図形の追加、スタイル、配置、箇条書き、番号付きリストの設定。Table: 行の追加、セル値の取得と設定、行の削除、セルの結合など、テーブル構造を操作します。Image: ファイルまたはストリームから画像を読み込み、テキストの折り返し、位置オフセット、幅、高さ、その他のプロパティを設定します。Shape: テキストの折り返し、位置オフセット、幅、高さ、図形の種類、回転を設定します。
.NET 用 Word 文書 C# ライブラリ
- DOCXドキュメントを処理するためのC#ライブラリをダウンロードする
- Word および DOCX ドキュメントの作成と変更
- 段落、セクション、表などの文書構造を追加する
- テキスト、画像、図形などのドキュメント要素を追加する
- ドキュメント要素を簡単にスタイル設定
インストール
IronWord ライブラリ
IronWordのインストールは迅速かつ簡単です。 次のコマンドを使用して NuGet からパッケージをインストールできます。
Install-Package IronWord
または、公式 IronWord NuGet サイトから直接ダウンロードします。
インストールが完了したら、C# コード ファイルの先頭に using IronWord; を追加して開始できます。
ライセンスキーの適用
次に、ライセンス キーを License クラスの LicenseKey プロパティに割り当てて、有効なライセンス キーまたは試用キーをIronWordに適用します。 インポート文の直後、任意の IronWord メソッドを使用する前に、次のコードを含めてください。
using IronWord;
// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
using IronWord;
// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
Imports IronWord
' Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
コード例
いくつかのコード例と利用可能な機能を探りましょう。
- "ファイル" > "情報"を選択し、"変換"をクリックします。
- ドキュメントが最新のファイル形式にアップグレードされるというメッセージが表示されます。 "OK"をクリックしてください。
Word および Docx ドキュメントの作成
コンストラクターの 1 つを使用して WordDocument クラスのインスタンスを作成し、Word 文書を作成します。 その後、SaveAs メソッドを使用して Word 文書をエクスポートします。 例:
using IronWord;
class Program
{
static void Main()
{
// Create a new Word document
var document = new WordDocument();
// Save the document as a .docx file
document.SaveAs("example.docx");
}
}
using IronWord;
class Program
{
static void Main()
{
// Create a new Word document
var document = new WordDocument();
// Save the document as a .docx file
document.SaveAs("example.docx");
}
}
Imports IronWord
Friend Class Program
Shared Sub Main()
' Create a new Word document
Dim document = New WordDocument()
' Save the document as a .docx file
document.SaveAs("example.docx")
End Sub
End Class
画像の追加
画像はそれ自体では追加できません; 代わりに、TableCell、または Section などのドキュメント構造の 1 つに追加する必要があります。 画像を追加するには、AddImage メソッドを使用します。 例:
using IronWord;
using System.Drawing;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
// Add an image to a paragraph
var paragraph = section.Paragraphs.Add();
paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));
document.SaveAs("example_with_image.docx");
}
}
using IronWord;
using System.Drawing;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
// Add an image to a paragraph
var paragraph = section.Paragraphs.Add();
paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));
document.SaveAs("example_with_image.docx");
}
}
Imports IronWord
Imports System.Drawing
Friend Class Program
Shared Sub Main()
Dim document = New WordDocument()
Dim section = document.Sections.Add()
' Add an image to a paragraph
Dim paragraph = section.Paragraphs.Add()
paragraph.AddImage("path/to/image.jpg", New Rectangle(0, 0, 100, 100))
document.SaveAs("example_with_image.docx")
End Sub
End Class
表の追加
表を追加するには、表、行、列、および表セルの作成が必要です。 これにより、各セルに異なるスタイルを持たせることができるため、重要な構成の機会が得られます。 例:
using IronWord;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
var table = section.Tables.Add(3, 3); // 3x3 table
// Iterate over cells and set their content
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count; j++)
{
table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
}
}
document.SaveAs("example_with_table.docx");
}
}
using IronWord;
class Program
{
static void Main()
{
var document = new WordDocument();
var section = document.Sections.Add();
var table = section.Tables.Add(3, 3); // 3x3 table
// Iterate over cells and set their content
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count; j++)
{
table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
}
}
document.SaveAs("example_with_table.docx");
}
}
Imports IronWord
Friend Class Program
Shared Sub Main()
Dim document = New WordDocument()
Dim section = document.Sections.Add()
Dim table = section.Tables.Add(3, 3) ' 3x3 table
' Iterate over cells and set their content
For i As Integer = 0 To table.Rows.Count - 1
Dim j As Integer = 0
Do While j < table.Rows(i).Cells.Count
table.Rows(i).Cells(j).Paragraphs.Add().AppendText($"Cell {i+1},{j+1}")
j += 1
Loop
Next i
document.SaveAs("example_with_table.docx")
End Sub
End Class
ライセンスとサポートの提供
IronWord は有料ライブラリです; ただし、無料の試用ライセンスはこちらで入手可能です。
Iron Softwareについての詳細は、弊社のウェブサイトをご覧ください:https://ironsoftware.com/。 サポートやお問い合わせについては、チームにお尋ねください。
Iron Software からのサポート
一般的なサポートや技術のお問い合わせは、次のメールアドレスまでご連絡ください:support@ironsoftware.com

