How to Convert Word Document to Images in C#

With Aspose.Words for .NET, you can convert document e.g. DOCX, DOC, RTF, ODT, HTML to image file formats e.g. PNG, JPEG, BMP, GIF. In this tutorial you will learn how to convert Word document to images in C#.

Steps to Convert Word Document to Images in C#

  1. Install Aspose.Words for .NET package from NuGet.org
  2. Add reference to Aspose.Words and Aspose.Words.Saving namespaces
  3. Set license of Aspose.Words for .NET using License.SetLicense method
  4. Import the input document into Aspose.Words’ DOM
  5. Create instance of ImageSaveOptions class to specify options when rendering document pages to images
  6. Implement IPageSavingCallback interface and set file name path using PageSavingArgs.PageFileName property
  7. Set Word document’s pages range for image conversion
  8. Finally, save the images to disk using Document.Save method

You often need to use different fonts formatting and styles when working with documents. If you open a document on a machine where used fonts in the document are not installed, the text representation will be different in document viewer. The case will be same for Aspose.Words. It requires TrueType fonts when rendering document to fixed-page formats (JPEG, PNG, PDF or XPS). The fonts used in the input document should be installed on the machine where you want to convert word document to images.

Previously, we looked into how to print Word document using C# in another how-to topic. This topic explains all steps to convert Word document to images in C#

Code to Convert Word Document to Images in C#

using Aspose.Words;
using Aspose.Words.Saving;
using System;
namespace KBCodeExamples
{
class How_to_Convert_Word_Document_to_Images_in_C_sharp
{
public static void ConvertWordDocumenttoImages(String wordtoimage_directory)
{
//Set Aspose license before converting word document to images
//using Aspose.Words for .NET
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License();
AsposeWordsLicense.SetLicense(wordtoimage_directory + @"Aspose.Words.lic");
//Import the document into Aspose.Words DOM.
//The document can be imported from disk or memory stream.
Document doc = new Document(wordtoimage_directory + "input.docx");
//Set ImageSaveOptions to convert document pages to image.
ImageSaveOptions wordpagestoimage = new ImageSaveOptions(SaveFormat.Png);
//Set page ranges to convert all word pages to image.
PageRange pagerange = new PageRange(0, doc.PageCount - 1);
wordpagestoimage.PageSet = new PageSet(pagerange);
wordpagestoimage.PageSavingCallback = new Word_Pages_To_Images();
//Save document's pages to PNG
doc.Save(@"output.png", wordpagestoimage);
}
//Implement this interface if you want to control how Aspose.Words saves separate pages
//when saving a document to fixed page formats.
class Word_Pages_To_Images : IPageSavingCallback
{
public void PageSaving(PageSavingArgs args)
{
args.PageFileName = string.Format(@"output_{0}.png", args.PageIndex);
}
}
}
}

The above code in C# converts word document to images without installing MS Office. You can use it where .NET is installed at Windows, Linux, macOS, and cloud platforms (Amazon Web Services and Microsoft Azure).

 English