RubyPDF Blog Java,My Software,Open Source,PDF,tutorial PdftoImage-Convert PDF to Image by using PdfRenderer library

PdftoImage-Convert PDF to Image by using PdfRenderer library

Bases on PDF Renderer library, we can easily develop many useful tools. Here is a example I wrote today, the aim is converting every pages of a pdf to images (png, if you need other format, you can use JAI).

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.imageio.ImageIO;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class Pdf2Image {

	 * @param args
	public static void main(String[] args) {
			System.err.println("Usage:Pdf2Image pdf imageFolder");
		File file = new File(args[0]);
		RandomAccessFile raf;
		try {
			raf = new RandomAccessFile(file, "r");

			FileChannel channel = raf.getChannel();
			ByteBuffer buf =, 0, channel.size());
			PDFFile pdffile = new PDFFile(buf);
			// draw the first page to an image
			int num=pdffile.getNumPages();
			for(int i=0;i

Source Code of PDF2Image

6 thoughts on “PdftoImage-Convert PDF to Image by using PdfRenderer library”

  1. First of all, congratulations for your blog, it’s very usefull and interesting.
    After that, do you know how can I control the quality of the image result?, because is diferent the pdf size than the image result size.

  2. Thanks a lot for the code. But when I tried uploading the Fillable PDF, only empty template comes in the Image. Can you help on this.

  3. Thanks a lot for the code.I successfully converted PDF to images.But the images are of poor resolution.So is there anyway that I could get better resolution images from the source PDF as the images in the PDF are of good resolution.

  4. In the code it is mentioned that we are getting the width and height for the doc at the default zoom.How can I change the zoom option to manual instead of default?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.