We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert Word document to PDF in Azure App service on Linux

Platform: WinForms |
Control: DocIO

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to PDF in Azure App Service on Linux.

Steps to convert Word document to PDF in Azure App service on Linux:

  1. Create a new ASP.NET Core application.

Create a New ASP.Net core MVC application.

  1. Select an MVC project.

Select a MVC project.

  1. Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your .NET Core application from NuGet.org.

Install Syncfusion.DocIORenderer.Net.Core NuGet packages.

  1. Install the SkiaSharp.NativeAssets.Linux NuGet package as a reference to your .NET Core application from NuGet.org.

Install the SkiaSharp.NativeAssets.Linux NuGet package

  1. Create a Font folder in the “wwwroot” folder of the project and copy the required fonts and include the files to the project.

Create Font folder in the “wwwroot” folder

  1. Set the copy to output directory to Copy if newer to all the data files.

Set the copy to output directory to Copy if newer to all the data files

  1. Add an Export To PDF button in index.cshtml.
    @{Html.BeginForm("WordToPDF", "Home", FormMethod.Post, new { enctype = "multipart/form-data" });
     
        {
            <div class="Common">
                <div class="tablediv">
                   <div class="rowdiv">
                        This sample illustrates how to convert Word document to PDF using Essential DocIO and Essential PDF.
                    </div>
                    &nbsp;
                    <div class="rowdiv" style="border-width: 0.5px;border-style:solid; border-color: lightgray; padding: 1px 5px 7px 5px">
                        Click the button to view the resultant PDF document being converted from Word document using Essential DocIO and Essential PDF. Please note that PDF viewer is required to view the resultant PDF.
                        <div class="rowdiv" style="margin-top: 10px">
                            <div class="celldiv">
                                Select Document :
                                @Html.TextBox("file", "", new { type = "file", accept = ".doc,.docx,.rtf,.dot,.dotm,.dotx,docm,.xml" }) <br />
                            </div>
                            <div class="rowdiv" style="margin-top: 8px">
                                <input class="buttonStyle" type="submit" value="Convert to PDF" name="button" style="width:150px;height:27px" />
                                <br />
                                <div class="text-danger">
                                    @ViewBag.Message
                                </div>
                            </div>
                        </div>
                    </div>
                    <br />
                    <div class="rowdiv" style="margin-top: 15px">
                        More information about Word to PDF conversion can be found in this
                        <a href="https://help.syncfusion.com/file-formats/docio/conversion#converting-word-document-to-pdf">documentation</a>
                        section.
                    </div>
                </div>
            </div>
            Html.EndForm();
        }
    }
    
  1. Include the following namespaces and code snippet in controller for converting Word to PDF.
    // [C# Code]
    using Syncfusion.DocIO;
    using Syncfusion.DocIO.DLS;
    using Syncfusion.DocIORenderer;
    using Syncfusion.Pdf;
    using Microsoft.AspNetCore.Hosting;
    using System.IO;
    

 

// [C# Code]
private IHostingEnvironment _env;
public HomeController(IHostingEnvironment env)
{
     _env = env;
}
/// <summary>
/// Convert Word document to PDF
/// </summary>
/// <param name="button"></param>
/// <returns></returns>
public IActionResult WordToPDF(string button)
{
     string fileLoadTime = "";
     string domLoadTime = "";
     string conversionTime = "";
     string saveTime = "";
     if (button == null)
        return View("Index");
 
     if (Request.Form.Files != null)
     {
          if (Request.Form.Files.Count == 0)
          {
              ViewBag.Message = string.Format("Browse a Word document and then click the button to convert as a PDF document");
              return View("Index");
          }
          // Gets the extension from file.
         string extension = Path.GetExtension(Request.Form.Files[0].FileName).ToLower();
          // Compares extension with supported extensions.
         if (extension == ".doc" || extension == ".docx" || extension == ".dot" || extension == ".dotx" || extension == ".dotm" || extension == ".docm"
                   || extension == ".xml" || extension == ".rtf")
          {
               MemoryStream stream = new MemoryStream();
               Request.Form.Files[0].CopyTo(stream);
               try
               {
                     //Open using Syncfusion
                     WordDocument document = new WordDocument(stream, Syncfusion.DocIO.FormatType.Automatic);                     
                     stream.Dispose();
                     stream = null;
                     //Hooks the font substitution event
                     document.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
                     // Creates a new instance of DocIORenderer class.
                     DocIORenderer render = new DocIORenderer();
                     // Converts Word document into PDF document.
                     PdfDocument pdf = render.ConvertToPDF(document);
                     //Unhooks the font substitution event after converting to PDF
                     document.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
                     MemoryStream memoryStream = new MemoryStream();
                     // Save the PDF document
                     pdf.Save(memoryStream);
                     memoryStream.Position = 0;
                     ViewBag.OS = string.Format(System.Environment.OSVersion.ToString());
                     ViewBag.Load = string.Format("FileLoadTime\t" + fileLoadTime);
                     ViewBag.DomLoad = "DomLoadTime\t" + domLoadTime;
                     ViewBag.Conversion = "ConversionTime\t" + conversionTime;
                     ViewBag.Save = "SaveTime\t" + saveTime;
                     return File(memoryStream, "application/pdf", "WordToPDF.pdf");
               }
               catch (Exception ex)
               {
                     ViewBag.Message = ex.ToString();
               }
          }
          else
          {
               ViewBag.Message = string.Format("Please choose Word format document to convert to PDF");
          }
     }
     else
     {
         ViewBag.Message = string.Format("Browse a Word document and then click the button to convert as a PDF document");
     }
     return View("Index");
}
/// <summary>
/// Sets the alternate font when a specified font is not installed in the production environment
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
      string filePath = string.Empty;
      FileStream fileStream = null;
      //Sets the alternate font when a specified font is not installed in the production environment
      if (args.OriginalFontName == "Calibri")
      {
          filePath = _env.WebRootPath + @"/Fonts/calibri.ttf";
          fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
          args.AlternateFontStream = fileStream;
      }
      else if (args.OriginalFontName == "Arial")
      {
          filePath = _env.WebRootPath + @"/Fonts/arial.ttf";
          fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
          args.AlternateFontStream = fileStream;
      }
      else
      {
          filePath = _env.WebRootPath + @"/Fonts/times.ttf";
          fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
          args.AlternateFontStream = fileStream;
     }
}

Refer to the following steps to publish as Azure App Linux:

  1. Right-click the project and select Publish.

Right click the project and select publish

  1. Create a new profile in publish target window.

Pick a App Service Linux as Publish target

  1. Create App service using Azure subscription and select a hosting plan.

Create a Linux App service with new resource.

  1. The Syncfusion DocIO library works from basic hosting plan (B1). So, select the required hosting plan. It does not work if the hosting plan is Free/Shared.

Configure Hosting plan

  1. After creating a profile, click the publish button.

Click Publish button.

  1. Now, the published webpage will open in the browser. Select the Word document and Click Convert to PDF to convert the given Word document to a PDF.

A screenshot of a social media post

Description automatically generated

 

Output document image

 

A complete work sample for converting an Word document to PDF in Azure App service on Linux can be downloaded from AzureAppServiceOnLinux

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile