简体   繁体   中英

PDFBox draw black image from BufferedImage

I try to draw an image from a bufferedImage into a PDF using PDFBox but fails, and I get black images and Acrobat Reader warns whith errors like "Out of memory" (but PDF is display).

I use a bufferedImage because I need to draw a JavaFX Image object (with came from call to Funciones.crearImagenDesdeTexto(), is a function which converts a text into an Image) into PDF. Rest of images works well without using bufferedimage.

    PDPixelMap img = null;
    BufferedImage bi;

    try {
        //If item has id, I try to get image with that id (image it's shows OK on PDF)
        img = new PDPixelMap(documento, read(getClass().getResourceAsStream("/com/img/" + item.getId() + ".png")));
    }
    catch (Exception e) {
        //If item has not id or fails load image, I create image on the fly (which contains item name. This not work on PDF, shows black images)
        bi = new BufferedImage(alto, ancho, BufferedImage.TYPE_INT_ARGB);
        bi.createGraphics().drawImage(SwingFXUtils.fromFXImage(Funciones.crearImagenDesdeTexto(item.getNombre()), null), ancho, alto, null);
        img = new PDPixelMap(documento, bi);
    }
    finally {
        contenedor.drawXObject(img, x, y, alto, ancho);
    }

NOTE: crearImagenDesdeTexto() returns a JavaFX Image Object that is create on the fly (I try this function in other parts of the program and works well, function is take from other stackOverflow response ).

Your code is confusing, you have three "new PDJpeg" and one of them is in a catch (which should just handle the error). And what does "read()" do? Does it pass a stream or a BufferedImage? If it is a stream, then it is wrong, because PDJpeg is for JPEGs, not for PNG.

The second one

img = new PDJpeg(documento, (getClass().getResourceAsStream("/com/img/" + Byte.toString(item.getId()) + ".png")));

is definitively wrong for the same reason: PDJPeg is not for PNG files / streams.

If you want to create an image from a PNG file / stream, use PDPixelMap.

It is possible to create a PDJpeg object from a BufferedImage, but this is recommended only if the image wasn't encoded before. Because if you would read a BufferedImage from a JPEG, and then use PDJPeg for this, you'll have a slight loss of quality as the image is decoded and encoded again (JPEG is a "lossy" compression format).

If my advice doesn't help, please upload the JPEG file and the PDF somewhere.

Also make sure that you're using the latest version, which is 1.8.7.

Update after comments: the parameters to createGraphics.drawImage() should be 0, 0 and not width, height. The two parameters are a location, not a size.

Finally, I find a solution (thanks also to Tilman Hausherr):

private void dibujarImagen(Item i, int x, int y, int alto, int ancho) throws IOException {
    PDPixelMap img = null;

    try {
        img = new PDPixelMap(documento, read(getClass().getResourceAsStream("/com/img/" + i.getId() + ".png")));
    }
    catch (IllegalArgumentException e) {
        img = new PDPixelMap(documento, SwingFXUtils.fromFXImage(Funciones.crearImagenDesdeTexto(i.getNombre()),null));
    }
    finally {
        contenedor.drawXObject(img, x, y, alto, ancho);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM