Convert Word files to image formats

The following example shows how you can convert a Word page to a PNG image of a specified size using GemBox.Document in C# and VB.NET.

Upload your file (Drag file here)
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using the 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%");

        // Create image save options.
        var imageOptions = new ImageSaveOptions(ImageSaveFormat.Png)
        {
            PageNumber = 0, // Select the first Word page.
            Width = 1240 // Set the image width and keep the aspect ratio.
        };

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

Module Program

    Sub Main()

        ' If using the 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%")

        ' Create image save options.
        Dim imageOptions As New ImageSaveOptions(ImageSaveFormat.Png) With
        {
            .PageNumber = 0, ' Select the first Word page.
            .Width = 1240 ' Set the image width and keep the aspect ratio.
        }

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

    End Sub
End Module
Converted Word document to PNG image in C# and VB.NET
Screenshot of an input Word file and converted output PNG image

You can specify what image background color should be used by setting the document's page color with the section's PageSetup.PageColor property. If a page color is not specified 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.

Convert a Word file to a multi-frame image

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

Upload your file (Drag file here)
using GemBox.Document;

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

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

        // Max integer value indicates that all document pages should be saved.
        var imageOptions = new ImageSaveOptions(ImageSaveFormat.Tiff)
        {
            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 the 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
TIFF frames from converted Word file using C# and VB.NET
Screenshot of a converted Word file to TIFF frames

Convert a Word file to multiple images

The next example shows how to save each page from a Word document as a separate PNG or JPEG image.

Upload your file (Drag file here)
using GemBox.Document;
using System.IO;
using System.IO.Compression;

class Program
{
    static void Main()
    {
        // If using the 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.Png);

        // 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 GemBox.Document
Imports System.IO
Imports System.IO.Compression

Module Program

    Sub Main()

        ' If using the 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(ImageSaveFormat.Png)

        ' 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
Converted Word pages to PNG images in C# and VB.NET
Screenshot of converted Word pages to PNG images

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy