PDFBox从BufferedImage绘制黑色图像 [英] PDFBox draw black image from BufferedImage

查看:66
本文介绍了PDFBox从BufferedImage绘制黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PDFBox将图像从bufferedImage绘制为PDF,但失败,并且我得到黑色图像,Acrobat Reader警告出现诸如内存不足"之类的错误(但显示PDF).

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).

我使用bufferedImage是因为我需要将JavaFX Image对象(来自对Funciones.crearImagenDesdeTexto()的调用,该对象将文本转换为Image)转换为PDF.其余图像在不使用bufferedimage的情况下效果很好.

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);
    }

注意:crearImagenDesdeTexto()返回动态创建的JavaFX图像对象(我在程序的其他部分尝试了此功能,效果很好,该功能取自

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).

推荐答案

最后,我找到了解决方案(也感谢Tilman Hausherr):

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);
    }
}

这篇关于PDFBox从BufferedImage绘制黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