bad png image exported to pdf from itext

1

I am developing an app using the itext library.

When I access one of my images hosted in the drawable folder, it is not copied correctly. It is a logo, and it has a transparent part. When included in the document, the background (what is transparent) becomes black. So the image does include it, but with the black background.

This is my code:

      Drawable d = getResources ().getDrawable (R.drawable.logopeq223);
      Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
      byte[] bitmapData = stream.toByteArray();
      Image imageLogo = Image.getInstance(bitmapData);
      imageLogo.scaleAbsolute(70,40);
      imageLogo.setAlignment(Element.ALIGN_RIGHT);
      document.add(imageLogo);
    
asked by Sergio Cv 22.08.2016 в 15:00
source

2 answers

1

Sergio, if you really want to use iText it should be like this:

Document document = new Document(PageSize.A4, 30, 30, 30, 30);
//Establece el nombre del archivo .pdf que se obtendra al agregar la imagen.
PdfWriter.getInstance(document, new FileOutputStream("C:/imagen_a_pdf.pdf"));
document.open();
//Imagen a agregar al .pdf.
Image myImage = Image.getInstance(getClass().getResource("/mi_imagen.png"));
document.add(myImage);
document.close();

or similar to your code, loading an image of / assets:

InputStream inputStream = MainActivity.this.getAssets().open("myimagen.png");
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image signature = Image.getInstance(stream.toByteArray());
signature.setAbsolutePosition(70, 40f);
signature.scalePercent(100f);
document.add(signature);
document.close();

Here you can see more examples.

    
answered by 22.08.2016 / 16:43
source
1
  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

Look at this line, in it you say that you change the format to .jpeg with 100% quality.

That is, it is no longer the PNG with transparency

    
answered by 22.08.2016 в 15:14