Skip to main content
All docs
V23.2

Azure Functions App for Office File API

  • 4 minutes to read

This topic uses Microsoft Visual Studio UI to create a sample Azure function application that merges uploaded documents, adds a barcode, creates a PDF file, and publishes your project to a function app in Azure.

Prerequisites

Office File API Linux Support

For more information on Office File API on Linux, review the following help topics:

Create an Azure Functions Project

  • From the File menu, select New > Project.
  • Enter func in the search box.
  • Select the Azure Functions template and click Next.

    Azure Functions Template

  • In the Configure your new project dialog, name the project DXOfficeAzureFunctionSample and click Next.

    Configure Project

  • For Create a new Azure Functions application settings, use the following values:

    Setting Value
    .NET version .NET 8.0 Isolated
    Function Http trigger
    Authorization level Anonymous

    Project Template Additional Information

  • Click Create to create the function project and HTTP trigger function.

Add NuGet Packages to the Project

Visit nuget.devexpress.com, log in to your account, and click Obtain Feed URL. Open the NuGet Package Manager in Visual Studio and register the DevExpress NuGet feed as the package source. Install the following packages:

DevExpress.Document.Processor
This package includes Word Processing Document API and other Office File API libraries. You need a license for the DevExpress Office File API Subscription or the DevExpress Universal Subscription to use this package in production code.
DevExpress.Drawing.Skia
This package implements the cross-platform PDF renderer based on the Skia Graphics Library. For more information, review the following help topic: DevExpress.Drawing Graphics Library.
HttpMultipartParser
This package is a Http Multipart/form-data parser.

If you are new to NuGet packages, see the following installation guide for assistance: Install DevExpress Packages Using NuGet Package Manager.

Implement a Custom Azure Function

Open the Function1.cs file and replace the Run method with a custom implementation. The modified file should contain the following content:

using System.Drawing;
using System.Net;
using System.Text;
using DevExpress.BarCodes;
using DevExpress.Drawing;
using DevExpress.Office.Services;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraSpreadsheet.Services;
using HttpMultipartParser;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace DXOfficeAzureFunctionSample {
    public class Function1 {
        [Function("Function1")]
        public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req) {
                OfficeCharts.Instance.ActivateCrossPlatformCharts(); // enable chart support
            using (var server = new RichEditDocumentServer()) {
                var parsedFormBody = MultipartFormDataParser.ParseAsync(req.Body);
                await MergeDocuments(server, parsedFormBody.Result.Files);
                server.Document.AppendSection();

                Stream imageStream = GenerateBarCode();
                server.Document.Images.Append(DocumentImageSource.FromStream(imageStream));

                MemoryStream resultStream = new MemoryStream();
                server.ExportToPdf(resultStream);
                resultStream.Seek(0, SeekOrigin.Begin);

                HttpResponseData okResponse = req.CreateResponse(HttpStatusCode.OK);
                okResponse.Headers.Add("Content-Type", "application/pdf");
                await okResponse.WriteBytesAsync(resultStream.ToArray());
                return okResponse;
            }
        }
        async Task MergeDocuments(RichEditDocumentServer server, IReadOnlyList<FilePart> files) {
            using (var stream = new MemoryStream()) {
                await files[0].Data.CopyToAsync(stream);
                stream.Seek(0, SeekOrigin.Begin);
                server.LoadDocument(stream);
            }
            for (int i = 1; i < files.Count; i++) {
                using (var stream = new MemoryStream()) {
                    await files[i].Data.CopyToAsync(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    server.Document.AppendDocumentContent(stream);
                }
            }
        }
        Stream GenerateBarCode() {
            // Create a QR code.
            BarCode barCode = new BarCode();
            barCode.Symbology = Symbology.QRCode;
            barCode.CodeText = "https://www.devexpress.com";
            barCode.BackColor = Color.White;
            barCode.ForeColor = Color.Black;
            barCode.RotationAngle = 0;
            barCode.CodeBinaryData = Encoding.Default.GetBytes(barCode.CodeText);
            barCode.Options.QRCode.CompactionMode = QRCodeCompactionMode.Byte;
            barCode.Options.QRCode.ErrorLevel = QRCodeErrorLevel.Q;
            barCode.Options.QRCode.ShowCodeText = false;
            barCode.DpiX = 72;
            barCode.DpiY = 72;
            barCode.Module = 2f;

            // Save the barcode as an image.
            var stream = new MemoryStream();
            barCode.Save(stream, DXImageFormat.Png);
            return stream;
        }
    }
}

Note that since the project uses an isolated process model, you should use asynchronous methods to write data to the response, and add a response header before calling an asynchronous method.

Test Azure Functions Locally

Run the project. After a successful build, the following terminal window will appear:

Azure Func Local Terminal Window

You can use Thunder Client to test API response.

Publish to Azure

  1. In Solution Explorer, right-click the project and select Publish. Select Azure and click Next.

    Publish to Azure Dialog

  2. Select Azure Function App (Linux) for the Specific target and click Next.

    Select Azure Function App (Linux)

  3. In the Function instance, select Create new.

    Select Create New Function

  4. The Function App (Linux) dialog appears with pre-populated fields. You may choose to keep these names or to modify them.

    Function App (Linux) Dialog

  5. Click Create to create a function app and associated resources in Azure. After the resources are created and the dialog closes, click Finish to close the Publish wizard.

  6. On the Publish page, select Publish to deploy the package containing the project files to a new function application in Azure. After the deployment is complete, the root URL of the function application in Azure is displayed on the Publish tab.

Test a Published Function

Use Thunder Client for VS Code to test the Azure Function. The request URL is composed of the root URL displayed on the Publish page and the path api/Function1. Thunder client enables you to specify files to process in a request:

Thuder Client Azure Function Test

The resulting PDF file appears as follows:

Azure Func Resulting PDF File