Convert Word document to PDF in ASP.NET MVC
29 Nov 20244 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#
Step 1: Create a new ASP.NET Web Application project.
Step 2: Select the MVC application.
Step 3: Install the Syncfusion.DocToPdfConverter.AspNet.Mvc5 NuGet package as a reference to your project from NuGet.org.
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");
}
};
}
}
You can download a complete working sample from GitHub.
By executing the program, you will get the PDF document as follows.
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