Gelly
Gelly

Reputation: 127

PDF file turns 0 kb when image is added in IText

I'm trying to create a pdf file which displays something like name, stop number, and fare. The code compiles and run perfectly, but when I tried to add an image in it, the pdf file it created was 0 kb and was damaged. I've been stuck in here for hours and still can't find what's wrong in my code. I just want to add an image on my pdf file. Any help would be appreciated. Thanks.

public class PDFDisplay {

public static void generatePDF(PassengerBean passengerBean) throws DocumentException, MalformedURLException, IOException {
     Document document = new Document();

     try {
         final Chunk NEWLINE = new Chunk("\n");
          PdfWriter.getInstance(document,
              new FileOutputStream("C:\\sample.pdf"));

          document.open();

          Image img = Image.getInstance("image/mrt.jpg");
            document.add(new Paragraph("Sample 1: This is simple image demo."));
            document.add(img);


          String hr = "-----------------------------------------------------------";
          String spacer = " ";
          String name = "Passenger Name: " + passengerBean.lname + "," +  " " + passengerBean.fname;
          String dest = "Destination: " + passengerBean.dest + " STATION";
          String stopno = passengerBean.stop;
          double fare = passengerBean.fare;
          String fare1 = "Fare: PHP" + " " + String.valueOf(fare);
          String ccnum = "CREDIT CARD NUMBER: " + " " + "************" + passengerBean.ccnum.substring(Math.max(0, passengerBean.ccnum.length() - 4));

            Paragraph para8 = new Paragraph(32);
            para8.setSpacingBefore(100);
            para8.setSpacingAfter(10);
            para8.add(new Chunk(spacer));
            document.add(para8);

            Paragraph para9 = new Paragraph(32);
            para9.setSpacingBefore(100);
            para9.setSpacingAfter(10);
            para9.add(new Chunk(hr));
            document.add(para9);

            // Setting paragraph line spacing to 32
            Paragraph para1 = new Paragraph(32);
            para1.setSpacingBefore(5);
            para1.setSpacingAfter(10);
            para1.add(new Chunk(name));
            document.add(para1);

            Paragraph para2 = new Paragraph();
            para2.setSpacingAfter(10);
            para2.add(new Chunk(dest));
            document.add(para2);

            Paragraph para3 = new Paragraph();
            para3.setSpacingAfter(10);
            para3.add(new Chunk(stopno));
            document.add(para3);

            Paragraph para4 = new Paragraph();
            para4.setSpacingAfter(10);
            para4.add(new Chunk(fare1));
            document.add(para4);

            Paragraph para5 = new Paragraph();
            para5.setSpacingAfter(10);
            para5.add(new Chunk(ccnum));
            document.add(para5);


          document.close();



        } catch (DocumentException e) {
          e.printStackTrace();
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
}
}

Upvotes: 2

Views: 891

Answers (1)

Bhavik vora
Bhavik vora

Reputation: 274

There might be possibility that you are not able to get image. Try to print url of you image using

System.out.print(img.getUrl());

I have tested your code to my local and its providing proper pdf file in output.

Upvotes: 1

Related Questions