フッターコンテンツにスキップ
IRONWORDの使用方法

VS 2022 プログラムで新しいワード文書を作成する(チュートリアル)

デジタル時代において、文書はますますデジタル化しています。 ビジネス環境における文書は、しばしばMicrosoft Wordとその文書ベースのWordファイルに関連付けられています。Microsoft Word文書の作成と編集は、大規模な組織での情報伝達の基盤となっています。 しかし、手動での文書作成や編集は苦痛なプロセスになることがあります; Word生成をプログラム的に自動化することも容易ではありません。多くのオープンソースライブラリはMicrosoft Office Wordに依存しているためです。

しかし、Word文書の管理や作成は、そこまで難しくある必要はありません。 IronWordはMicrosoft Office Wordに依存しないC#ワードライブラリで、ユーザーは文書全体をカスタマイズでき、プログラム的に文書を生成し、文書テンプレートを作成することができます。

今日のチュートリアルでは、IronWordを使用してMicrosoft Word文書をプログラムで作成する方法を簡単に説明し、簡単な例を提供します。

IronWord: A C# Word Library

IronWordは非常に信頼性が高く使いやすいC# Docxライブラリで、開発者がC#を使用してWord文書を作成および修正することができ、従来の依存関係、つまりMicrosoft OfficeやWord Interopを必要としません。さらに、広範なドキュメントを持ち、.NET 8, 7, 6, Framework, Core, Azureに完全対応しており、ほとんどのアプリケーションに普遍的に互換性があります。 この柔軟性は、取り扱うどんなアプリケーションに対しても優れた選択肢となります。

環境を設定する

この例では、Visual Studioを使用してコンソールアプリを作成し、IronWordライブラリを使用して空白のWord文書を作成し、それを操作する方法を紹介します。 この次のステップを進める前に、Visual Studioがインストールされていることを確認してください。

まず、新しいコンソールアプリを作成しましょう。

次に、以下に示すようにプロジェクト名を提供し、保存場所を指定します。

最後に、希望のフレームワークを選択してください。

C# Word Libraryのインストール

空白のコンソールアプリを作成した後、NuGetパッケージマネージャーを通じてIronWordをダウンロードします。

「NuGetパッケージの管理」をクリックし、「参照」タブでIronWordを検索します。

その後、新しく作成したプロジェクトにインストールします。

または、以下のコマンドをコマンドラインで入力してIronWordをインストールすることもできます。

Install-Package IronWord

すべての準備が完了したら、Word文書を作成する例に進みましょう。

ライセンスキー

IronWord の操作にはライセンス キーが必要であることを忘れないでください。 このリンクから無料トライアルの一部としてキーを取得できます。

// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
' Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"
$vbLabelText   $csharpLabel

トライアル キーを受け取った後、プロジェクト内でこの変数を設定します。

新しいWord文書を作成する

using IronWord;
using IronWord.Models;

