Convert PDF to Image in Java

By using Spire.PDF for JAVA, we can easily convert any specific page of PDF document to BMP and Metafile image in JAVA applications. The following sample demonstrate how to convert PDF to image in JAVA. The supported images could be in Jpeg, Jpg, Png, Bmp, Tiff, Gif, EMF format etc. We will use PDF to .png image for example.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import com.spire.pdf.PdfDocument;
import javax.imageio.ImageIO;

public class toImage {

        public static void main(String[] args) throws IOException {
            //load the sample PDF
            PdfDocument doc = new PdfDocument();
            doc.loadFromFile("Sample.pdf");

            //save every PDF to .png image
            BufferedImage image;
            for (int i = 0; i < doc.getPages().getCount(); i++) {
                image = doc.saveAsImage(i);
                File file = new File( String.format("ToImage-img-%d.png", i));
                ImageIO.write(image, "PNG", file);
            }

            doc.close();
        }

}

The sample PDF file:

Convert PDF to Image in Java

Effective screenshot of the resulted PNG image:

Convert PDF to Image in Java