It works correctly but you are making the wrong assumption that writing to a BufferedImage will make the BufferedImage transparent. Another error is that you are converting the images to jpeg and jpeg doesn't support any transparency. Try:
 
Graphics2D g2d = template.createGraphics(240, 170, mapper);
//test PNG image
//http://images.linux.com/babytux.png
FileInputStream fileInputStream = new FileInputStream("C:/babytux.png");
BufferedImage image = ImageIO.read(fileInputStream);
g2d.setColor(Color.red);
g2d.fillRect(0, 0, 100, 200);
g2d.drawImage(image, 0, 0, 100, 200, null);
g2d.dispose();
contentByte.addTemplate(template, 0, 0);
You'll see the red background.
 
Paulo
 
 


From: Peter van Raamsdonk [mailto:[EMAIL PROTECTED]
Sent: Wednesday, March 22, 2006 9:57 AM
To: Paulo Soares
Cc: itext-questions@lists.sourceforge.net
Subject: Re: [iText-questions] Graphics2d + transparency

Thanks for the reply Paulo,

If transparency in Graphics2d should work or mostly like you say then I can figure out what can be done.

Here is a simple code snippet of the general idea;

I want the background to be transparent like it is. The image is just an test image, it can easily be a gif. I've checked but the imageIO doesn't muck up the real image (only if you want to edit :P).

public class Test {

public static void main(String args[]) {

try {

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, intermediateStream);

document.open();

PdfContentByte contentByte = writer.getDirectContent();

DefaultFontMapper mapper = new DefaultFontMapper();

PdfTemplate template = contentByte.createTemplate(240, 170);

Graphics2D g2d = template.createGraphics(240, 170, mapper, true, 1.0);

//test PNG image

//http://images.linux.com/babytux.png

FileInputStream fileInputStream = new FileInputStream("C:/babytux.png");

BufferedImage image = ImageIO.read(fileInputStream);

//if skipped background on GIF and PNG are black

if (image != null && (image.getType() == BufferedImage.TYPE_BYTE_INDEXED || image.getType() == BufferedImage.TYPE_CUSTOM)) {

BufferedImage bufferedImage= new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D g = bufferedImage.createGraphics();

g.setColor(g.getColor());

g.fillRect(0,0,image.getWidth(),image.getHeight());

g.drawImage(image, 0, 0, null);

g.dispose();

image = bufferedImage;

}

g2d.drawImage(image, 0, 0, 100, 200, null);

g2d.dispose();

contentByte.addTemplate(template, 0, 0);

document.close();

} catch ( Exception e ) {

e.printStackTrace();

}

}

}

Is there something I can try?

Greetings flattened Peter

Reply via email to