Convert Word DOCX Document to JPEG Images | C# VB.NET


We’re using Aspose of version to convert word document to jpeg.
For majority of documents the functionality works with no problem.
But there are some which generate null reference exception.

Piece of code we use(in

Dim result As Stream = New MemoryStream()
        Dim doc As Aspose.Words.Document = New Aspose.Words.Document(documentStream)
        Dim imageOptions As New Aspose.Words.Saving.ImageSaveOptions(SaveFormat.Jpeg)
        imageOptions.PageIndex = 0
        imageOptions.PageCount = 1
        doc.Save(result, imageOptions)
    Catch ex As Exception

    End Try

The document which produces such error is in attachments.

Some test.docx (538.9 KB)

Would be appreciated if you could help me to fix that problem.
Thank you in advance.


After an initial test with the licensed latest (21.7) version of Aspose.Words for .NET, we were unable to reproduce this exception on our end. Please check below Aspose.Words for .NET 21.7 generated JPEG Image files:

Please note that PageCount and PageIndex properties are no longer part of the ImageSaveOptions Class. We have now added PageSet and PageRange Classes. Here is the C# example to convert all pages in Word Document to separate JPEG image files:

Document doc = new Document("C:\\Temp\\Some test.docx");

ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.Jpeg);
PageRange pageRange = new PageRange(0, doc.PageCount - 1);
imageSaveOptions.PageSet = new PageSet(pageRange);
imageSaveOptions.PageSavingCallback = new HandlePageSavingCallback();

doc.Save("C:\\temp\\awnet-21.7.jpg", imageSaveOptions);

private class HandlePageSavingCallback : IPageSavingCallback
    public void PageSaving(PageSavingArgs args)
        args.PageFileName = string.Format(@"C:\Temp\Page_{0}.jpg", args.PageIndex);

So, we suggest you to please upgrade to the latest version of Aspose.Words for .NET.


Using new version of Aspose.Words problem has gone.

1 Like