How to resize image of Word document using Java

Hi,

I am trying to save the first page of a Word document as an image with some size constraints. I have been able to achieve the first part but I’m struggling to specify the image size. I want a maximum width and to maintain the aspect ratio. This means that for a document in portrait orientation the image will have a smaller width than this as the height is the larger dimension.

Is this something Aspose is able to do?

My code for generating the image of the first page of the document is as follows:

void convertWordFirstPageToPng(final File wordDocument, final String outputFilename) {
    final String inputPath = wordDocument.getPath();
    final Document sourceDoc = new Document(inputPath);
    final ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.PNG);
    imageSaveOptions.setPageCount(1);

    final File outputFile = new File(wordDocument.getParent() + "/" + outputFilename);
    sourceDoc.save(outputFile.getPath(), imageSaveOptions);
}

Thanks in advance.

And in the time honoured tradition, as soon as I posted my question I resolved it myself! Posting the code in case this helps someone else in the future.

void convertWordFirstPageToPng(final File wordDocument, final String outputFilename) {

        final String inputPath = wordDocument.getPath();
        final Document sourceDoc = new Document(inputPath);
        final ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.PNG);
        imageSaveOptions.setPageCount(1);

        final int targetResolution = 150;
        final Dimension currentSize = sourceDoc.getPageInfo(0).getSizeInPixels(1, targetResolution);
        final Dimension renditionSize = getScaledDimension(currentSize, new Dimension(200, 200));

        final float scale = Math.min((float)renditionSize.height / currentSize.height, (float)renditionSize.width / currentSize.width);
        imageSaveOptions.setScale(scale);
        imageSaveOptions.setResolution(targetResolution);

        final File outputFile = new File(wordDocument.getParent() + "/" + outputFilename);
        sourceDoc.save(outputFile.getPath(), imageSaveOptions);
}

private Dimension getScaledDimension(final Dimension imgSize, final Dimension boundary) {
    final int originalWidth = imgSize.width;
    final int originalHeight = imgSize.height;
    final int boundWidth = boundary.width;
    final int boundHeight = boundary.height;
    int newWidth = originalWidth;
    int newHeight = originalHeight;

    // first check if we need to scale width
    if (originalWidth > boundWidth) {
        //scale width to fit
        newWidth = boundWidth;
        //scale height to maintain aspect ratio
        newHeight = (newWidth * originalHeight) / originalWidth;
    }

    // then check if we need to scale even with the new height
    if (newHeight > boundHeight) {
        //scale height to fit instead
        newHeight = boundHeight;
        //scale width to maintain aspect ratio
        newWidth = (newHeight * originalWidth) / originalHeight;
    }

    return new Dimension(newWidth, newHeight);
}

@sleylandcole

It is nice to hear from you that you have solved your issue. We always appreciate positive feedback from our customers. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.