Azure 上で .NET を使用して IronWord を実行する方法
IronWordは、Word文書をプログラム的に作成、編集、および読み取るための強力な.NETライブラリです。 Azure App Services、Azure Functions、およびAzure Container Instancesなど、さまざまなAzureサービスでシームレスに動作します。
IronWordのインストール
公式のNuGetリポジトリからIronWordのNuGetパッケージをインストールすることから始めます:
Azureのホスティングに関する考慮事項
適切なAzureサービスタイアの選択
IronWordは、一貫した計算可能性を提供するAzureサービスプランで最良のパフォーマンスを発揮します。 ほとんどの小規模から中規模のユースケースでは、Basic (B1) アプリサービスプランで十分です。 もしアプリケーションが大量のWordドキュメントを処理するか、複雑なフォーマット作業を行う場合は、パフォーマンスのボトルネックを避けるためにStandard (S1) またはそれ以上の層へのアップグレードを検討してください。
さまざまなAzureサービスでIronWordを展開する柔軟性を提供します。
-
.NET Standard 2.1
-
.NET 6+ (LTS推奨)
-
.NET Core 3.1
-
.NET Standard 2.1
これにより、App Services、Azure Functions、およびDockerコンテナなど、さまざまなAzureサービスにわたってIronWordをデプロイする柔軟性が得られ、互換性について心配する必要がなくなります。
Azure上のDockerにデプロイする
-
テンプレートや静的リソースを事前にロードする
-
ドキュメント処理の設定を構成する これにより、次のことが可能になります:
-
テンプレートまたは静的リソースを事前に読み込む
-
ドキュメント処理設定を構成する
Azure FunctionsでのIronWordの使用
IronWordは、.NET 6以降で実行されるAzure Functions v4に完全に対応しています。
これは軽量でイベント駆動のドキュメント生成を可能にし、以下のようなシナリオに最適です:
-
HTTPを介したオンデマンドのレポート作成
-
フォーム送信からのWordドキュメント生成 - 構造的なデータを.docx形式に変換する
Azure Functionの例:リクエストに応じたWordドキュメントの生成
以下は、HTTPリクエストに応じてWordドキュメントを生成して返すAzure Functionの実例です:
- 構造化データを.docx形式に変換する
Azure Functionの例:要求に応じてWord文書を生成する
以下は、HTTPリクエストに応じてWord文書を作成し返すAzure Functionの実世界の例です:
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using IronWord;
using IronWord.Models;
using System.IO;
using System.Threading.Tasks;
public static class WordFunction
{
[FunctionName("GenerateWordDoc")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Processing request to generate Word document...");
// Set your IronWord license key
IronWord.License.LicenseKey = "YOUR-LICENSE-KEY";
// Create and populate Word document
var doc = new WordDocument();
Paragraph para1 = new Paragraph(new TextContent("This Word document was generated by IronWord in an Azure Function."));
Paragraph para2 = new Paragraph(new TextContent($"Timestamp: {System.DateTime.UtcNow}"));
doc.AddParagraph(para1);
doc.AddParagraph(para2);
// Save to temporary file
string tempPath = Path.GetTempFileName().Replace(".tmp", ".docx");
doc.SaveAs(tempPath);
// Read the file bytes
byte[] fileBytes = File.ReadAllBytes(tempPath);
// Optionally delete the temp file
File.Delete(tempPath);
// Build the response with the document as an attachment
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(fileBytes)
};
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"IronWord_{System.DateTime.UtcNow:yyyyMMdd_HHmmss}.docx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
return response;
}
}Imports System
Imports System.Net
Imports System.Net.Http
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Azure.WebJobs
Imports Microsoft.Azure.WebJobs.Extensions.Http
Imports Microsoft.Extensions.Logging
Imports System.Net.Http.Headers
Imports IronWord
Imports IronWord.Models
Imports System.IO
Imports System.Threading.Tasks
Public Module WordFunction
<FunctionName("GenerateWordDoc")>
Public Function Run(<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route := Nothing)> ByVal req As HttpRequest, ByVal log As ILogger) As HttpResponseMessage
log.LogInformation("Processing request to generate Word document...")
' Set your IronWord license key
IronWord.License.LicenseKey = "YOUR-LICENSE-KEY"
' Create and populate Word document
Dim doc = New WordDocument()
Dim para1 As New Paragraph(New TextContent("This Word document was generated by IronWord in an Azure Function."))
Dim para2 As New Paragraph(New TextContent($"Timestamp: {DateTime.UtcNow}"))
doc.AddParagraph(para1)
doc.AddParagraph(para2)
' Save to temporary file
Dim tempPath As String = Path.GetTempFileName().Replace(".tmp", ".docx")
doc.SaveAs(tempPath)
' Read the file bytes
Dim fileBytes() As Byte = File.ReadAllBytes(tempPath)
' Optionally delete the temp file
File.Delete(tempPath)
' Build the response with the document as an attachment
Dim response = New HttpResponseMessage(HttpStatusCode.OK) With {.Content = New ByteArrayContent(fileBytes)}
response.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {.FileName = $"IronWord_{DateTime.UtcNow:yyyyMMdd_HHmmss}.docx"}
response.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
Return response
End Function
End ModuleImports System
Imports System.Net
Imports System.Net.Http
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Azure.WebJobs
Imports Microsoft.Azure.WebJobs.Extensions.Http
Imports Microsoft.Extensions.Logging
Imports System.Net.Http.Headers
Imports IronWord
Imports IronWord.Models
Imports System.IO
Imports System.Threading.Tasks
Public Module WordFunction
<FunctionName("GenerateWordDoc")>
Public Function Run(<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route := Nothing)> ByVal req As HttpRequest, ByVal log As ILogger) As HttpResponseMessage
log.LogInformation("Processing request to generate Word document...")
' Set your IronWord license key
IronWord.License.LicenseKey = "YOUR-LICENSE-KEY"
' Create and populate Word document
Dim doc = New WordDocument()
Dim para1 As New Paragraph(New TextContent("This Word document was generated by IronWord in an Azure Function."))
Dim para2 As New Paragraph(New TextContent($"Timestamp: {DateTime.UtcNow}"))
doc.AddParagraph(para1)
doc.AddParagraph(para2)
' Save to temporary file
Dim tempPath As String = Path.GetTempFileName().Replace(".tmp", ".docx")
doc.SaveAs(tempPath)
' Read the file bytes
Dim fileBytes() As Byte = File.ReadAllBytes(tempPath)
' Optionally delete the temp file
File.Delete(tempPath)
' Build the response with the document as an attachment
Dim response = New HttpResponseMessage(HttpStatusCode.OK) With {.Content = New ByteArrayContent(fileBytes)}
response.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {.FileName = $"IronWord_{DateTime.UtcNow:yyyyMMdd_HHmmss}.docx"}
response.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
Return response
End Function
End Moduleコードの説明:
- 名前を "GenerateWordDoc" としてAzure Functionを定義します。
- 関数はHTTP GETまたはPOSTリクエストによってトリガーされ、処理を始めるとメッセージをログに記録します。
- IronWordのライセンスキーを、IronWord.License.LicenseKeyを設定することで指定します("YOUR-LICENSE-KEY" を実際のキーに置き換えます)。
- IronWordのAPIを使用して新しいWordDocumentが作成されます。
- ドキュメントに2つの段落が追加されます - 一つは静的テキストで、もう一つは現在のUTCタイムスタンプを表示します。
- doc.SaveAs(tempPath)を使用してドキュメントがサーバー上の一時的な.docxファイルに保存されます。
- 保存されたファイルはFile.ReadAllBytesを使用してバイト配列に読み込まれ、ダウンロードの準備が整います。
- 一時ファイルは読み込み完了直後に削除され、システムをクリーンに保ちます。
- ドキュメントのバイト コンテンツをダウンロード可能な添付ファイルとして含む HttpResponseMessage が構築されます。
- Content-Disposition ヘッダーは、現在の日付と時刻を使用してダウンロード ファイル名を設定します。
- Content-Type ヘッダーは、Word ファイル形式を示すために"application/vnd.openxmlformats-officedocument.wordprocessingml.document"に設定されています。
よくある質問
AzureでIronWordを設定するための最初のステップは何ですか?
AzureでIronWordを設定するための最初のステップは、Azureアカウントを作成することです。既にアカウントを持っていない場合は、次に、.NETアプリケーションをIronWordを使用してデプロイする新しいAzure App Serviceを設定する必要があります。
AzureでIronWordを使用して.NETアプリケーションをデプロイするにはどうすればよいですか?
AzureでIronWordを使用して.NETアプリケーションをデプロイするには、アプリケーションとその依存関係をパッケージ化し、IronWordライブラリを含めてAzure App Serviceにアップロードします。このプロセスには、Visual Studio PublishやAzure DevOpsパイプラインのようなツールを使用できます。
IronWordを実行するために特定のAzureサービスが必要ですか?
IronWordは標準のAzure App Servicesで実行できます。ただし、最適なパフォーマンスを得るには、アプリケーションの要求に応じた十分なリソースを提供するプランを使用することをお勧めします。
IronWordはAzure Functionsと一緒に使用できますか?
はい、IronWordはAzure Functionsと統合して、サーバーレスアーキテクチャの一部としてWordドキュメントを処理することができます。Azure Function環境にIronWordの必要な依存関係があることを確認してください。
IronWordはどのようにしてAzure上でのWordドキュメント処理を向上させますか?
IronWordはAzure上でのWordドキュメント処理を強化し、強力な.NETライブラリを提供し、Azureサービスと簡単に統合でき、Wordドキュメントの効率的な作成、操作、および変換を可能にします。
Azure上でIronWordタスクを自動化する方法はありますか?
はい、特定のイベントやスケジュールに基づいてWordドキュメント処理をトリガーするAzure Logic AppsやAzure Functionsを使用して、Azure上でIronWordタスクを自動化できます。
IronWordをAzure上で使用する利点は何ですか?
Azure上でIronWordを使用することで、スケーラブルで信頼性の高いWordドキュメント処理が可能になり、Azureのクラウドインフラストラクチャを活用して大量のドキュメントを扱いつつ、高いパフォーマンスを維持します。
IronWordをAzure Blob Storageに統合することはできますか?
はい、IronWordをAzure Blob Storageと統合してWordドキュメントを保存および取得し、Azure環境内のドキュメント処理とストレージ管理をシームレスに行うことができます。
Azure上でIronWordを効率的に実行する方法を教えてください。
Azure上でIronWordを効率的に実行するためには、作業負荷に合ったサービスプランを選択し、アプリケーションのコードを最適化し、トラブルシューティングのための適切なエラーハンドリングとログ記録を実装します。
Azure上でIronWordを使用するための前提条件はありますか?
Azure上でIronWordを使用するための前提条件には、.NET環境が設定されていること、必要な権限を持つAzureアカウントがあること、およびプロジェクトにIronWordライブラリが含まれていることが含まれます。
Kye Stuartは、Iron Softwareでコーディングへの情熱と執筆スキルを融合させています。ソフトウェアデプロイメントを学んでいるYoobeeカレッジで教育を受け、今では複雑な技術的概念を明瞭な教育コンテンツに変換しています。Kyeは生涯学習を重視し、新しい技術的課題を受け入れています。