This tutorial is designed to provide basic information on how to get images from a PDF in Java. You will get the reference to the resources used in the sample code, detailed programming logic guiding the development of the application, and a runnable sample code to get an image from a PDF in Java and then save it as a JPG image. Details will also be provided to customize the process like loading the source document from different sources along with the option to set passwords for the protected PDF files or setting the properties of the output image.
Steps to get Images from PDF in Java
- Configure the IDE to add Aspose.PDF for Java for extracting PDF images
- Load the input PDF file into the Document class object from the disk or memory stream
- Get the reference to the resources collection of the target PDF page from which the image is to be extracted
- Get the reference of the desired image using XImage class object from the image collection in the resources
- Instantiate the FileOutputStream object for saving the output image
- Save the target image as a JPG file
These steps provide a step-by-step logic to get image out of PDF in Java. It explains the internal structure of the PDF where each page has a collection of resources, including the images on that page used to access the target image. Once the image reference is obtained, it is saved into the XImage class object that contains methods to save the loaded image on the disk.
Code to get Images from a PDF in Java
This code demonstrates how to get image from PDF in Java by introducing the Document class that is used to load the source PDF. The next important class is XImage which can hold the reference to the image in the images collection of the selected PDF page. Once you get the image reference, you can not only use it to save the loaded image on the disk, but also delete this image from the source PDF, rename the image in the source PDF, and check if the image is transparent or not.
This tutorial can also be used to get all images from PDF in Java. If you are interested in the process of converting an image to PDF, refer to the article on how to convert image to PDF in Java.