Example usage for com.lowagie.text.pdf.codec PngImage getImage

List of usage examples for com.lowagie.text.pdf.codec PngImage getImage

Introduction

In this page you can find the example usage for com.lowagie.text.pdf.codec PngImage getImage.

Prototype

public static Image getImage(byte data[]) throws IOException 

Source Link

Document

Reads a PNG from a byte array.

Usage

From source file:com.songbook.pc.exporter.PdfExporter.java

License:Open Source License

private Element buildQrCodeSection() throws IOException, DocumentException {
    // Load images
    Image qrApkImage = PngImage
            .getImage(PdfExporter.class.getResourceAsStream("/export/qr/songbook_apk_qr.png"));
    Image qrPdfImage = PngImage//from   w ww  . ja  v  a  2s. com
            .getImage(PdfExporter.class.getResourceAsStream("/export/qr/songbook_pdf_qr.png"));

    PdfPCell cell = new PdfPCell((Phrase) null);
    cell.setBorder(PdfPCell.NO_BORDER);
    cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
    cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);

    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(100);
    table.setWidths(new float[] { 1, 2, 1 });

    cell.setImage(qrApkImage);
    table.addCell(cell);
    cell.setImage(null);

    table.addCell(cell);

    cell.setImage(qrPdfImage);
    table.addCell(cell);
    cell.setImage(null);

    cell.setPhrase(new Phrase("APP"));
    table.addCell(cell);
    cell.setPhrase(null);

    table.addCell(cell);

    cell.setPhrase(new Phrase("PDF"));
    table.addCell(cell);
    cell.setPhrase(null);

    return table;
}

From source file:org.geomajas.plugin.print.component.impl.LegendViaUrlComponentImpl.java

License:Open Source License

@Override
public void calculateSize(PdfContext context) {

    if (null == getLegendImageServiceUrl()) {
        log.error("getLegendImageServiceUrl() returns unexpectedly with NULL");
        setBounds(new Rectangle(0, 0));
        return; // Abort
    }/*from   w  w  w.j a v  a 2  s .  c  o  m*/

    String locale = getLocale();
    try {
        if (null != locale && !locale.isEmpty()) {
            resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, new Locale(locale));
        } else {
            resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
        }
    } catch (MissingResourceException e) {
        resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, new Locale("en'"));
    }

    if (getConstraint().getMarginX() <= 0.0f) {
        getConstraint().setMarginX(MARGIN_LEFT_IMAGE_RELATIVE_TO_FONTSIZE * getLegend().getFont().getSize());
    }
    if (getConstraint().getMarginY() <= 0.0f) {
        getConstraint().setMarginY(MARGIN_TOP_IMAGE_RELATIVE_TO_FONTSIZE * getLegend().getFont().getSize());
    }

    @SuppressWarnings("deprecation")
    float width = getConstraint().getWidth();
    @SuppressWarnings("deprecation")
    float height = getConstraint().getHeight();

    // Retrieve legend image from URL if not yet retrieved
    if (null == image && visible && null == warning) {
        if (getLegendImageServiceUrl().contains("=image/png")) {
            // Its approx. 2 times faster to use PngImage.getImage() instead of Image.getInstance()
            // since the latter will retrieve the URL twice!
            try {
                image = PngImage.getImage(new URL(getLegendImageServiceUrl()));
                // Image.getInstance(new URL(getLegendImageServiceUrl()));
                image.setDpi(DPI_FOR_IMAGE, DPI_FOR_IMAGE); // Increase the precision
            } catch (MalformedURLException e) {
                log.error("Error in Image.getInstance() for URL " + getLegendImageServiceUrl(), e);
                e.printStackTrace();
            } catch (IOException e) {
                // This exception is OK if no legend image is generated because out of scale range
                // for a dynamic layer, then a text message which indicates an invisible legend is referred
                // to by the URL 
                visible = !hasInVisibleResponse();
                if (visible) {
                    log.warn("Unexpected IOException for Image.getInstance() for URL "
                            + getLegendImageServiceUrl(), e);
                }
            }
        } else {
            try {
                image = Image.getInstance(new URL(getLegendImageServiceUrl()));
                image.setDpi(DPI_FOR_IMAGE, DPI_FOR_IMAGE); // Increase the precision
            } catch (BadElementException e) {
                log.error("Error in Image.getInstance() for URL " + getLegendImageServiceUrl(), e);
                e.printStackTrace();
            } catch (MalformedURLException e) {
                log.error("Error in Image.getInstance() for URL " + getLegendImageServiceUrl(), e);
                e.printStackTrace();
            } catch (IOException e) {
                // This exception is OK if no legend image is generated because out of scale range
                // for a dynamic layer, then a text message which indicates an invisible legend is referred
                // to by the URL 
                visible = !hasInVisibleResponse();
                if (visible) {
                    log.warn("Unexpected IOException for Image.getInstance() for URL "
                            + getLegendImageServiceUrl(), e);
                }
            }

        }
    }
    if (!visible) {
        setBounds(new Rectangle(0, 0));
    } else if (null == image) {
        generateWarningMessage(context);
    } else {

        if (width <= 0 && height <= 0) {
            // when / 2.0f: The image is generated with a scale of 1:0.5 (but looks awful!)
            width = image.getWidth(); // 2.0f;
            height = image.getHeight(); // 2.0f;
        } else if (width <= 0) {
            width = image.getWidth() / image.getHeight() * height;
        } else if (height <= 0) {
            height = image.getHeight() / image.getWidth() * width;
        }

        setBounds(new Rectangle(width, height)); // excluding the marginX
    }
}