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 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 The following example shows how you can convert a Word page to a PNG image of a specified size. 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 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 The following example shows how you can convert all Word pages to a multi-page TIFF image. You can also save each page from a Word document as a separate PNG or JPEG image. We recommend saving each ImageSaveFormat
like PNG, JPEG, SVG, BMP, WMP, GIF, or TIFF.Convert a Word file to a single image
ImageSaveOptions
.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
PageSetup.PageColor
property.Convert a Word file to a multi-frame image
ImageSaveOptions.PageCount
property.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
Convert a Word file to multiple images
DocumentModelPage
as a single image file instead of saving the DocumentModel
into multiple image files, as shown in the next example.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