Convert Word document to PDF in ASP.NET MVC

27 Feb 20259 minutes to read

Syncfusion® Essential® DocIO is a .NET 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 PDF in ASP.NET MVC.

Steps to convert Word document to PDF in C#

Prerequisites:

  • Visual Studio 2022.
  • Install .NET desktop development workload with necessary .NET Framework SDK.

Step 1: Create a new ASP.NET Web Application project.

Create ASP.NET MVC application in Visual Studio

Step 2: Select the MVC application.

Create ASP.NET MVC application in Visual Studio

Step 3: Install the Syncfusion.DocToPdfConverter.AspNet.Mvc5 NuGet package as a reference to your project from NuGet.org.

Install DocIO ASP.NET MVC 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 4: Include the following namespace in that HomeController.cs file.

using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.Pdf;
using Syncfusion.DocToPDFConverter;

Step 5: 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 6: Add a new button in the Index.cshtml as shown below.

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

Step 7: Add a new action method ConvertWordDocumentToPDF in HomeController.cs and include the below code snippet to convert the Word document to PDF and download it.

//Open the file as Stream
using (FileStream docStream = new FileStream(Server.MapPath("~/App_Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
    //Loads file stream into Word document
    using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
    {
        //Instantiation of DocToPDFConverter for Word to PDF conversion
        using (DocToPDFConverter converter = new DocToPDFConverter())
        {
            //Converts Word document into PDF document
            using (PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument))
            {
                //Saves the PDF document to MemoryStream.
                MemoryStream stream = new MemoryStream();
                pdfDocument.Save(stream);
                stream.Position = 0;

                //Download PDF document in the browser.
                return File(stream, "application/pdf", "Sample.pdf");
            }                       
        };                 
    }
}

Step 8: Build the project.

Click on Build → Build Solution or press Ctrl+Shift+B to build the project.

Step 9: Run the project.

Click the Start button (green arrow) or press F5 to run the app.

You can download a complete working sample from GitHub.

By executing the program, you will get the PDF document as follows.

Word to PDF in ASP.NET MVC

Prerequisites:

  • JetBrains Rider.
  • Install .NET Framework Developer Pack.

Step 1. Open JetBrains Rider and create a new ASP.NET MVC web application project.

  • Launch JetBrains Rider.
  • Click New Solution on the welcome screen.

Launch JetBrains Rider

  • In the New Solution dialog, select Project Type as Web.
  • Enter a project name and specify the location.
  • Select the target framework as Full Framework and choose the desired version.
  • Select Template as Web App.
  • Click create.

Creating a new ASP.NET MVC web application in JetBrains Rider

Step 2: Install the NuGet package from NuGet.org.

  • Click the NuGet icon in the Rider toolbar and type Syncfusion.DocToPdfConverter.AspNet.Mvc5 in the search bar.
  • Ensure that nuget.org is selected as the package source.
  • Select the latest Syncfusion.DocToPdfConverter.AspNet.Mvc5 NuGet package from the list.
  • Click the + (Add) button to add the package.

Select the Syncfusion.DocToPdfConverter.AspNet.Mvc5 NuGet package

  • Click the Install button to complete the installation.

Install the Syncfusion.DocToPdfConverter.AspNet.Mvc5 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 namespace in that HomeController.cs file.

using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.Pdf;
using Syncfusion.DocToPDFConverter;

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("ConvertWordtoPDF", "Home", FormMethod.Get);
{
<div>
    <input type="submit" value="Convert Word Document to PDF" style="width:220px;height:27px" />
</div>
}
Html.EndForm();
}

Step 6: Add a new action method ConvertWordDocumentToPDF in HomeController.cs and include the below code snippet to convert the Word document to PDF and download it.

//Open the file as Stream
using (FileStream docStream = new FileStream(Server.MapPath("~/App_Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
    //Loads file stream into Word document
    using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
    {
        //Instantiation of DocToPDFConverter for Word to PDF conversion
        using (DocToPDFConverter converter = new DocToPDFConverter())
        {
            //Converts Word document into PDF document
            using (PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument))
            {
                //Saves the PDF document to MemoryStream.
                MemoryStream stream = new MemoryStream();
                pdfDocument.Save(stream);
                stream.Position = 0;

                //Download PDF document in the browser.
                return File(stream, "application/pdf", "Sample.pdf");
            }                       
        };                 
    }
}

Step 7: Build the project.

Click the Build button in the toolbar or press Ctrl+Shift+B to build the project.

Step 8: Run the project.

Click the Run button (green arrow) in the toolbar or press F5 to run the app.

You can download a complete working sample from GitHub.

By executing the program, you will get the PDF document as follows.

Word to PDF in ASP.NET MVC

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

An online sample link to convert Word document to PDF in ASP.NET MVC