0

I am using Graphics2D itext feature and draw image with g2.drawImage(x, y, null). If i load this image using ImageIO.read() image looks black and white in generated PDF. But Toolkit.getDefaultToolkit().createImage() works fine. Here is the code:

public static final String filename = "dummy.pdf";
public static void main(String[] args) {
    try {
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(filename));
        writer.setPdfVersion(PdfWriter.VERSION_1_5);

        doc.open();
        PdfContentByte cb = writer.getDirectContent();
        Graphics2D g2 = cb.createGraphics(1654, 1168);
        draw(g2);


        doc.close();
        System.out.println("Done!");
    } catch(Exception e) {
        e.printStackTrace();
    }
}


private static void draw(Graphics2D g2) throws Exception {
    g2.setColor(Color.red);
    g2.fill(new Rectangle2D.Double(0, 0, 100, 100));

    BufferedImage img = ImageIO.read(new File("Speedy2BigClr.gif"));
    BufferedImage bi = toBufferedImage(img);
    Image i = makeColorTransparentAndBW(bi, Color.WHITE);

    Image iii = Toolkit.getDefaultToolkit().createImage("Speedy2BigClr.gif");

    g2.translate(0, 300);
    g2.scale(0.3, 0.3);
    g2.drawImage(img, 0, 0, null);

}
}

Unfortunately for another gif the ImageIO works fine, and Toolkit does not work. But all images look fine on screen. Why the way i load image affects the result in PDF?

enter image description here

P.S. For the clear reasons i am using LGPL iText (2.0.4, 2.1.7, 4.2.0). UPD: works fine on iText-5.3.5, that i can not use :-(

AvrDragon
  • 7,139
  • 4
  • 27
  • 42
  • *For the clear reasons i am using itext-2.0.4* --- which clear reasons? Even if you are required to use a LGPL/MPL version, you can use 2.1.7 or the unofficial 4.2 with many many bugs fixed since 2.0.4. More fixes and features certainly can be found in even newer versions. – mkl Feb 12 '13 at 15:37
  • @mkl tested it either under 2.1.7 and 4.02, with the same result – AvrDragon Feb 12 '13 at 15:48
  • Ok, will look into it later. – mkl Feb 12 '13 at 15:50
  • I kind of remember that there were some fixes in the image handling in the 5.x history. You might have run into one of the issues. I'm afraid that you will have to parallely fix the bug in an own branch of the pre-5.x itext and use that branch. – mkl Feb 12 '13 at 23:03

0 Answers0