Set image size limits

The HtmlViewOptions class has the following methods to get and set the image width or/and height:

/**
 * Max width of an output image in pixels. (When converting single image to HTML only)
 */
public int getImageMaxWidth();

/**
 * Max width of an output image in pixels. (When converting single image to HTML only)
 */
public void setImageMaxWidth(int imageMaxWidth);

/**
 * Max height of an output image in pixels. (When converting single image to HTML only)
 */
public int getImageMaxHeight();

/**
 * Max height of an output image in pixels. (When converting single image to HTML only)
 */
public void setImageMaxHeight(int imageMaxHeight);

/**
 * The width of the output image in pixels. (When converting single image to HTML only)
 */
public int getImageWidth();

/**
 * The width of the output image in pixels. (When converting single image to HTML only)
 */
public void setImageWidth(int imageWidth);

/**
 * The height of an output image in pixels. (When converting single image to HTML only)
 */
public int getImageHeight();

/**
 * The height of an output image in pixels. (When converting single image to HTML only)
 */
public void setImageHeight(int imageHeight);

You can set the width and/or height of the output images. Use one of the following methods:

  • To render a single image, use setImageWidth/setImageHeight methods.
  • To render multiple images, set setImageMaxWidth/setImageMaxHeight methods. If an image exceeds these limits, it is resized proportionally.
Warning
If you use the setImageWidth/setImageHeight methods, the values specified by thesetImageMaxWidth/setImageMaxHeight methods are ignored.

To call the setImageMaxWidth/setImageMaxHeight methods, follow these steps:

  1. Instantiate the Viewer object.
  2. Instantiate the HtmlViewOptions object.
  3. Call the setImageMaxWidth() and/or setImageMaxHeight() methods.
  4. Call the View.view() method.

The following code snippet shows how to set the output image size limits:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...

try (Viewer viewer = new Viewer("sample.jpg")) {
    HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources();
    // Specify the maximum width and height.
    viewOptions.setImageMaxWidth(800);
    viewOptions.setImageMaxHeight(600);

    viewer.view(viewOptions);
}