Tuesday, June 25, 2013

PDF To JPG Converter

The PDFToJPGConverter program is able to convert all pages of a PDF document to JPG image format. Each page is converted to one image file.
In Java, it is simple to convert a PDF document to images by using PdfBox library. You can download the library jar file at http://pdfbox.apache.org/.  One additional library that you also need is FontBox. It can be dowloaded  from http://sourceforge.net/projects/fontbox/.


pdf to jpg converter java

PDFToJPGConverter source code:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import org.pdfbox.TextToPDF;
import org.pdfbox.exceptions.COSVisitorException;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDPage;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

public class PDFToJPGConverter{
public static void main(String[] args){

selectPdf();

}

//allow images selection for converting
public static void selectPdf(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF","pdf");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(false);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();  
            convertPDFToJPG(file.toString());
            }

     
}

public static void convertPDFToJPG(String src){

try{

//load pdf file in the document object
PDDocument doc=PDDocument.load(new FileInputStream(src));
//Get all pages from document and store them in a list
List<PDPage> pages=doc.getDocumentCatalog().getAllPages();
//create iterator object so it is easy to access each page from the list
Iterator<PDPage> i= pages.iterator();
int count=1; //count variable used to separate each image file
//Convert every page of the pdf document to a unique image file
System.out.println("Please wait...");
while(i.hasNext()){
PDPage page=i.next();
BufferedImage bi=page.convertToImage();
ImageIO.write(bi, "jpg", new File("pdfimage"+count+".jpg"));
count++;
}
System.out.println("Conversion complete");
}catch(IOException ie){ie.printStackTrace();}
}



}

In the example code above, the PDDocument class is used to create a pdf document from the InputStream object. The InputStream object contains all data of the original pdf file. Once you have the pdf document object, you can get all pages of the pdf document by using the getAllPages method from the document catalog object. The document catalog object is returned by the getDocumentCatalog method of the pdf document object. You will use the List data structure to store all pages of the pdf document. Each page of the pdf document is represented by the PDPage object. From this object, you can convert the page to the BufferedImage object by using the convertToImage method.

PDF To Word Converter, convert PDF To Word

13 comments:

  1. May I suggest that you look at our library as well? It is called jPDFImages, it is 100% Java and commercial, starting at $400 a server.

    Our library supports all types of fonts, images and transparency.

    Sample code would be:

    import com.qoppa.pdfImages.PDFImages;

    public class PDFToJPEGs
    {
    public static void main (String [] args)
    {
    try
    {
    PDFImages pdfDoc = new PDFImages ("input.pdf", null);
    for (int count = 0; count < pdfDoc.getPageCount(); ++count)
    {
    pdfDoc.savePageAsJPEG(count, "output_" + count + ".jpg", 150, 0.8f);
    }
    }
    catch (Throwable t)
    {
    t.printStackTrace();
    }
    }
    }

    ReplyDelete
  2. I have found java code for converting a particular or all pages of pdf to jpeg format but it requires Aspose.PDF for Java Library to use this code properly. It also offers trial version so you can use that also.

    Below is the code:

    Convert particular PDF page to JPEG Image

    //open document
    com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
    // create stream object to save the output image
    java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image.jpg");

    //create JPEG device with specified attributes
    //Quality [0-100], 100 is Maximum
    //create Resolution object
    com.aspose.pdf.Resolution resolution = new com.aspose.pdf.Resolution(300);
    //create JpegDevice object where second argument indicates the quality of resultant image
    com.aspose.pdf.JpegDevice jpegDevice = new com.aspose.pdf.JpegDevice(resolution, 100);
    //convert a particular page and save the image to stream
    jpegDevice.process(pdfDocument.getPages().get_Item(1), imageStream);

    //close the stream
    imageStream.close();

    Convert all PDF pages to JPEG Images

    //open document
    com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");

    // loop through all the pages of PDF file
    for (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++)
    {
    // create stream object to save the output image
    java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".jpg");


    //create Resolution object
    com.aspose.pdf.Resolution resolution = new com.aspose.pdf.Resolution(300);
    //create JpegDevice object where second argument indicates the quality of resultant image
    com.aspose.pdf.JpegDevice jpegDevice = new com.aspose.pdf.JpegDevice(resolution, 100);
    //convert a particular page and save the image to stream
    jpegDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream);

    //close the stream
    imageStream.close();

    ReplyDelete
  3. Fantastic blog you got here,I enjoyed reading some of your posts.
    JPG to PDF Converter

    ReplyDelete
  4. BufferedImage bi=page.convertToImage();
    convertToImage method doesn't exist in the page object now.
    COuld you please provide the alternate code for the same,

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. with version

      public static void convertPDFtoIMG(String file) throws IOException {
      FileInputStream pdfInputStream = null;
      try {
      pdfInputStream = new FileInputStream(file);
      } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      PDDocument pdDocument = null;
      try {
      pdDocument = PDDocument.load(pdfInputStream);
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      PDPage pdPage = pdDocument.getPage(0);
      PDResources pdResources = pdPage.getResources();

      Iterable cosList = pdResources.getXObjectNames();
      for (COSName key : cosList) {
      PDXObject xobject = pdResources.getXObject(key);
      if (xobject instanceof PDImageXObject) {
      PDImageXObject imageObject = (PDImageXObject) xobject;
      BufferedImage image = imageObject.getImage();
      File yourImageFile = new File("D:/barcode/page_"+ ".png");
      ImageIO.write( image,"png",yourImageFile);
      }
      }
      }

      Delete
  5. public static void convertPDFtoIMG(String file) throws IOException {
    FileInputStream pdfInputStream = null;
    try {
    pdfInputStream = new FileInputStream(file);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    PDDocument pdDocument = null;
    try {
    pdDocument = PDDocument.load(pdfInputStream);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    PDPage pdPage = pdDocument.getPage(0);
    PDResources pdResources = pdPage.getResources();

    Iterable cosList = pdResources.getXObjectNames();
    for (COSName key : cosList) {
    PDXObject xobject = pdResources.getXObject(key);
    if (xobject instanceof PDImageXObject) {
    PDImageXObject imageObject = (PDImageXObject) xobject;
    BufferedImage image = imageObject.getImage();
    File yourImageFile = new File("D:/barcode/page_"+ ".png");
    ImageIO.write( image,"png",yourImageFile);
    }
    }
    }

    ReplyDelete
  6. Most scanners additionally output and spare information as PDF documents. At times the scanner can make PDF records or JPG pictures. https://www.altoconvertpdftopng.com/faq

    ReplyDelete
  7. Generally, PDF records found on the web are the littlest, most reduced available for use. https://altomergepdf.com/blog/how_to_merge_pdf_files

    ReplyDelete
  8. This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. Youconvertit.com is offering you the best PDF to JPG converter online with just one click. Visit us on Convert ogg to mp3

    ReplyDelete
  9. Thanks a lot for sharing java program that can Convert pdf to jpg. This is an amazing blog explaining core concepts.

    ReplyDelete