Skip to main content Skip to footer

Deploy Microsoft Word library to Azure, cloud in 5 easy steps

GrapeCity Documents for Word (GcWord) is a Word API that offers a complete solution for working with Word documents in .NET Standard 2.0 applications. GcWord allows users to generate, modify, load, and save Word documents in code with the same high-speed and small footprint that define the GrapeCity Documents product line. You can work with MS Word documents with full feature support on Azure. Here are the steps needed to create a basic GcWord web app and deploy it to Azure.

Read more about GcWord's features.

Step 1. Create a new ASP.NET Core MVC app

In Visual Studio, go to File > New > Project, and select ASP.NET Core Web Application. In the wizard that opens, select Web Application (Model-View-Controller)

Go to Dependencies > Manage NuGet Packages, and install the packages GrapeCity.Documents.Word and GrapeCity.Documents.Layout (needed if you want to export Word documents to PDF).

Step 3. Modify the Index.cshtml page

Change the Index.cshtml page as shown below (we will add the GetDocx and GetPdf methods to the home controller, which will download the "Hello, World!" DOCX/show PDF):

Index.cshtml 
@{
    ViewData["Title"] = "Home Page";
}

<div class="row">
    <div class="col-sm-6">
        <h2>Welcome to GcWord Web App</h2>
        <ul>
            <li><a href="~/Home/GetDocx">Generate and download the "Helo, World!" DOCX</a></li>
            <li><a href="~/Home/GetPdf">Generate the "Helo, World!" DOCX and render it as PDF</a></li>
        </ul>
    </div>
</div>

Step 4. Modify HomeController.cs

Modify the HomeController.cs so that it will be able to generate the "Hello, World!" DOCX and PDF using the following:

Add usings 
using System.IO;
using GrapeCity.Documents.Word;
using GrapeCity.Documents.Layout;

Add the following two methods:

Modify Index() 
public IActionResult GetDocx()
{
  // Create the 'Hello, World!' DOCX:
  var doc = new GcWordDocument();
  doc.Body.Sections.First.GetRange().Paragraphs.Add("Hello, World!");

  // Save it to a memory stream:
  MemoryStream ms = new MemoryStream();
  doc.Save(ms);
  ms.Seek(0, SeekOrigin.Begin);
  // Send it back to the web page: 
  Response.Headers["Content-Disposition"] = "inline; filename=\"HelloWorld.docx\"";
  return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}

public IActionResult GetPdf()
{
  // Create the 'Hello, World!' DOCX:
  var doc = new GcWordDocument();
  doc.Body.Sections.First.GetRange().Paragraphs.Add("Hello, World!");

  // Save it as PDF to a memory stream (GrapeCity.Documents.Layout is required for this):
  MemoryStream ms = new MemoryStream();
  doc.SaveAsPdf(ms);
  ms.Seek(0, SeekOrigin.Begin);
  // Send it back to the web page:
  Response.Headers["Content-Disposition"] = "inline; filename=\"HelloWorld.pdf\"";
  return new FileStreamResult(ms, "application/pdf");
}

We're done editing the app, run it in Visual Studio to make sure everything works.

Step 5. Publish the app to Azure

Go to to Build > Publish, create a new 'Microsoft Azure App Service' publish profile, select appropriate name, and subscription details. The defaults should work just fine.

You're done!

Learn how to build a .NET Core console application with GcWord in Visual Studio for Windows, MAC, and Linux and how to generate a Word Document in code.

Dmitry Yaitskov

comments powered by Disqus