Example usage for com.itextpdf.text Image getInstance

List of usage examples for com.itextpdf.text Image getInstance

Introduction

In this page you can find the example usage for com.itextpdf.text Image getInstance.

Prototype

public static Image getInstance(final PdfContentByte cb, final java.awt.Image awtImage, final float quality)
        throws BadElementException, IOException 

Source Link

Document

Gets an instance of a Image from a java.awt.Image.

Usage

From source file:com.datamyne.charts.FixingUglyDemoByScaling.java

License:Apache License

/**
 * Creates PDf file./*from   ww w.j a v  a 2 s  .c  om*/
 * @param outputStream {@link OutputStream}.
 * @throws DocumentException
 * @throws IOException
 */
public void create(OutputStream outputStream) throws DocumentException, IOException {
    Document document = null;
    PdfWriter writer = null;

    try {
        //instantiate document and writer
        document = new Document();
        writer = PdfWriter.getInstance(document, outputStream);

        //open document
        document.open();

        //add image
        int width = 300;
        int height = 300;
        float fWidth = 0.6f * width;
        float fHeight = 0.6f * height;
        JFreeChart chart = getChart();
        BufferedImage bufferedImage = chart.createBufferedImage(width, height);
        Image image = Image.getInstance(writer, bufferedImage, 1.0f);
        image.scaleAbsolute(fWidth, fHeight);
        document.add(image);

        //release resources
        document.close();
        document = null;

        writer.close();
        writer = null;
    } catch (DocumentException de) {
        throw de;
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        //release resources
        if (null != document) {
            try {
                document.close();
            } catch (Exception ex) {
            }
        }

        if (null != writer) {
            try {
                writer.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:com.datamyne.charts.UglyDemo.java

License:Apache License

/**
 * Creates PDf file./*from  w ww.j a  v a2s  .co  m*/
 * @param outputStream {@link OutputStream}.
 * @throws DocumentException
 * @throws IOException
 */
public void create(OutputStream outputStream) throws DocumentException, IOException {
    Document document = null;
    PdfWriter writer = null;

    try {
        //instantiate document and writer
        document = new Document();
        writer = PdfWriter.getInstance(document, outputStream);

        //open document
        document.open();

        //add image
        int width = 300;
        int height = 300;
        JFreeChart chart = getChart();
        BufferedImage bufferedImage = chart.createBufferedImage(width, height);
        Image image = Image.getInstance(writer, bufferedImage, 1.0f);
        document.add(image);

        //release resources
        document.close();
        document = null;

        writer.close();
        writer = null;
    } catch (DocumentException de) {
        throw de;
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        //release resources
        if (null != document) {
            try {
                document.close();
            } catch (Exception ex) {
            }
        }

        if (null != writer) {
            try {
                writer.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:de.tf.capmath.puzzle.pdf.PdfUtil.java

License:Open Source License

public static void image2Pdf(File pdf, BufferedImage image) throws IOException, DocumentException {
    logger.info(String.format("Writing PDF to %s", pdf.getAbsolutePath()));
    Document document = new Document(PageSize.A4, 5, 5, 5, 5);
    PdfWriter writer = null;//from  ww  w.j a v  a2s. c o m
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(pdf));
        document.open();
        PdfContentByte pdfCB = new PdfContentByte(writer);
        Image img = Image.getInstance(pdfCB, image, 1);
        document.add(img);
    } finally {
        document.close();
        writer.close();
    }
}

From source file:net.apocalypselabs.symat.TasksExport.java

License:Open Source License

private void savePdfFile(String html, String path) {
    Platform.runLater(new Runnable() {
        @Override// w ww  . j  a va2 s  .c  o m
        public void run() {
            try {
                String k = html;
                OutputStream file = new FileOutputStream(new File(path));
                Document document = new Document();
                PdfWriter writer = PdfWriter.getInstance(document, file);
                document.open();
                PdfContentByte pdfCB = new PdfContentByte(writer);
                WritableImage image = browser.snapshot(null, null);
                BufferedImage buffered = SwingFXUtils.fromFXImage(image, null);
                Image img = Image.getInstance(pdfCB, buffered, 1);
                document.open();
                document.add(img);
                document.close();
                savedMsg();
            } catch (Exception ex) {
                Debug.stacktrace(ex);
                java.awt.EventQueue.invokeLater(() -> {
                    JOptionPane.showInternalMessageDialog(Main.mainPane, "Error saving: " + ex.getMessage());
                });
            }
        }
    });
}