androidGenX
androidGenX

Reputation: 1148

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?

Upvotes: 4

Views: 5249

Answers (2)

Kalanithi Elangovan
Kalanithi Elangovan

Reputation: 31

Bitmap alphaImage = BitmapFactory.decodeStream(in);
PDImageXObject alphaXimage = LosslessFactory.createFromImage(document, alphaImage);

Upvotes: 1

Tilman Hausherr
Tilman Hausherr

Reputation: 18851

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.

Upvotes: 12

Related Questions