class Program
{
    static void Main()
    {
        // Create a textrun
        Text textRun = new Text("Sample text");
        Paragraph paragraph = new Paragraph();
        paragraph.AddChild(textRun);

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

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

class Program
{
    static void Main()
    {
        // Create a textrun
        Text textRun = new Text("Sample text");
        Paragraph paragraph = new Paragraph();
        paragraph.AddChild(textRun);

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

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

Friend Class Program
	Shared Sub Main()
		' Create a textrun
		Dim textRun As New Text("Sample text")
		Dim paragraph As New Paragraph()
		paragraph.AddChild(textRun)

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

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

VS 2022プログラムによる新しいWord文書作成(チュートリアル):上記の例の出力

説明

  1. まずは、サンプルコードでIronWordライブラリとそのモデルをインポートします。
  2. 新しいText変数を作成します。 この変数を使用して、Word文書にテキストを追加できます。
  3. 次に、textRunParagraphに追加します。 これにより、2つを区別してテキストを簡単に操作できます。
  4. Paragraphパラメータを新しいWordDocumentクラスに渡して、新しい文書オブジェクトを作成します。
  5. 最後に、文書を「document.docx」としてWordファイルに保存します。

前例では、基本的なテキストを含むWord文書を作成しました。 より高度な機能、例えばテキストのカスタマイズやテキスト効果の追加を行った例を行いましょう。

using IronWord;
using IronWord.Models;

class Program
{
    static void Main()
    {
        // Ensure to set up the license key
        IronWord.License.LicenseKey = "YOUR-KEY";

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

        // Create sample texts with styles
        Text introText = new Text("This is an example paragraph with italic and bold styling.");
        TextStyle italicStyle = new TextStyle()
        {
            IsItalic = true
        };
        Text italicText = new Text("Italic example sentence.");
        italicText.Style = italicStyle;

        TextStyle boldStyle = new TextStyle()
        {
            IsBold = true
        };
        Text boldText = new Text("Bold example sentence.");
        boldText.Style = boldStyle;

        // Create a paragraph and add texts
        Paragraph paragraph = new Paragraph();
        paragraph.AddText(introText);
        paragraph.AddText(italicText);
        paragraph.AddText(boldText);

        // Add paragraph to the document
        doc.AddParagraph(paragraph);

        // Export the document
        doc.SaveAs("save_document.docx");
    }
}
using IronWord;
using IronWord.Models;

class Program
{
    static void Main()
    {
        // Ensure to set up the license key
        IronWord.License.LicenseKey = "YOUR-KEY";

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

        // Create sample texts with styles
        Text introText = new Text("This is an example paragraph with italic and bold styling.");
        TextStyle italicStyle = new TextStyle()
        {
            IsItalic = true
        };
        Text italicText = new Text("Italic example sentence.");
        italicText.Style = italicStyle;

        TextStyle boldStyle = new TextStyle()
        {
            IsBold = true
        };
        Text boldText = new Text("Bold example sentence.");
        boldText.Style = boldStyle;

        // Create a paragraph and add texts
        Paragraph paragraph = new Paragraph();
        paragraph.AddText(introText);
        paragraph.AddText(italicText);
        paragraph.AddText(boldText);

        // Add paragraph to the document
        doc.AddParagraph(paragraph);

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

Friend Class Program
	Shared Sub Main()
		' Ensure to set up the license key
		IronWord.License.LicenseKey = "YOUR-KEY"

		' Load an existing docx
		Dim doc As New WordDocument("document.docx")

		' Create sample texts with styles
		Dim introText As New Text("This is an example paragraph with italic and bold styling.")
		Dim italicStyle As New TextStyle() With {.IsItalic = True}
		Dim italicText As New Text("Italic example sentence.")
		italicText.Style = italicStyle

		Dim boldStyle As New TextStyle() With {.IsBold = True}
		Dim boldText As New Text("Bold example sentence.")
		boldText.Style = boldStyle

		' Create a paragraph and add texts
		Dim paragraph As New Paragraph()
		paragraph.AddText(introText)
		paragraph.AddText(italicText)
		paragraph.AddText(boldText)

		' Add paragraph to the document
		doc.AddParagraph(paragraph)

		' Export the document
		doc.SaveAs("save_document.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

VS 2022プログラムによる新しいWord文書作成(チュートリアル):上記のサンプルコードの出力

説明 of code

  1. 上記の例のように、Textオブジェクトを作成し、文書のサンプルテキストを追加します。
  2. 次に、新しいTextStyleオブジェクトを作成し、プロパティIsItalictrueとして割り当て、テキストがイタリックになるようにします。
  3. 同様に、boldText変数もプロパティIsBoldtrueとして割り当てます。
  4. その後、TextStyle変数をそれぞれのText変数に割り当てます。
  5. Paragraph変数を作成し、AddTextメソッドを呼び出してイタリックおよび太字のテキストを追加します。
  6. AddParagraphメソッドを使用して文書に段落を追加します。
  7. 最後に、文書を「save_document.docx」として保存します。

上記の例は、IronWordを使用したフォントとスタイルを開発者が利用できることを示しています。 イタリックや太字効果に加えて、IronWordは他の機能を提供しており、開発者がさまざまな状況でユニークなWord文書を作成できるようにしています。

結論

VS 2022プログラムによる新しいWord文書作成(チュートリアル):IronWordライセンス情報

私たちのデモンストレーションは、IronWordライブラリを使用して、C#でプログラム的にWord文書を作成することがいかに簡単であるかを示しました。 ライブラリの柔軟性とスケーラビリティは、実際のシナリオでの開発者にとって、文書内のテキスト、フォント、スタイルのカスタマイズといった課題に対する貴重なツールとなります。 Wordが他のアプリケーションとどのように統合されているかを理解することで、開発者には彼らの課題に対する追加のソリューションが提供されます。

IronWordは無料の試用ライセンスを提供しています。

よくある質問

C#でMicrosoft Word文書をプログラムで作成する方法は?

IronWordライブラリを使用して、C#でMicrosoft Word文書をプログラムで作成できます。WordDocumentオブジェクトを初期化し、必要な内容を追加してSaveAsメソッドを使い.docxファイルとしてエクスポートします。

Microsoft Office Interopを使用したWord文書作成に対するIronWordの利点は何ですか?

IronWordは、Microsoft Officeインストールを必要とせず、異なるプラットフォーム間での柔軟性と容易な統合を提供し、Officeへの依存を排除する利点があります。

Visual StudioでIronWordをインストールするには?

Visual StudioでIronWordをインストールするには、NuGetパッケージマネージャーを開き、IronWordを検索してプロジェクトにインストールします。あるいは、Install-Package IronWordを使ってコマンドラインを使用します。

IronWordを使ってWord文書のテキストをフォーマットできますか?

はい、IronWordを使ってTextStyleオブジェクトを利用し、イタリックや太字などのスタイルをテキスト要素に適用してWord文書のテキストをフォーマットできます。

IronWordは.NETやAzureと互換性がありますか?

はい、IronWordは.NET 8, 7, 6, Framework, Core そしてAzureと完全に互換性があり、さまざまなアプリケーションで多用途に活用できます。

IronWordのインストールの問題をトラブルシュートするには?

IronWordのインストールで問題が発生した場合、Visual Studioと.NETの互換バージョンを使用していること、NuGetパッケージマネージャーが更新されていることを確認してください。インターネット接続を確認して再インストールを試みてください。

IronWordでWord文書の作成を開始するためには何が必要ですか?

IronWordライブラリとそのモデルをC#プロジェクトにインポートし、WordDocumentオブジェクトを初期化して文書の作成を開始します。

IronWordを使用してWord文書に段落をプログラムで追加するには?

IronWordを使用してParagraphオブジェクトを作成し、そこにテキストを追加し、WordDocumentに含めることでWord文書に段落をプログラムで追加できます。

Jordi Bardia
ソフトウェアエンジニア
Jordiは、最も得意な言語がPython、C#、C++であり、Iron Softwareでそのスキルを発揮していない時は、ゲームプログラミングをしています。製品テスト、製品開発、研究の責任を分担し、Jordiは継続的な製品改善において多大な価値を追加しています。この多様な経験は彼を挑戦させ続け、興味を持たせており、Iron Softwareで働くことの好きな側面の一つだと言います。Jordiはフロリダ州マイアミで育ち、フロリダ大学でコンピュータサイエンスと統計学を学びました。