Set image size limits

The PdfViewOptions class has the following methods to set or get 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, call setImageWidth/setImageHeight methods.
  • To render multiple images, call setImageMaxWidth/setImageMaxHeight options. If an image exceeds these limits, it is resized proportionally.
Warning
If you set the ImageWidth/ImageHeight options, calls of setImageMaxWidth/setImageMaxHeight methods are ignored.

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

  1. Instantiate the Viewer object.
  2. Instantiate the PdfViewOptions 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.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("sample.jpg")) {
    PdfViewOptions viewOptions = new PdfViewOptions("result.pdf");
    viewOptions.setImageMaxWidth(800);
    viewOptions.setImageMaxHeight(600);

    viewer.view(viewOptions);
}