3 Easy Ways to Convert Word Documents to Images in C# | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (163).NET Core  (29).NET MAUI  (81)Angular  (81)ASP.NET  (50)ASP.NET Core  (79)ASP.NET MVC  (88)Azure  (38)Blazor  (160)DocIO  (23)Essential JS 2  (89)Essential Studio  (180)File Formats  (33)Flutter  (123)JavaScript  (190)Microsoft  (114)PDF  (68)React  (65)Succinctly series  (128)Syncfusion  (634)TypeScript  (32)Uno Platform  (3)UWP  (4)Vue  (40)Webinar  (35)Windows Forms  (59)WinUI  (56)WPF  (140)Xamarin  (157)XlsIO  (26)Other CategoriesBarcode  (5)BI  (29)Bold BI  (5)Build conference  (6)Business intelligence  (54)Button  (4)C#  (121)Chart  (59)Cloud  (13)Company  (445)Dashboard  (8)Data Science  (3)Data Validation  (6)DataGrid  (56)Development  (418)Doc  (7)DockingManager  (1)eBook  (96)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (24)Extensions  (19)File Manager  (4)Gantt  (14)Gauge  (11)Git  (4)Grid  (28)HTML  (12)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (333)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (33)Performance  (10)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (8)Road Map  (11)Scheduler  (39)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (23)Solution Services  (3)Spreadsheet  (11)SQL  (8)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (95)UI  (238)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (23)Visual Studio Code  (17)Web  (396)What's new  (229)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
3 Easy Ways to Convert Word Documents to Images in C#

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

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;

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.
//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))
    {
        //Create a new instance of DocIORenderer.
        using (DocIORenderer render = new DocIORenderer())
        {
            //Convert an entire Word document to images.
            Stream[] imageStreams = wordDocument.RenderAsImages(); 
            for (int i = 0; i < imageStreams.Length; i++)
            {
                //Save the stream as file.
                using (FileStream fileStreamOutput = File.Create("WordToImage_" + i + ".jpeg"))
                {
                    imageStreams[i].CopyTo(fileStreamOutput);
                }
            }
        }
    }
}

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))
    {
        //Create a new instance of DocIORenderer.
        using (DocIORenderer render = new DocIORenderer())
        {
            //Convert the first page of the Word document into an image.
            Stream imageStream = wordDocument.RenderAsImages(0, ExportImageFormat.Jpeg); 
            //Save the stream as file.
            using (FileStream fileStreamOutput = File.Create("WordToImage.jpeg"))
            {
                imageStream.CopyTo(fileStreamOutput);
            }
        }
    }
}

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))
    {
        //Create a new instance of DocIORenderer.
        using (DocIORenderer render = new DocIORenderer())
        {
            //Convert a specific range of pages in Word document to images.
            Stream[] imageStreams = wordDocument.RenderAsImages(1, 2);
            for (int i = 0; i < imageStreams.Length; i++)
            {
                //Save the stream as file.
                using (FileStream fileStreamOutput = File.Create("WordToImage_" + i + ".jpeg"))
                {
                    imageStreams[i].CopyTo(fileStreamOutput);
                }
            }
        }
    }
}

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 PDFimageHTMLRTF, 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 forumsupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top