Convert Word document to Image in ASP.NET
29 Nov 20244 minutes to read
Syncfusion® Essential® DocIO is a .NET Word library used to 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 ASP.NET Web Forms.
NOTE
This ASP.NET Web Form platform is deprecated, you can use the same product from ASP.NET Core platform. For more information on migrating the .NET Word library from .NET Framework to .NET Core, refer here.
Steps to convert Word document to Image in C#
Step 1: Create a new ASP.NET Web application project.
Step 2: Select the Empty project.
Step 3: Install the Syncfusion.DocIO.AspNet 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: Add a new Web Form in your project. Right click on the project and select Add > New Item and add a Web Form from the list. Name it as MainPage.
Step 5: Add a new button in the MainPage.aspx as shown below.
Step 6: Include the following namespace in your MainPage.aspx.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
Step 7: Include the below code snippet in the click event of the button in MainPage.aspx.cs, to convert the Word document to image and download it.
protected void OnButtonClicked(object sender, EventArgs e)
{
//Open existing Word document.
using (FileStream docStream = new FileStream(Server.MapPath("~/App_Data/Input.docx"), FileMode.Open, FileAccess.Read))
{
//Loads file stream into Word document
using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
{
//Convert the first page of the Word document into an image.
System.Drawing.Image image = wordDocument.RenderAsImages(0, ImageType.Bitmap);
//Save the image as jpeg.
ExportAsImage(image, "WordToImage.Jpeg", ImageFormat.Jpeg, HttpContext.Current.Response);
}
}
}
//Download the image file
protected void ExportAsImage(System.Drawing.Image image, string fileName, ImageFormat imageFormat, HttpResponse response)
{
string disposition = "content-disposition";
response.AddHeader(disposition, "attachment; filename=" + fileName);
if (imageFormat != ImageFormat.Emf)
image.Save(Response.OutputStream, imageFormat);
Response.End();
}
You can download a complete working sample from GitHub.
By executing the program, you will get the image as follows.
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.