Create pdf with image using java


import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDFWithImageDemo {
  public static void main(String[] args) {
         new GeneratePDFWithImageDemo().createPDF();

  public void createPDF(){
 Document document = new Document();
      try {
        new FileOutputStream("C://Test.pdf"));;
        PdfPTable table = new PdfPTable(1); //1 column.    
        Image image = Image.getInstance("C://Image/MyImage.jpg");
   image.scaleToFit(800f, 600f);
        image.scaleAbsolute(800f,600f);   // Give the size of image you want to print on pdf
PdfPCell nestedImgCell=new PdfPCell(image);
        document.close ();
      }catch (FileNotFoundException e) {
      } catch (DocumentException e) {
      }catch (Exception e) {
      System.out.println("======== PDF Created Successfully =========);

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Session timeout interceptor in struts 2