- Copy pqScan.PDFtoImage.jar to your Java project library folder.
- Add pqScan.PDFtoImage.jar to your Java classpath.
How to Convert PDF to PNG in Java?
Getting PNG 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 png function in their Java application.
pq scan
Install Java PDF to Image Converter to your Java application
pq scan
Java PDF to PNG APIs
Before your conversion, we provide some useful properties and methods.
//open PDF document from the PDF file at the disk public void loadPDF(String fileName); //open PDF document from PDF stream public void loadPDF(InputStream stream); //return whole PDF page count, so you can have some idea of what was going on public int getPageCount(); //set resolution of output png image to improve the image quality public void setDPI(int dpi);
You can get png images from PDF pages with the size you wanting or as original image size.
//turn PDF to png image with default page size public BufferedImage toImage(int pageIndex); //turn PDF to png image with customized size public BufferedImage toImage(int pageIndex, int width, int height);
pq scan
Transforming PDF Pages to PNG in Java class
Look at the following Java sample code, it shows how to converting PDF to png images in just several lines code. It's really simple to understand and easy to use, isn't it?
import com.pqscan.pdftoimage.PDFDocument; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class PDFtoPNGConverter { public static void main(String[] args) { try { //initialize PDFDocument object PDFDocument doc = new PDFDocument(); //load PDF document doc.loadPDF("Sample.pdf"); //get total PDF page count int pageCount = doc.getPageCount(); for(int i = 0; i < pageCount; i++) { //convert each PDF page to BufferedImage object BufferedImage image = doc.toImage(i); //save image as png image file ImageIO.write(image,"png", new File("output"+ i +".png")); } } catch (Exception e) { e.printStackTrace(); } } }