Convert Word files to image formats in C# and VB.NET

With GemBox.Document you can easily convert Word files to images using just C# or VB.NET code. You can save your document to any type of ImageSaveFormat like PNG, JPEG, BMP, WMP, GIF, or TIFF.

Convert a Word file to a single image

Saving a single page of a Word document as an image is useful for generating the document's preview or thumbnail. To create a single PNG or JPEG file from a Word file, you need to save the document with ImageSaveOptions.

The following example shows how you can convert a Word page to a PNG image of a specified size and resolution.

Converted Word document to PNG image in C# and VB.NET
Screenshot of an input Word file and converted output PNG image
Upload your file (Drag file here)
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load a Word file into the DocumentModel object.
        var document = DocumentModel.Load("%InputFileName%");
        var section = document.Sections[0];

        // Calculate the default image width based on the page size and DPI resolution.
        var widthInPoints = section.PageSetup.PageWidth;
        var resolution = 300;
        var widthInPixels = widthInPoints * resolution / 72;
        
        var imageOptions = new ImageSaveOptions(ImageSaveFormat.Png)
        {
            // Select the first Word page.
            PageNumber = 0,

            // Set the DPI resolution.
            DpiX = resolution,
            DpiY = resolution,

            // Set the image width to half and keep the aspect ratio.
            Width = widthInPixels / 2
        };

        // Save the DocumentModel object to a PNG file.
        document.Save("Output.png", imageOptions);
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load a Word file into the DocumentModel object.
        Dim document = DocumentModel.Load("%InputFileName%")
        Dim section = document.Sections(0)

        ' Calculate the default image width based on the page size and DPI resolution.
        Dim widthInPoints = section.PageSetup.PageWidth
        Dim resolution = 300
        Dim widthInPixels = widthInPoints * resolution / 72
        
        ' Select the first Word page.
        ' Set the DPI resolution.
        ' Set the image width to half and keep the aspect ratio.
        Dim imageOptions As New ImageSaveOptions(ImageSaveFormat.Png) With
        {
            .PageNumber = 0,
            .DpiX = resolution,
            .DpiY = resolution,
            .Width = widthInPixels / 2
        }

        ' Save the DocumentModel object to a PNG file.
        document.Save("Output.png", imageOptions)

    End Sub
End Module

If a document doesn't specify page color then image formats that support transparency, like PNG, will have a transparent background and image formats that don't support transparency, like JPEG, will have a white background.

Or you can specify what image background color should be used by setting the document's page color with the section's PageSetup.PageColor property.

Convert a Word file to a multi-frame image

You can also save multiple pages from a Word document as a single multi-frame image.

For this, you need to use an image format that supports multiple frames, like TIFF or GIF, and you need to specify the ImageSaveOptions.PageCount property.

The following example shows how you can convert all Word pages to a multi-page TIFF image.

TIFF frames from converted Word file using C# and VB.NET
Screenshot of a converted Word file to TIFF frames
Upload your file (Drag file here)
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load a Word file.
        var document = DocumentModel.Load("%InputFileName%");

        var imageOptions = new ImageSaveOptions(ImageSaveFormat.Tiff)
        {
            // Max integer value indicates that all document pages should be saved.
            PageCount = int.MaxValue
        };

        // Save the TIFF file with multiple frames, each frame represents a single Word page.
        document.Save("Output.tiff", imageOptions);
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load a Word file.
        Dim document = DocumentModel.Load("%InputFileName%")

        ' Max integer value indicates that all document pages should be saved.
        Dim imageOptions As New ImageSaveOptions(ImageSaveFormat.Tiff) With
        {
            .PageCount = Integer.MaxValue
        }

        ' Save the TIFF file with multiple frames, each frame represents a single Word page.
        document.Save("Output.tiff", imageOptions)

    End Sub
End Module

Convert a Word file to multiple images

You can also save each page from a Word document as a separate PNG or JPEG image.

We recommend saving each DocumentModelPage as a single image file instead of saving the DocumentModel into multiple image files, as shown in the next example.

Converted Word pages to PNG images in C# and VB.NET
Screenshot of converted Word pages to PNG images
Upload your file (Drag file here)
using System;
using System.IO;
using System.IO.Compression;
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load a Word file.
        var document = DocumentModel.Load("%InputFileName%");

        var imageOptions = new ImageSaveOptions();

        // Get Word pages.
        var pages = document.GetPaginator().Pages;

        // Create a ZIP file for storing PNG files.
        using (var archiveStream = File.OpenWrite("Output.zip"))
        using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create))
        {
            // Iterate through the Word pages.
            for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
            {
                DocumentModelPage page = pages[pageIndex];

                // Create a ZIP entry for each document page.
                var entry = archive.CreateEntry($"Page {pageIndex + 1}.png");

                // Save each document page as a PNG image to the ZIP entry.
                using (var imageStream = new MemoryStream())
                using (var entryStream = entry.Open())
                {
                    page.Save(imageStream, imageOptions);
                    imageStream.CopyTo(entryStream);
                }
            }
        }
    }
}
Imports System
Imports System.IO
Imports System.IO.Compression
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load a Word file.
        Dim document = DocumentModel.Load("%InputFileName%")

        Dim imageOptions As New ImageSaveOptions()

        ' Get Word pages.
        Dim pages = document.GetPaginator().Pages

        ' Create a ZIP file for storing PNG files.
        Using archiveStream = File.OpenWrite("Output.zip")
            Using archive As New ZipArchive(archiveStream, ZipArchiveMode.Create)
                ' Iterate through the Word pages.
                For pageIndex As Integer = 0 To pages.Count - 1

                    Dim page As DocumentModelPage = pages(pageIndex)

                    ' Create a ZIP entry for each document page.
                    Dim entry = archive.CreateEntry($"Page {pageIndex + 1}.png")

                    ' Save each document page as a PNG image to the ZIP entry.
                    Using imageStream As New MemoryStream()
                        Using entryStream = entry.Open()
                            page.Save(imageStream, imageOptions)
                            imageStream.CopyTo(entryStream)
                        End Using
                    End Using
                Next
            End Using
        End Using

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.