0

When attempting to include some jpeg files into a PDF using iText I get an error: Premature EOF while reading JPG

The images are loaded from android phones and most can be embedded into the pdf file, however some can not.

PushbuttonField ad = pdfForm.getNewPushbuttonFromField(fieldName);
if(ad != null) {
    ad.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
    ad.setProportionalIcon(true);
    try {
        ad.setImage(Image.getInstance(basePath + "/" + r.value));
    } catch (Exception e) {
        log.log(Level.SEVERE, "Image error detail", e);
    }
    pdfForm.replacePushbuttonField(fieldName, ad.getField());
} 

The error occurs during setImage at: com.itextpdf.text.Jpeg.processParameters(Jpeg.java:219) iText version is: 5.5.5

I have put an image that causes the error onto a public dropbox folder: https://dl.dropboxusercontent.com/u/46349359/image.jpg The image is 1.6 MB and is displayed without a problem in html or using other image display tools.

nap
  • 226
  • 1
  • 2
  • 9
  • But is the image present in the specified location? – user207421 Jul 25 '16 at 00:19
  • Yes it is present. – nap Jul 25 '16 at 00:52
  • Following up on my previous comment I ran some tests where I renamed the file so it would not be found. The error reported was then java.io.FileNotFoundException. – nap Jul 25 '16 at 01:18
  • Confirmed its a duplicate question. Thanks Amedee. I ran imagemagick: convert x.jpg y.jpg. The resulting y.jpg file was accepted by iText. I guess I have a workaround although in most cases this is an unnecessary step. I think that iText should be able to read these files as other image processing applications can. – nap Jul 25 '16 at 05:25
  • Actually with imagemagick the following will do the trick: convert x.jpg x.jpg – nap Jul 25 '16 at 05:34

1 Answers1

0

As stated by Amedee this question was a duplicate. The specific resolution that has worked for me is to:

  1. install imageMagick
  2. Prior to adding image file image.jpg into a PDF using iText run: "convert image.jpg image.jpg"
  3. Then add the jpeg
nap
  • 226
  • 1
  • 2
  • 9