Convert PNG to PDF iText Java Example Tutorial

In this code example, we will provide a standalone Java class which will convert a PNG image to a PDF document, by using the iText Java API. This example is on the same lines as our BMP to PDF Conversion tutorial. The difference lies in the class used to read the input PNG image file. We will use the class com.itextpdf.text.pdf.codec.PngImage to accept a PNG image and return an object of type com.itextpdf.text.Image, so that we can add it to the PDF document directly. Note that this example requires a basic understanding of iText, and if you are new to iText, then I would suggest using the search functionality on this blog to find some beginners tutorials on iText. As such the code to convert PNG to PDF is very straightforward, and you can follow it quite easily, if you know some Java basics. The commented version of the Java code for this example is provided below;
//FileOutputStream holds the Physical PDF file
import java.io.FileOutputStream;
//The image class which will hold the PNG image as an object
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Document;
//This class is required to read PNG image into Image object
import com.itextpdf.text.pdf.codec.PngImage;
public class PngToPDF {
public static void main(String[] args) {
try{
    //Create Document Object
    Document convertPngToPdf=new Document();
    //Create PdfWriter for Document to hold physical file
    //Change the PDF file path to suit to your needs
    PdfWriter.getInstance(convertPngToPdf, new FileOutputStream("c:\\java\\ConvertImagetoPDF.pdf"));
    convertPngToPdf.open();
    //Get the PNG image to Convert to PDF
    //getImage of PngImage class is a static method
    //Edit the file location to suit to your needs
    Image convertBmp=PngImage.getImage("c:\\java\\test.png");
    //Add image to Document
    convertPngToPdf.add(convertBmp);
    //Close Document
    convertPngToPdf.close();
    System.out.println("Converted and stamped PNG Image in a PDF Document Using iText and Java");
}
catch (Exception i1){
    i1.printStackTrace();
}
}
}
This example accepts a test image file and converts it to PDF. You may wish to use the methods inside the Image object to resize the image and fit to your needs. If you have a image resize / compression specific question on this example, you can post it as a comment in this blog. We will get back with a suitable working example based answer for your question. In the next blog post, we will update our generic image conversion servlet,to accept a user uploaded PNG image and render a PDF back to the browser.
Stay connected with Thinktibits through Facebook and Twitter (look to your left) and never miss a tutorial.

2 comments:

  1. Hi, How to set the size of converted PDF? I just got a PDF missing right part. Thanks in advanced!

    ReplyDelete
  2. I am more familiar with the method of convert pdf to png for c#, thanks to the author's method of sharing of java.

    ReplyDelete