Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-2654

NullPointerException when reading a GIF file with a transparent color

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.8, 1.8.9, 2.0.0
    • 1.8.9, 2.0.0
    • PDModel
    • None
    • JDK 1.8.0_25 Running on Mac OsX 10.10.2

    Description

      NullPointerException while reading a gif file before writing it to a pdf file. I am able to write jpg's successfully to the pdf. The code snippet that I use to write to a pdf. Can you please help ?

      public void generatePDF() throws IOException,COSVisitorException,FileNotFoundException
      	{
      		PDDocument report_pdf = new PDDocument();
      		PDPage pdfpage1 = new PDPage(PDPage.PAGE_SIZE_LETTER);
      		PDRectangle text_dimensions = pdfpage1.getMediaBox();
      		PDXObjectImage pdfImage = null;
      		PDPageContentStream stream = new PDPageContentStream(report_pdf, pdfpage1);
      		stream.setFont(PDType1Font.HELVETICA, 30);
      		stream.beginText();
      		stream.moveTextPositionByAmount(130,text_dimensions.getHeight()-50);
      		stream.drawString("Hello");
      		stream.endText();
      		BufferedImage awtImage = ImageIO.read(new File("/var/tmp/test1.gif"));
      		 //pdfImage = new PDJpeg(report_pdf,awtImage);
      		pdfImage = new PDPixelMap(report_pdf, awtImage);
      		stream.drawImage(pdfImage, 60, 120 );
              //stream.drawXObject(pdfImage, 150, 400, pdfImage.getWidth()*10, pdfImage.getHeight()*20);
      		stream.close();
      		report_pdf.addPage(pdfpage1);
      		report_pdf.save(new File("/var/tmp/report.pdf"));
      	}
      

      Throws an error

      Exception in thread "main" java.lang.NullPointerException
      	at org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage.extractAlphaImage(PDXObjectImage.java:499)
      	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.createImageStream(PDPixelMap.java:122)
      	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.<init>(PDPixelMap.java:86)
      	at org.reports.ReportGenerator.ReportGenerator.generatePDF(ReportGenerator.java:122)
      

      Attachments

        1. PDFBOX-2654-target.pdf
          7 kB
          Tilman Hausherr
        2. report.pdf
          41 kB
          Shiva Sathiraju
        3. test1.gif
          3 kB
          Shiva Sathiraju

        Activity

          People

            tilman Tilman Hausherr
            Shiva Shiva Sathiraju
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: