2

I'm using iText 5.5.9 inside my Android Studio project to make a PDF file.
When I add an image, the transparent parts turn to black inside the PDF.

How can I avoid that?

Here is the part of my code which illustrates the problem:

        // add card

        Resources res=getResources();
        Drawable drawable=res.getDrawable(R.drawable.card);
        Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        byte[] bitMapData = stream.toByteArray();
        Image img=Image.getInstance(bitMapData);

        img.scaleAbsolute(300f,156.3f);
        img.setSmask(false);
        doc.add(img);

enter image description here

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Amir Sfz
  • 21
  • 1
  • 3
  • 1
    try to compress with bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); instead of bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); may solve your problem – Pratik Tank Sep 07 '17 at 07:17
  • @pratik-tank Tried that already. the whole picture disappears :| – Amir Sfz Sep 07 '17 at 07:54
  • 2
    `setTransparency` see http://what-when-how.com/itext-5/making-images-transparent-itext-5/ and @PratikTank has a point: JPEG does not handle transparency, whereas PNG (and GIF somewhat) do. – Joop Eggen Sep 07 '17 at 14:04

2 Answers2

0

If you want to add a transparent circle image with broader (i.e. stroke) in iText, you can add a circle with stroke.

Use the code below (and change it to suit your needs) :

try {
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("myCirclepdffile.pdf"));
  document.open();

  PdfContentByte cb = writer.getDirectContent();

  cb.circle(250.0f, 500.0f, 200.0f);
  cb.stroke();

} catch (Exception e) {
  System.err.println(e.getMessage());
}
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Pratik Tank
  • 2,213
  • 1
  • 17
  • 29
  • I don't think this is the answer to the question. I think the OP wants to know if transparent images are supported in PDF or iText. The answer is: transparent images aren't supported in PDF, but they are supported in iText. Transparency in images in PDF is achieved by adding the opaque image along with an image mask that mimics the transparency. For every transparent image added to a PDF, iText adds two images: the opaque image and the mask. – Bruno Lowagie Sep 07 '17 at 14:48
0

Transparent images aren't supported in PDF (such as PNGs or GIFs with a color that acts as if it were transparent), but they are supported in iText.

Transparency in images in PDF is achieved by adding the opaque image along with an image mask that mimics the transparency. For every transparent image added to a PDF, iText adds two images: the opaque image and the mask.

This is true for PNG files and GIF files that support transparency. Unfortunetely, you are using the JPEG format, and JPEG doesn't support transparency. See Transparent background in JPEG image

Use PNG or GIF instead of JPEG.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165