Java Program to Extract a Image From a PDF
Last Updated :
17 Nov, 2020
Program to extract an image from a PDF using Java. The external jar file is required to import in the program. Below is the implementation for the same.
Algorithm:
- Extracting image using the APACHE PDF Box module.
- Load the existing PDF document using file io.
- Creating an object of PDFRenderer class.
- Rendering an image from the PDF document using the BufferedImage class.
- Writing the extracted image to the new file.
- Close the document.
Note: External files are required to download for performing the operation. For more documentation of the module used to refer to this.
Implementation:
Java
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
class GFG {
public static void main(String[] args) throws Exception
{
File newFile
= new File( "C:/Documents/GeeksforGeeks.pdf" );
PDDocument pdfDocument = PDDocument.load(newFile);
PDFRenderer pdfRenderer
= new PDFRenderer(pdfDocument);
BufferedImage img = pdfRenderer.renderImage( 0 );
ImageIO.write(
img, "JPEG" ,
new File( "C:/Documents/GeeksforGeeks.png" ));
System.out.println(
"Image has been extracted successfully" );
pdfDocument.close();
}
}
|
PDF before execution:
Existing PDF Document which containing the image which is to be extracted
Image after extraction:
Extracted Image from the PDF document
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...