Convert word document to image format

Hello AsposeTeam, I am trying to convert a word document to image document.

We have checked the documentation of aspose java word api version-23 which has the extractPage(arg1, arg2) method.

Currently we are using aspose word version 2018 which does not have the extractPage method. How to convert a word text document to word image document in 2018 aspose java version api??

@ketanpbh

To convert a Word document to an image format using Aspose.Words for Java version 2018, you can utilize the ImageSaveOptions class. Although the extractPage method is not available in your version, you can still achieve the conversion by specifying the desired image format and options.

Here’s a sample code snippet that demonstrates how to convert a Word document to a JPEG image:

import com.aspose.words.*;

public class WordToImage {
    public static void main(String[] args) throws Exception {
        // Load the Word document
        Document doc = new Document("path/to/your/document.docx");

        // Create an instance of ImageSaveOptions
        ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.JPEG);
        
        // Optionally, set the resolution and other properties
        saveOptions.setResolution(300); // Set the resolution to 300 DPI

        // Save the document as an image
        doc.save("path/to/output/image.jpg", saveOptions);
    }
}

In this example:

  • The Document class is used to load the Word document.
  • The ImageSaveOptions class is configured to save the document as a JPEG image.
  • You can adjust the resolution and other properties as needed before saving the document.

This approach allows you to convert the entire document or specific pages to an image format, even without the extractPage method. If you need to save specific pages, you can use the PageSet property in ImageSaveOptions to specify which pages to render.

For more detailed options and configurations, you may want to refer to the official documentation on saving images from documents using Aspose.Words for Java.

If you have any further questions or need additional assistance, feel free to ask!

@ketanpbh Actually it is not quite correct to use Document.extractPages method to convert document to images. It is better to use Document.save method and specify page set in the image save options:

Document doc = new Document("in.docx");
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
for (int i = 0; i < doc.getPageCount(); i++)
{
    opt.setPageSet(new PageSet(i));
    doc.save("page_" + i + ".png", opt);
}

Hello @alexey.noskov ,
Thanks for you quick response.

opt.setPageSet(new PageSet(i));
I have not found the new PageSet(i) . Is it something I have missed?
PageSet class is present in which package?

Below is my code -

    public static void main(String[] args) throws Exception {
        Document docOut = new Document();
        DocumentBuilder builder = new DocumentBuilder(docOut);

        Document doc = new Document("E:\\General Terms and Conditions for Services - CANADA 12_4 29sept24.docx");
        ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
        for (int i = 0; i < doc.getPageCount(); i++)
        {
            opt.setPageSet(new PageSet(i));
            opt.setResolution(150f);
            doc.save("E:\\page_" + i + ".png", opt);
            String imagePath = String.format("E:\\page_%d.png", i);
            builder.insertImage(imagePath);

            File imageFile = new File(imagePath);
            if (imageFile.delete()) {
                System.out.println("Deleted the file: " + imagePath);
            } else {
                System.out.println("Failed to delete the file: " + imagePath);
            }

            docOut.save("E:\\Output.docx");
        }
        System.out.printf("Hello and completed!!!");
    }

@ketanpbh You ca using an old version of Aspose.Words. There are PageIndex and PageCount properties. You can modify the code like this:

Document doc = new Document("in.docx");
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
for (int i = 0; i < doc.getPageCount(); i++)
{
    opt.setPageIndex(i);
    opt.setPageCount(1);
    doc.save("page_" + i + ".png", opt);
}


opt.setPageIndex(i); does the job but delay in dll loading. Getting the same warning messages depending on the page count. Is there any way handle it?
What could be the reason? Please look the attached image.

@ketanpbh I am not sure the error is related to Aspose.Words. In the error message I also do not see any mentioning of Aspose.Words.