2

I'm trying to add a PNG image to an existing pdf, but the transparency is converted to black color.

        PdfReader reader = new PdfReader(pdfPath);
        File f = new File(pdfPath);
        String result = f.getParent() + File.separator + UUID.randomUUID().toString() + ".pdf";
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(result));
        Image image = Image.getInstance(ImageIO.read(new File(imagePath)), null);
        PdfImage stream = new PdfImage(image, null, null);
        PdfIndirectObject ref = stamper.getWriter().addToBody(stream);      
        image.setDirectReference(ref.getIndirectReference());
        image.setAbsolutePosition(30, 300);
        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.addImage(image);
        stamper.close();
        reader.close();

How can I keep transparency?

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Roman
  • 115
  • 2
  • 10
  • @Bruno Strictly speaking this is not a duplicate as this question is about adding images with transparency to a PDF and the referenced duplicate question is about extracting them. Admittedly, though, the basic misunderstanding is the same in both... – mkl Dec 12 '17 at 22:08
  • @BrunoLowagie, tell me, please, what mask should I use? Sorry for this question. I have one task - to add a PNG image to the PDF. I'm not a PDF guru, but I also do not have time to study many documentation – Roman Dec 12 '17 at 22:16
  • OK, @mkl, I closed the question too quickly. I saw: black background, and I thought: image mask missing. Now I am looking at the code, and I see: *O boy! where did the OP find this clunky code?* – Bruno Lowagie Dec 12 '17 at 22:42
  • @Bruno *"where did the OP find this clunky code?"* - :))) yes indeed! – mkl Dec 12 '17 at 23:09
  • @mkl I wrote code like that in an example to show how one could add extra keys to the stream dictionary of an image. It would be amazing if someone looking how to add an image using `PdfStamper` would find only that example, assuming that's the best way to do it. What are the odds that such a person wouldn't read any further documentation? – Bruno Lowagie Dec 12 '17 at 23:25
  • *"What are the odds that such a person wouldn't read any further documentation?"* - about 100% for a certain type of persons. – mkl Dec 13 '17 at 10:53

1 Answers1

2

First this: I am violating the policy at iText Software by answering this question. You are using an old version of iText, and the policy dictates that voluntary support on iText 5 or earlier has stopped. You should either use iText 7, or you should get a support contract if you still want support for an old iText version.

However, I am curious. I want to know where you found this clunky code (or why you decided to write this code):

Image image = Image.getInstance(ImageIO.read(new File(imagePath)), null);
PdfImage stream = new PdfImage(image, null, null);
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);      
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(30, 300);
PdfContentByte canvas = stamper.getOverContent(1);
canvas.addImage(image);

You don't need ImageIO and you don't need to create a PdfImage, nor do you need to add that image to the body of a PDF file. The code you are using is code specialists would use for a very particular purpose. If you know that particular purpose, please explain.

If adding an image at an absolute position is all you want to do (that's a general purpose, not a particular purpose), your code should be as simple as this:

Image image = Image.getInstance(imagePath);
image.setAbsolutePosition(30, 300);
PdfContentByte canvas = stamper.getOverContent(1);
canvas.addImage(image);

In this case, you don't have to worry about the image mask; iText will take care of that for you.

Please also explain why you're using an outdated version of iText instead of iText 7. If you want your application to be future-proof, you should upgrade to iText 7 now (to avoid wasting time later).

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 2
    This is an old question but I can add my reason of not upgrading from iText 5 to iText 7: iText 7 is six times larger library than iText 5. If I develop just a simple command line utility with very few dependencies, the utility is in fact as large as the iText library. And making the utility six times larger is not worth. – Cimlman Oct 08 '20 at 15:50