- Copy pqScan.PDFtoImage.jar to your Java project library folder.
- Add pqScan.PDFtoImage.jar to your Java classpath.
How to Convert PDF to JPEG in Java?
Getting JPG image from PDF pages is a key feature in pqScan Java PDF to Image SDK. Java PDF to Image converter library is completely developed in Java SDK 1.6, and customers can easily integrate the transforming PDF pages to jpg/jpeg function in their Java application.
In order to provide perfect PDF to JPEG conversion, there are some extra properties and methods.
//open PDF document from the local PDF file public void loadPDF(String fileName); //load PDF document from PDF stream public void loadPDF(InputStream stream); //you can get the total PDF page count before your conversion public int getPageCount(); //set resolution of output jpg image to improve the image quality public void setDPI(int dpi);
Depending to your needs, you can render jpeg image from PDF pages with original size, or you can render jpg image from PDF pages with specify size(such as you want to get thumbnail image from each PDF pages).
//convert PDF to jpg/jpeg image with default page size public BufferedImage toImage(int pageIndex); //convert PDF to jpg image with target customized size public BufferedImage toImage(int pageIndex, int width, int height);
Here is a simple example to show you how to transform PDF to jpg/jpeg image in Java code. You can see in this example, only two steps can lead you getting the jpg image from PDF.
import com.pqscan.pdftoimage.PDFDocument; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class PDFtoJPGConverter { public static void main(String[] args) { try { //first, load PDF document to memory PDFDocument doc = new PDFDocument(); doc.loadPDF("Sample.pdf"); int pageCount = doc.getPageCount(); //second, convert each PDF page and save to jpg image for(int i = 0; i < pageCount; i++) { BufferedImage image = doc.toImage(i); ImageIO.write(image,"jpg", new File("output"+ i +".jpg")); } } catch (Exception e) { e.printStackTrace(); } } }