Aspose Word won't convert entire Word document to png

I’m using the following code:

Document document = new Document(documentStream);

ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.Png);
opt.PageIndex = 0;
opt.PageCount = document.PageCount;

MemoryStream imageDocument = new MemoryStream();
document.Save(imageDocument, opt);

imageDocument.Position = 0;
documentStream.Position = 0;
return imageDocument;

This code is taking a documentStream, and saving it as a png. This document stream is also being saved separately as a docx. When I download the saved docx, all 5 pages are there, however, when I download the saved png, only one page is there. Even if I change around the PageIndex or PageCount, I only get one page at a time.

Not sure if this is a limitation of the evaluation copy or not. Is there something I’m doing wrong?

Hi James,

Thanks for your inquiry. Following code example shows how to convert all pages of document to images.

Document doc = new Document(MyDir + @"in.docx");
ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Png);
opts.PageIndex = 0;
opts.PageCount = doc.PageCount;
opts.PageSavingCallback = new HandlePageSavingCallback();
doc.Save(MyDir + @"16.11.0.png", opts);
private class HandlePageSavingCallback : IPageSavingCallback
{
    public void PageSaving(PageSavingArgs args)
    {
        args.PageFileName = string.Format(MyDir + @"Page_{0}.png", args.PageIndex);
    }
}

Thanks. So theres no built in way to convert all pages one single image?

Nevermind, I think I can just make the document one long page then make it an image

Hi James,

Thanks for your inquiry. Unfortunately, there is no APIs to export document’s pages into one image. However, you can use following code example to render individual pages to graphics to create one image with thumbnails of all pages. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
// This defines the number of columns to display the thumbnails in.
const int thumbColumns = 2;
// Calculate the required number of rows for thumbnails.
// We can now get the number of pages in the document.
int remainder;
int thumbRows = Math.DivRem(doc.PageCount, thumbColumns, out remainder);
if (remainder > 0)
    thumbRows++;
// Lets say I want thumbnails to be of this zoom.
const float scale = 0.25f;
// For simplicity lets pretend all pages in the document are of the same size, 
// so we can use the size of the first page to calculate the size of the thumbnail.
Size thumbSize = doc.GetPageInfo(0).GetSizeInPixels(scale, 96);
// Calculate the size of the image that will contain all the thumbnails.
int imgWidth = thumbSize.Width * thumbColumns;
int imgHeight = thumbSize.Height * thumbRows;
using (Bitmap img = new Bitmap(imgWidth, imgHeight))
{
    // The user has to provides a Graphics object to draw on.
    // The Graphics object can be created from a bitmap, from a metafile, printer or window.
    using (Graphics gr = Graphics.FromImage(img))
    {
        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        // Fill the "paper" with white, otherwise it will be transparent.
        gr.FillRectangle(new SolidBrush(Color.White), 0, 0, imgWidth, imgHeight);
        for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
        {
            int columnIdx;
            int rowIdx = Math.DivRem(pageIndex, thumbColumns, out columnIdx);
            // Specify where we want the thumbnail to appear.
            float thumbLeft = columnIdx * thumbSize.Width;
            float thumbTop = rowIdx * thumbSize.Height;
            SizeF size = doc.RenderToScale(pageIndex, gr, thumbLeft, thumbTop, scale);
            // Draw the page rectangle.
            gr.DrawRectangle(Pens.Black, thumbLeft, thumbTop, size.Width, size.Height);
        }
        img.Save(MyDir + "Rendering.Thumbnails Out.png");
    }
}