Extract Images from PDF
As part of IronPDF's extensive collection of PDF creation and editing functions, IronPDF also facilitates granular processing of a PDF document's content through its content extraction methods.
The extractAllImages
method returns a collection of all the images embedded in a PDF document, each formatted as a BufferedImage
object.
The most common business use case is to save PDF images in separate files. The featured code example below demonstrates a method to achieve this using the extractAllImages
method along with the Java ImageIO
class.
IronPDF can also pull images from PDFs in their raw byte form. For this, use the extractAllRawImages
method instead.
It is also possible to extract images from a subset of PDF pages (as opposed to the entire document). The brief code snippet below uses the extractAllImagesFromPages
method to pull the images from pages 3 and 7 of a sample document.
import com.example.IronPdfLibrary; // Example import, the actual library path may differ depending on your setup
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import javax.imageio.ImageIO;
public class PdfImageExtractor {
public static void main(String[] args) {
try {
// Load the PDF document from a file
PdfDocument document = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Extract all images from pages 3 to 7
List<BufferedImage> pageRangeImages = document.extractAllImagesFromPages(PageSelection.pageRange(3, 7));
// Iterate through each extracted BufferedImage
for (int i = 0; i < pageRangeImages.size(); i++) {
BufferedImage image = pageRangeImages.get(i);
// Save each extracted image as a separate file
File outputfile = new File("extracted_image_" + i + ".png");
ImageIO.write(image, "png", outputfile);
}
} catch (IOException e) {
System.out.println("An error occurred while processing the PDF: " + e.getMessage());
}
}
}
import com.example.IronPdfLibrary; // Example import, the actual library path may differ depending on your setup
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import javax.imageio.ImageIO;
public class PdfImageExtractor {
public static void main(String[] args) {
try {
// Load the PDF document from a file
PdfDocument document = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Extract all images from pages 3 to 7
List<BufferedImage> pageRangeImages = document.extractAllImagesFromPages(PageSelection.pageRange(3, 7));
// Iterate through each extracted BufferedImage
for (int i = 0; i < pageRangeImages.size(); i++) {
BufferedImage image = pageRangeImages.get(i);
// Save each extracted image as a separate file
File outputfile = new File("extracted_image_" + i + ".png");
ImageIO.write(image, "png", outputfile);
}
} catch (IOException e) {
System.out.println("An error occurred while processing the PDF: " + e.getMessage());
}
}
}
How to Extract Images from PDF in Java
- Install IronPDF Java Library
- Use the
extractAllImages
method to extract images from a PDF - Iterate through each image in the images collection object
- Export the extracted images with the
write
method in Java - Perform the extraction without affecting the original PDF