Convert Word document to Image in AWS Elastic Beanstalk

8 Nov 20245 minutes to read

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to image in AWS Elastic Beanstalk.

Steps to convert Word document to Image in AWS Elastic Beanstalk

Step 1: Create a new ASP.NET Core Web application (Model-View-Controller) project.

Create ASP.NET Core Web application in Visual Studio

Step 2: Install the following Nuget packages in your application from Nuget.org.

Install Syncfusion.DocIORenderer.Net.Core NuGet Package
Install SkiaSharp.NativeAssets.Linux.NoDependencies v2.88.8 NuGet Package

NOTE

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your application to use our components.

Step 3: Include the following namespaces in the HomeController.cs file.

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;

Step 4: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml.

Step 5: Add a new button in the Index.cshtml as shown below.

@{
    Html.BeginForm("ConvertWordtoImage", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Convert Word to Image" style="width:200px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 6: Include the below code snippet in the Homecontroller.cs file to convert a Word document to image and download it.

public ActionResult ConvertWordtoImage()
{
    //Open the file as Stream
    using (FileStream docStream = new FileStream(Path.GetFullPath("wwwroot/Data/Input.docx"), FileMode.Open, FileAccess.Read))
    {
        //Loads file stream into Word document
        using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
        {
            //Hooks the font substitution event.
            wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
            //Instantiation of DocIORenderer
            using (DocIORenderer render = new DocIORenderer())
            {
                //Convert the first page of the Word document into an image.
                Stream imageStream = wordDocument.RenderAsImages(0, ExportImageFormat.Jpeg);
                //Unhooks the font substitution event after converting to image.
                wordDocument.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
                //Reset the stream position.
                imageStream.Position = 0;
                //Save the image file.
                return File(imageStream, "application/jpeg", "WordToImage.Jpeg");
            }
        }
    }
}

private static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
    if (args.OrignalFontName == "Calibri" && args.FontStyle == FontStyle.Regular)
    {
        args.AlternateFontStream = new FileStream(Path.GetFullPath("wwwroot/Fonts/calibri.ttf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    }
}

Steps to publish as AWS Elastic Beanstalk

Step 1: Right-click the project and select Publish to AWS Elastic Beanstalk (Legacy) option.
Right-click the project and select the Publish option

Step 2: Select the Deployment Target as Create a new application environment and click Next button.
Deployment Target in AWS Ealastic Beanstalk

Step 3: Choose the Environment Name in the dropdown list and the URL will be automatically assign and check the URL is available, if available click next otherwise change the URL.
Application Environment in AWS Elastic Beanstalk

Step 4: Select the instance type in t3a.micro from the dropdown list and click next.
Application Environment in AWS Elastic Beanstalk

Step 5: Click the Next button to proceed further.
Application Environment in AWS Elastic Beanstalk

Step 6: Click the Next button.
Application Options in AWS Elastic Beanstalk

Step 7: Click the Deploy button to deploy the sample on
AWS Elastic Beanstalk.
Deploy the sample in AWS Elastic Beanstalk

Step 8: After changing the status from Updating to Environment is healthy, click the URL.
Status check in AWS Elastic Beanstalk

Step 9: After opening the provided URL, click Convert Word to Image button to download the image file.
Click button to Convert Word to Image

You can download a complete working sample from GitHub.

By executing the program, you will get the image as follows.

Word to Image in AWS Elastic Beanstalk

Click here to explore the rich set of Syncfusion Word library (DocIO) features.

An online sample link to convert Word document to image in ASP.NET Core.