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

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

Introduction

In this page you can find the example usage for com.itextpdf.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:edu.harvard.iq.dvn.core.web.ExploreDataPage.java

License:Apache License

private void writeImageFile(File fileIn, File pdfFileIn) {

    File imagePngFile = null;/*from  w  ww . j  av  a  2 s  . c o  m*/

    try {
        String decoded = URLDecoder.decode(imageURL, "UTF-8");

        if (!decoded.isEmpty()) {
            URL imageURLnew = new URL(imageURL);

            try {
                BufferedImage image = ImageIO.read(imageURLnew);

                BufferedImage combinedImage = getCompositeImage(image);

                if (fileIn != null) {
                    ImageIO.write(combinedImage, "png", fileIn);
                }

                if (pdfFileIn != null) {
                    imagePngFile = File.createTempFile("pdfDownload", "png");
                    ImageIO.write(combinedImage, "png", imagePngFile);
                    Document convertPngToPdf = new Document();
                    PdfWriter.getInstance(convertPngToPdf, new FileOutputStream(pdfFileIn, true));
                    convertPngToPdf.open();
                    Image convertBmp = PngImage.getImage(imagePngFile.getAbsolutePath());
                    convertBmp.scaleToFit(530f, 500f);
                    convertPngToPdf.add(convertBmp);
                    convertPngToPdf.close();
                }
            } catch (IIOException io) {
                System.out.println(io.getMessage().toString());
                System.out.println(io.getCause().toString());
                System.out.println("IIOException " + imageURLnew);

            } catch (FontFormatException ff) {
                System.out.println("FontFormatException " + imageURLnew);

                System.out.println("FontFormatException " + ff.toString());
            }
        }

    } catch (UnsupportedEncodingException uee) {
        System.out.println("UnsupportedEncodingException ");
    } catch (MalformedURLException mue) {
        System.out.println("MalformedURLException ");
    } catch (IOException io) {
        System.out.println("IOException - outer ");
    } catch (DocumentException io) {
        System.out.println("IOException - document ");
        System.out.println(io.getMessage());
    } finally {
        if (imagePngFile != null && imagePngFile.exists()) {
            if (!FileUtil.keepTempFiles("core.web.ExploreDataPage")) {
                imagePngFile.delete();
            }
        }
    }
}