3 Easy Ways to Convert Word Documents to Images in C# | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)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

Experience the interactive demos to see the vast functionality of Syncfusion’s Word Library for yourself.

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

Be the first to get updates

Subscribe RSS feed