Convert Word files to image formats in C# and VB.NET
You can convert a Word page to an image (SVG, PNG, JPEG, GIF, BMP, TIFF, WMP, EMF) by using the GemBox.Document library in C# and VB.NET.
The following example shows how you can convert a Word page into a PNG image of a specified size:
using GemBox.Document;
// 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("Input.docx");
// 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("Input.docx")
' 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
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 the image formats that support transparency (like PNG) will have a transparent background, and the 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 the pages of a Word document to a multi-frame TIFF image.
using GemBox.Document;
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Load a Word file.
var document = DocumentModel.Load("Input.docx");
// 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("Input.docx")
' 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
The next example shows how to save each page from a Word document as a separate PNG or JPEG image.
using GemBox.Document;
using System.IO;
using System.IO.Compression;
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Load a Word file.
var document = DocumentModel.Load("Input.docx");
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("Input.docx")
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
