Convert all Pages in Word DOCX Document to separate JPEG or PNG Image Files or Memory Streams C# .NET

Where can I find the code example for the tools “Convert Files Online - Word, PDF, HTML, JPG And Many More

I want to be able to convert a single document into multiple jpg/png images (one-per-page) and be also able to choose what conversion method to use. I’m using the latest version of Aspose.Total.NET

Hi @filipe.costa Maybe this helps: Boost productivity with high-performance document processing libraries in Python, Java, C#, and C++

@filipe.costa,

You can use the following C# code of Aspose.Words for .NET API to convert all pages in Word DOCX Document to separate JPEG or PNG image files:

Document doc = new Document(@"C:\Temp\input.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\output.jpeg", imageSaveOptions); 

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

It works exactly as asked. Thank you.

How would I send them all, back in a byte[] array instead of writing to disk?

@filipe.costa,

The following code will convert all Word Pages to JPEG streams:

Document doc = new Document(@"C:\Temp\input.docx");

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

HandlePageSavingCallback handler = new HandlePageSavingCallback();
imageSaveOptions.PageSavingCallback = handler;

MemoryStream memoryStream = new MemoryStream();
doc.Save(memoryStream, imageSaveOptions);

// handler.jpeg_streams should now contain jpeg images

private class HandlePageSavingCallback : IPageSavingCallback
{
    public ArrayList jpeg_streams = new ArrayList();
    public void PageSaving(PageSavingArgs args)
    {
        args.PageStream = new MemoryStream();
        args.KeepPageStreamOpen = true;
        jpeg_streams.Add(args.PageStream);
    }
}
1 Like