How To Run IronWord with .NET on Azure

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

IronWord is a powerful .NET library for creating, editing, and reading Word documents programmatically. It works seamlessly on various Azure services including Azure App Services, Azure Functions, and Azure Container Instances.

Installing IronWord

Begin by installing the IronWord NuGet package from the official NuGet repository:

Install-Package IronWord

Hosting Considerations for Azure

Choosing the Right Azure Service Tier

IronWord performs best on Azure service plans that provide consistent compute availability. For most small to medium use cases, the Basic (B1) App Service Plan is sufficient. If your application processes a high volume of Word documents or performs complex formatting tasks, consider upgrading to Standard (S1) or higher tiers to avoid performance bottlenecks.

Supported .NET Runtimes and Compatibility

IronWord works out of the box with the following frameworks commonly used in Azure-hosted solutions:

  • .NET 6+ (LTS recommended)
  • .NET Core 3.1
  • .NET Standard 2.1

This gives you flexibility to deploy IronWord across various Azure services like App Services, Azure Functions, and Docker containers without worrying about compatibility.

Deploying in Docker on Azure

Containerized Deployment with IronWord

If you're looking for maximum control over your runtime environment, consider deploying IronWord inside a Docker container on Azure Container Instances (ACI) or Azure Kubernetes Service (AKS). This allows you to:

  • Pre-load templates or static resources
  • Configure document processing settings
  • Fine-tune performance at the OS level

To get started, use a base image such as mcr.microsoft.com/dotnet/aspnet:6.0 or 7.0 and add IronWord via NuGet or manual DLL inclusion.

Serverless with Azure Functions

Using IronWord in Azure Functions

IronWord is fully compatible with Azure Functions v4 running on .NET 6 or higher. This enables lightweight, event-driven document generation—perfect for scenarios like:

  • On-demand report creation via HTTP
  • Generating Word documents from form submissions
  • Converting structured data into .docx format

Azure Function Example: Generate Word Document on Request

Below is a real-world example of an Azure Function that creates and returns a Word document in response to an HTTP request:

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;
    }
}
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 Module
$vbLabelText   $csharpLabel

Code Explanation:

  1. We define an Azure Function with the name "GenerateWordDoc".
  2. The function is triggered by an HTTP GET or POST request and logs a message when it begins processing.
  3. We specify the license key for IronWord by setting IronWord.License.LicenseKey (replace "YOUR-LICENSE-KEY" with your actual key).
  4. A new WordDocument is created using IronWord's API.
  5. Two paragraphs are added to the document — one with static text and another showing the current UTC timestamp.
  6. The document is saved to a temporary .docx file on the server using doc.SaveAs(tempPath).
  7. The saved file is read into a byte array using File.ReadAllBytes, preparing it for download.
  8. The temporary file is deleted immediately after reading to keep the system clean.
  9. An HttpResponseMessage is built, containing the document's byte content as a downloadable attachment.
  10. The Content-Disposition header sets the download filename using the current date and time.
  11. The Content-Type header is set to "application/vnd.openxmlformats-officedocument.wordprocessingml.document" to indicate a Word file format.

常见问题解答

设置IronWord在Azure上的第一步是什么?

在Azure上设置IronWord的第一步是创建Azure帐户(如果您还没有的话)。然后,您需要设置一个新的Azure应用服务,您将使用IronWord在此部署您的.NET应用程序。

如何在Azure上使用IronWord部署.NET应用程序?

要在Azure上使用IronWord部署.NET应用程序,您应该将应用程序及其依赖项(包括IronWord库)打包,并将它们上传到您的Azure应用服务。您可以使用Visual Studio发布或Azure DevOps流水线等工具完成此过程。

运行IronWord需要任何特定的Azure服务吗?

IronWord可以在标准的Azure应用服务上运行。然而,为了获得最佳性能,建议使用根据应用程序的需求提供充足资源的计划。

可以在Azure Functions中使用IronWord吗?

可以,将IronWord与Azure Functions结合使用,以一种无服务器架构处理Word文档。确保Azure Functions环境具备IronWord所需的依赖项。

IronWord如何增强在Azure上的Word文档处理?

IronWord通过提供强大的.NET库增强Azure上的Word文档处理,这些库能够轻松与Azure服务集成,实现Word文档的高效创建、操作和转换。

有没有办法在Azure上自动化IronWord任务?

可以,您可以使用Azure逻辑应用或Azure Functions,根据特定事件或计划触发Word文档处理,从而自动化IronWord任务。

在Azure上使用IronWord有什么好处?

在Azure上使用IronWord可以实现可扩展且可靠的Word文档处理,利用Azure的云基础设施处理大量文档,同时保持高性能。

我可以将IronWord与Azure Blob存储集成吗?

可以,您可以将IronWord与Azure Blob存储集成,以存储和检索Word文档,从而实现Azure环境内无缝的文档处理和存储管理。

如何确保IronWord在Azure上高效运行?

为了确保IronWord在Azure上高效运行,选择与您的工作负载匹配的正确服务计划,优化您的应用程序代码,并实施适当的错误处理和日志记录以便于故障排除。

在Azure上使用IronWord有任何前提条件吗?

在Azure上使用IronWord的前提条件包括设置一个.NET环境、一个具有必要权限的Azure帐户,以及在您的项目中包含IronWord库。

Kye Stuart
技术作家

Kye Stuart 在 Iron Software 中将编码热情与写作技能结合在一起。他在 Yoobee 学院接受软件部署教育,现在将复杂的技术概念转化为清晰的教育内容。Kye 重视终身学习,接受新的技术挑战。

工作之余,他们喜欢 PC 游戏、Twitch 上的直播,以及户外活动如园艺和带狗 Jaiya 散步。Kye 的直截了当的方法使他们成为 Iron Software 使命的关键,即为全球开发者解密技术。

准备开始了吗?
Nuget 下载 25,807 | 版本: 2025.11 刚刚发布