DEV Community

Cover image for 3 Easy Ways to Convert Word Documents to Images in C#
Jollen Moyani for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

3 Easy Ways to Convert Word Documents to Images in C#

In situations like downloading a huge Word document, we sometimes want to preview one or more pages of the document before downloading it. You can easily convert pages of a Word document into images to preview them without the help of MS Word.

The Syncfusion .NET Core Word Library (DocIO) can programmatically convert any Word document into images with just a few lines of code in C#. This library does not need Microsoft Word or any interop dependencies. With this functionality, you can easily preview Word documents in applications and share the images for printing.

This Word-to-image conversion is available in multiple environments such as:

  • Windows
  • Linux
  • macOS
  • Mobile devices
  • Docker
  • Cloud computing environments

Word to PDF conversion in csharp

This conversion function supports all the elements of a typical Word document: text, formatting, images, tables, hyperlinks, fields, table of contents, shapes, headers, footers, etc. You can convert commonly used Word file formats such as DOC, DOCX, and RTF into images and save them in different image formats like PNG, JPEG, and BMP.

Note: For more details, refer to our documentation on Converting Word documents to images in C#.

In this blog, we are going to discuss these options available in our Syncfusion .NET Core Word Library for Word-to-image conversion with code examples:

Let’s get started with the implementation!

Getting started

Step 1: First, create a new C# .NET Core Console App in Visual Studio.

Create a new C# .NET Core Console ApplicationStep 2: Then, install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to the app from NuGet Gallery.

Install the Syncfusion.DocIORenderer.Net.Core NuGet package

Step 3: Now, include the following namespaces in the Program.cs file.

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
Enter fullscreen mode Exit fullscreen mode

The project is ready! Let’s write the code to convert a Word document into an image.

Convert all the pages in a Word document into images

With our .NET Core Word Library, you can easily convert all the pages of a Word document into images using the RenderAsImages method of the WordDocument instance.

Refer to the following code example.

//Open the file as Stream.
using (FileStream docStream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read))
{
  //Load an existing Word document.
  using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic))
  {
    //Convert an entire Word document to images.
    Stream[] imageStreams = wordDocument.RenderAsImages();
    for (int i = 0; i < imageStreams.Length; i++)
    {
      //Save the stream as a file.
      using (FileStream fileStreamOutput = File.Create("WordToImage_" + i + ".jpeg"))
      {
        imageStreams[i].CopyTo(fileStreamOutput);
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

By executing the previous code example, we will get output like in the following screenshot.

Converting all pages in a Word document to images

Converting all pages in a Word document to images

Convert the first page of a Word document to an image

If you’re dealing with a large document, you might decide to just export the first page or any single page of it as an image for previewing. You can use the RenderAsImage method of the WordDocument instance with an overload to provide the index of the page to be exported as image. To convert the first page of the Word document into an image, use the index value 0.

Refer to the following code example.

//Open the file as Stream.
Using (FileStream docStream = new FileStream(Template.docx, FileMode.Open, FileAccess.Read))
{
  //Load an existing Word document.
  Using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic))
  {
    //Convert the first page of the Word document into an image.
    Stream imageStream = wordDocument.RenderAsImages(0, ExportImageFormat.Jpeg);
    //Save the stream as a file.
    using (FileStream fileStreamOutput = File.Create("WordToImage.jpeg"))
    {
       imageStream.CopyTo(fileStreamOutput);
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

By executing this code example, we will get output like in the following screenshot.

Converting the first page of a Word document to an image for previewing

Converting the first page of a Word document to an image for previewing

Convert a specific range of pages to images

You may wish to export a range of pages as images in a Word document. For this, use the RenderAsImages method of the WordDocument instance with an overload that accepts two-page indices. The first parameter is the start-page index, and the second parameter is the end-page index. The pages that fall in this range will be converted into images.

Refer to the following code example.

//Open the file as Stream.
Using (FileStream docStream = new FileStream(Template.docx, FileMode.Open, FileAccess.Read))
{
  //Load an existing Word document.
  Using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic))
  {
    //Convert a specific range of pages in a Word document to images.
    Stream[] imageStreams = wordDocument.RenderAsImages(1, 2);
    for (int I = 0; i < imageStreams.Length; i++)
    {
      //Save the stream as a file.
      using (FileStream fileStreamOutput = File.Create"WordToImage”" + i +".jpe”"))
      {
        imageStreams[i].CopyTo(fileStreamOutput);
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

GitHub reference

You can find all the examples for converting Word documents to images using Syncfusion .NET Core Word Library in the GitHub repository.

Conclusion

Thanks for reading this blog. We have seen how to easily convert Word documents into images easily using the Syncfusion .NET Core Word Library. Take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.

Apart from this Word-to-image conversion functionality, our Syncfusion .NET Word Library (DocIO) has the following significant functionalities and many more:

  • Create, read, and edit Word documents programmatically.
  • Create complex reports by merging data into a Word template from various data sources through mail merge.
  • Merge, split, and organize Word documents.
  • Convert Word documents into PDF, image, HTML, RTF, and other formats.

You can find more Word Library examples at this GitHub location.

Are you already a Syncfusion user? You can download the product setup here. If you’re not yet a Syncfusion user, you can download a 30-day free trial.

If you have questions, contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

Top comments (0)