Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBOX JPG image is working but PNG not working

I had print an image into 'PDF' using the following code:

InputStream in = new FileInputStream(new File("C:/"+imageName));
PDJpeg img = new PDJpeg(doc, in);
contentStream.drawXObject(img, 20, pageYaxis-120, 80, 80);

Here when imagName="a.jpg" its working fine, In case of imagName="b.png" its not working. In jpg images its working but in png its not. Why it is so? Please help me. How can I make print both the formats, I mean format in depended?

like image 266
androidGenX Avatar asked Dec 12 '22 03:12

androidGenX


2 Answers

In Apache PDFBox 1.8, use PDPixelMap for PNG images:

BufferedImage awtImage = ImageIO.read(new File(image));
ximage = new PDPixelMap(doc, awtImage);

In the source code of PDFBox, see the ImageToPDF.java example. This will work with all files that can be read with ImageIO. However it is still useful to keep using PDJpeg for JPG images, because there the JPEG files are directly put into the PDF files without being converted into a lossless format.

like image 117
Tilman Hausherr Avatar answered Dec 13 '22 15:12

Tilman Hausherr


Bitmap alphaImage = BitmapFactory.decodeStream(in);
PDImageXObject alphaXimage = LosslessFactory.createFromImage(document, alphaImage);
like image 44
Kalanithi Elangovan Avatar answered Dec 13 '22 16:12

Kalanithi Elangovan