IronWord を始める

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

IronWord.NET用Word文書ライブラリ

IronWordは、Iron Softwareによって開発されたWordドキュメントライブラリです。 IronWord は、.NETアプリケーションでWord文書を操作するための堅牢な機能を提供することに優れています。

  • WordおよびDocxドキュメントの読み込み、操作、保存。
  • ページ設定:用紙サイズ、ページの向き、余白、背景色を設定します。
  • TextRun:テキストコンテンツ、スタイル、分割、テキストの追加、画像の追加を扱う。
  • TextStyle:フォントファミリ、サイズ、色、太字、斜体、取り消し線、下線、上付き文字、下付き文字の管理。
  • 段落:テキストラン、画像、図形の追加、スタイルの設定、整列、箇条書き、番号リストの追加。
  • 表:行の追加、セル値の取得と設定、行の削除、セルの結合など、テーブル構造の操作。
  • 画像:ファイルまたはストリームから画像を読み込み、ラップテキスト、位置オフセット、幅、高さ、およびその他のプロパティを設定します。
  • 形状:ラップテキスト、位置オフセット、幅、高さ、形状タイプ、回転の設定。

インストール

IronWord・ライブラリー

IronWord のインストールは簡単で、このようにパッケージをインストールしてください:

Install-Package IronWord

あるいは、公式IronWord NuGetサイトから直接ダウンロードしてください。

インストールが完了したら、C#コードの冒頭にusing IronWord;を追加して始めることができます。

ライセンスキーの適用

次に、License クラスの LicenseKey プロパティにライセンスキーを割り当てることで、IronWord に有効なライセンスまたは試用キーを適用します。 IronWord のメソッドを使用する前に、import文の直後に以下のコードを記述してください:

:path=/static-assets/word/content-code-examples/get-started/get-started-license.cs
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01";
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01"
$vbLabelText   $csharpLabel

コード例

いくつかのコード例と利用可能な機能を探ってみましょう。

次の内容にご注意ください。

IronWordによって生成されたDOCXファイルは、特定のバージョンのMicrosoft Wordで開くと互換モードで表示され、一部のスタイルが使用できない場合があります。 互換モードを解除してWord文書を変換するには:

  1. ファイル'> '情報'を選択し、 "変換 "をクリックします。

  2. 文書が最新のファイル形式にアップグレードされるというメッセージが表示されます。 OK "をクリックする。

WordおよびDocxドキュメントの作成

WordDocument クラスのコンストラクターの1つを使用してWordドキュメントを作成します。 その後、SaveAs メソッドを使用して Word ドキュメントをエクスポートします。

:path=/static-assets/word/content-code-examples/get-started/get-started-1.cs
using IronWord;
using IronWord.Models;

// Create textrun
TextContent textRun = new TextContent("Sample text");

Paragraph paragraph = new Paragraph();
paragraph.AddText(textRun);

// Create a new Word document
WordDocument doc = new WordDocument(paragraph);

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

' Create textrun
Private textRun As New TextContent("Sample text")

Private paragraph As New Paragraph()
paragraph.AddText(textRun)

' Create a new Word document
Dim doc As New WordDocument(paragraph)

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

画像を追加

画像は単独では追加できない; 代わりに、それはParagraphTableCell、またはSectionのようなドキュメント構造の一部に追加されるべきです。 AddImage メソッドを使用して画像を追加します。

:path=/static-assets/word/content-code-examples/get-started/get-started-2.cs
using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument("document.docx");

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 250; // In unit pixel
image.Height = 200; // In unit pixel
Paragraph paragraph = new Paragraph();

// Add image
paragraph.AddImage(image);

// Add paragraph
doc.AddParagraph(paragraph);

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

' Load docx
Private doc As New WordDocument("document.docx")

' Configure image
Private image As New ImageContent("image.jpg")
image.Width = 250 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim paragraph As New Paragraph()

' Add image
paragraph.AddImage(image)

' Add paragraph
doc.AddParagraph(paragraph)

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

テーブルを追加

表を追加するには、表、行、列、表のセルを作成しなければならないので、もう少し手間がかかる。 しかし、このセットアップの場合、コンフィギュレーションに大きなチャンスがある。 各セルは異なるスタイルを持つことができる。 様々なボーダー・スタイル、24種類の豊富なセレクションをご覧ください。

:path=/static-assets/word/content-code-examples/get-started/get-started-3.cs
using IronWord;
using IronWord.Models;

// Create table cell
TableCell cell = new TableCell();

TextContent textRun = new TextContent();
textRun.Text = "Sample text";

// Add textrun to the cell
cell.AddChild(new Paragraph(textRun));

// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = Color.Black;
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;

// Configure table border
TableBorders tableBorders = new TableBorders() {
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};

cell.Borders = tableBorders;

// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);

// Create table and add row
Table table = new Table();
table.AddRow(row);

// Create new Word document from the table
WordDocument doc = new WordDocument(table);

// Export Word document
doc.SaveAs("Document.docx");
Imports IronWord
Imports IronWord.Models

' Create table cell
Private cell As New TableCell()

Private textRun As New TextContent()
textRun.Text = "Sample text"

' Add textrun to the cell
cell.AddChild(New Paragraph(textRun))

' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = Color.Black
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick
borderStyle.BorderSize = 5

' Configure table border
Dim tableBorders As New TableBorders() With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}

cell.Borders = tableBorders

' Create row and add cell
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)

' Create table and add row
Dim table As New Table()
table.AddRow(row)

' Create new Word document from the table
Dim doc As New WordDocument(table)

' Export Word document
doc.SaveAs("Document.docx")
$vbLabelText   $csharpLabel

ライセンスおよびサポート利用可能

IronWordは有料ライブラリですが、こちらで無料トライアルライセンスも利用可能です。

Iron Softwareに関する詳細情報は、当社のウェブサイトhttps://ironsoftware.com/をご覧ください。サポートやお問い合わせについては、当社のチームにご相談ください

Iron Softwareのサポート

一般的なサポートや技術的なお問い合わせは、電子メールでご連絡ください:support@ironsoftware.com