Resize Images in PDF using Java

This short guide explains how to resize images in PDF using Java. You will find details to set the IDE, a list of steps, and a sample code to reduce image size in PDF using Java. A lot of enhancements will be discussed while changing the sizes of images in a PDF.

Steps to Resize PDF Picture using Java

  1. Set the environment to use Aspose.PDF for Java to resize photos in a PDF
  2. Load the source PDF file into the Document class object and select the target page using a 1-based index
  3. Use the ImagePlacementAbsorber to get the collection of images from the selected page
  4. Get a reference to the target image from the collection and save it in a byte array stream
  5. Now, remove the original image by hiding its placement
  6. Create a new ImageStamp object using the image stream and set the new size as per the requirement
  7. Save the resized image using the ImageStamp object in the same bottom-left location
  8. Save the updated PDF file with resized images in it

These steps summarize the process to change image size in PDF using Java. Load the source PDF, get the target page, get the collection of images from the selected page, access the desired image, and save it in a memory stream. Remove the placement of the original image, create an object of the ImageStamp and initialize it with the original image stream, change the size of the image in the ImageStamp, add it to the page at the same position, set a new size, and save the resultant PDF file.

Code to Resize PDF Photo using Java

This code demonstrates the process to reduce image size of PDF using Java. You may apply image compression or change image type while resizing the image through ImageStamp for a smaller output size, and even replace images in-place if required. Enhance the process by setting a bound check so that the new image does not go beyond the page margins, rotating the image if required, and making the resizing factor configurable.

This article has taught us to increase or decrease image sizes in a PDF. To add JavaScript to a PDF, refer to the article on Add JavaScript to PDF using Java.