Monday, June 24, 2013

JPG To PDF Converter

The JPGToPDFConverter program is able to convert an image to a PDF file. You can convert different image formats (e.g jpg, png, gif, bmp, ...) to PDF files. When the program runs, it allows you to select many image files as you want. Each image file is converted to a PDF file. The PDF file is stored in the current working folder.
By using the JPGToPDFConverter, you are not worry about a big image that can't fit well on the PDF page. For the big image, it is resized automatically to fit the A4 paper size.


JPG to image PDF converter java

JPGToPDFConverter source code:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

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

selectImage();
}


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

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif","png","bmp");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(true);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File[] Files=chooser.getSelectedFiles();
System.out.println("Please wait...");
            for( int i=0;i<Files.length;i++){    
            convertJPGToPDF(Files[i].toString(),"pdfimage"+i+".pdf");
            }
System.out.println("Conversion complete");
        }
}

public static void convertJPGToPDF(String strimg,String desc){
try{
//create document object
Document doc=new Document();
//create pdf writer object to write the document to the output file
PdfWriter.getInstance(doc,new FileOutputStream(desc));
//get a4 paper size
Rectangle r=PageSize.A4;
//read the image
BufferedImage orImg=ImageIO.read(new File(strimg));
//initialize image width and height
int width=orImg.getWidth();
int height=orImg.getHeight();
//resize the image that is bigger than A4 size
if(width>r.getWidth())
width=(int)r.getWidth();
if(height>r.getHeight())
height=(int)r.getHeight();
//create a blank buffered image
BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//create graphic2d object from the buffered image
Graphics2D g2d=bi.createGraphics();
//draw the original image on the buffered image
//so the image is resized to fit the A4 paper size if it is bigger than A4 size
g2d.drawImage(orImg, 0,0,width,height,null);
//store the image data in memory
ByteArrayOutputStream bas=new ByteArrayOutputStream();
ImageIO.write(bi, "png", bas);
//create image from the image data stored in memory
Image img=Image.getInstance(bas.toByteArray());
//centrally align the image on the pdf page
img.setAlignment(Element.ALIGN_CENTER);
//open document
doc.open();
//add image to the document
doc.add(img);
//close the document
doc.close();


}catch(Exception e){e.printStackTrace();}
}



}

The process of converting an image to a PDF file in the code above is simple. Firstly, you need to have a BufferedImage object of the original image file. This can be done by using the read method of the ImageIO class. The BufferedImage is resized to fit the A4 paper size if its size is greater than the A4 paper size. This is to make sure that the output PDF file does not contain an image that greater than its size. For the image that is not larger than the A4 paper size, its size remains unchanged. You will need to convert the BufferedImage object back to an Image object since the add method of the Document object of the iText library accepts only the image object ( not the BufferedImage object). This can be done by using the getInstance method of the Image class in iText library. When the image is ready, you can call the add method of the Document object to add the image to the Document object.

12 comments:

  1. Replies
    1. "This is an awesome resource! Really makes it easy to convert images to PDF. I didn't have to download anything and it was really easy to use. You can also convert all image formats, it's great. It converts JPG to PDF but also PNG, BMP, TIF or GIF”

      Delete
  2. Thanks for sharing the code i will try it. I am using Aspose.PDF for Java Library for managing my PDF Documents including converting them to image. I will try your code and will see the difference in result.

    ReplyDelete
  3. what version of itext library you have used??????

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. where can i get output PDF file....
    output path???

    ReplyDelete
  6. change the desc String and it is now working perfectly

    ReplyDelete
  7. how was the output looks. can u show a sample output.

    ReplyDelete
    Replies
    1. there is PdfWriter.getInstance(doc,new FileOutputStream(desc));
      You can give your path to save for "desc" here.

      Delete
  8. I can't do 10 pages in pdf from 10 images. I mean how can I create multipages pdf by this? this merge my all images and give me my last image to the pdf

    ReplyDelete