Example usage for com.itextpdf.text.pdf PdfContentByte createTemplate

List of usage examples for com.itextpdf.text.pdf PdfContentByte createTemplate

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte createTemplate.

Prototype

public PdfTemplate createTemplate(final float width, final float height) 

Source Link

Document

Creates a new template.

Usage

From source file:mymoney.view.java

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
    getContentPane().setLayout(new BorderLayout());
    DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
    JFileChooser cho = new JFileChooser();
    cho.showSaveDialog(null);//from   www . j  av a 2  s  .  c  om
    File f = cho.getSelectedFile();
    String filename = f.getAbsolutePath();
    com.itextpdf.text.Document document = new com.itextpdf.text.Document();

    try {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename + ".pdf"));
        document.open();

        PdfContentByte cb = writer.getDirectContent();
        cb.saveState();
        PdfTemplate pdfTemplate = cb.createTemplate(jTable2.getWidth(), jTable2.getHeight());

        Graphics2D g2 = cb.createGraphicsShapes(800, 500);
        Shape oldClip = g2.getClip();
        g2.clipRect(0, 0, 800, 500);
        jTable2.print(g2);
        g2.setClip(oldClip);
        g2.dispose();
        cb.restoreState();
        JOptionPane.showMessageDialog(null, "Data Exported to pdf");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error");
    }
    document.close(); // TODO add your handling code here:
}

From source file:net.pickapack.chart.ChartPdfExporter.java

License:Open Source License

/**
 * Export the specified chart to the specified PDF file.
 *
 * @param chart the chart/* ww w  . j av a2 s .co m*/
 * @param width the width of the PDF file
 * @param height the height of the PDF file
 * @param fileName the PDF file name
 */
public static void exportPdf(JFreeChart chart, int width, int height, String fileName) {
    try {
        Document document = new Document(new Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2d, r2d);
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

License:Open Source License

/**
 * This method saves a chart as a PDF with given dimensions
 * //w  w w  . j a  va 2  s  .  c  o  m
 * @param chart
 * @param width
 * @param height
 * @param fileName is a full path
 */
public static void writeChartToPDF(JFreeChart chart, int width, int height, File fileName) throws Exception {
    PdfWriter writer = null;

    Document document = new Document(new Rectangle(width, height));

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        document.close();
    }
}

From source file:nz.ac.waikato.cms.supernova.io.PDF.java

License:Open Source License

/**
 * Generates the intermediate data structure.
 *
 * @param test      the test results (measure - [score, percentile])
 * @param angle      the angle to use//from   www .  ja v  a  2s  . c om
 * @param numFlips      the number of flips
 * @param overallFlipCycles   the overall flip cycles
 * @param errors      for storing error messages
 * @return         null if successfully generated, otherwise error message
 */
public ByteArrayOutputStream generatePlot(Map<String, List<Double>> test, double angle,
        Map<String, Integer> numFlips, int overallFlipCycles, StringBuilder errors) {
    ByteArrayOutputStream result;
    Document document;
    PdfWriter writer;
    PdfContentByte canvas;
    PdfTemplate template;
    Graphics2D g;

    result = new ByteArrayOutputStream();
    g = null;
    document = null;
    try {
        document = new Document(new Rectangle(m_Width, m_Height));
        writer = PdfWriter.getInstance(document, result);
        document.open();
        canvas = writer.getDirectContent();
        template = canvas.createTemplate(m_Width, m_Height);
        g = new PdfGraphics2D(template, m_Width, m_Height);
        draw(g, test, angle, numFlips, overallFlipCycles, errors);
        canvas.addTemplate(template, 0, 0);
    } catch (Exception e) {
        errors.append(Utils.throwableToString(e));
    } finally {
        try {
            if (g != null)
                g.dispose();
        } catch (Exception e) {
            // ignored
        }
        try {
            if (document != null)
                document.close();
        } catch (Exception e) {
            // ignored
        }
    }

    return result;
}

From source file:org.fhaes.fhsamplesize.view.SSIZCurveChart.java

License:Open Source License

/**
 * Save chart as PDF file. Requires iText library.
 * /*from   www  .j a v  a  2s .  c om*/
 * @param chart JFreeChart to save.
 * @param fileName Name of file to save chart in.
 * @param width Width of chart graphic.
 * @param height Height of chart graphic.
 * @throws Exception if failed.
 * @see <a href="http://www.lowagie.com/iText">iText</a>
 */
@SuppressWarnings("deprecation")
public static void writeAsPDF(File fileToSave, int width, int height) throws Exception {

    if (chart != null) {
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(fileToSave.getAbsolutePath()));

            // convert chart to PDF with iText:
            Rectangle pagesize = new Rectangle(width, height);
            Document document = new Document(pagesize, 50, 50, 50, 50);
            try {
                PdfWriter writer = PdfWriter.getInstance(document, out);
                document.addAuthor("JFreeChart");
                document.open();

                PdfContentByte cb = writer.getDirectContent();
                PdfTemplate tp = cb.createTemplate(width, height);
                Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());

                Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
                chart.draw(g2, r2D, null);
                g2.dispose();
                cb.addTemplate(tp, 0, 0);
            } finally {
                document.close();
            }
        } finally {
            if (out != null)
                out.close();
        }
    }
}

From source file:org.fhaes.neofhchart.PDFExportOptionsDialog.java

License:Open Source License

/**
 * Performs the export operation using the currentChart as the source.
 * /*from w w  w. j a va2 s  .  c o m*/
 * @return true if the operation completed successfully, false otherwise
 */
private boolean doExportToPDF() {

    boolean completedSuccessfully = false;

    if (currentChart != null) {
        log.debug("Exporting to PDF...");
        Document document = null;

        if (cboPaperSize.getSelectedItem() instanceof Rectangle) {
            Rectangle rect = (Rectangle) cboPaperSize.getSelectedItem();

            if (radLandscape.isSelected()) {
                rect = rect.rotate();
            }

            document = new Document(rect, 10, 10, 10, 10);
        } else {
            Rectangle rect = new Rectangle(currentChart.getTotalWidth(), currentChart.getTotalHeight());
            document = new Document(rect, 10, 10, 10, 10);
        }

        try {
            currentChart.setVisibilityOfNoExportElements(false);

            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream(outputFile.getAbsolutePath()));
            document.open();

            int width = (int) document.getPageSize().getWidth();
            int height = (int) document.getPageSize().getHeight();

            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate template = cb.createTemplate(width, height);

            @SuppressWarnings("deprecation")
            Graphics2D g2 = template.createGraphics(width, height);

            PrintTranscoder prm = new PrintTranscoder();
            TranscoderInput ti = new TranscoderInput(currentChart.getSVGDocument());
            prm.transcode(ti, null);

            PageFormat pg = new PageFormat();
            Paper pp = new Paper();
            pp.setSize(width, height);
            pp.setImageableArea(0, 0, width, height);
            pg.setPaper(pp);
            prm.print(g2, pg, 0);
            g2.dispose();

            ImgTemplate img = new ImgTemplate(template);
            document.add(img);

            completedSuccessfully = true;
        } catch (DocumentException e) {
            System.err.println(e);
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            currentChart.setVisibilityOfNoExportElements(true);
        }

        document.close();
    }

    return completedSuccessfully;
}

From source file:org.imos.abos.plot.JfreeChartDemo.java

License:Open Source License

protected void createPDF(String filename) {
    try {/*w  w  w  .  j  av  a  2s .co  m*/
        Rectangle page = PageSize.A4.rotate();

        // step 1
        Document document = new Document(page);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        float width = page.getWidth();
        float height = page.getHeight();
        // add chart
        PdfTemplate pie = cb.createTemplate(width, height);
        Graphics2D g2d1 = new PdfGraphics2D(pie, width, height);
        Rectangle2D r2d1 = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2d1, r2d1);
        g2d1.dispose();
        cb.addTemplate(pie, 0, 0);
        // step 5
        document.close();
    } catch (DocumentException ex) {
        Logger.getLogger(JfreeChartDemo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(JfreeChartDemo.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println("PDF finsihed");
}

From source file:org.jfree.chart.swt.ChartPdf.java

License:Open Source License

protected static void writeChart(JFreeChart chart, int width, int height, PdfContentByte cb) {
    PdfTemplate tp = cb.createTemplate(width, height);
    Graphics2D g2 = new PdfGraphics2D(cb, width, height);
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
    chart.draw(g2, r2D, null);// w w w. j a  va  2s  .c om
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
}

From source file:org.niord.core.fm.pdf.SVGReplacedElement.java

License:Apache License

/** {@inheritDoc} **/
@Override//from w ww  . j  a  v  a 2s.  co m
public void paint(RenderingContext renderingContext, ITextOutputDevice outputDevice, BlockBox blockBox) {
    PdfContentByte cb = outputDevice.getWriter().getDirectContent();
    float width = cssWidth / outputDevice.getDotsPerPoint();
    float height = cssHeight / outputDevice.getDotsPerPoint();

    PdfTemplate template = cb.createTemplate(width, height);
    Graphics2D g2d = new PdfGraphics2D(template, width, height);
    PrintTranscoder prm = new PrintTranscoder();
    TranscoderInput ti = new TranscoderInput(svg);
    prm.transcode(ti, null);
    PageFormat pg = new PageFormat();
    Paper pp = new Paper();
    pp.setSize(width, height);
    pp.setImageableArea(0, 0, width, height);
    pg.setPaper(pp);
    prm.print(g2d, pg, 0);
    g2d.dispose();

    PageBox page = renderingContext.getPage();
    float x = blockBox.getAbsX() + page.getMarginBorderPadding(renderingContext, CalculatedStyle.LEFT);
    float y = (page.getBottom() - (blockBox.getAbsY() + cssHeight))
            + page.getMarginBorderPadding(renderingContext, CalculatedStyle.BOTTOM);
    x /= outputDevice.getDotsPerPoint();
    y /= outputDevice.getDotsPerPoint();

    cb.addTemplate(template, x, y);
}

From source file:org.openscience.cdk.applications.taverna.basicutilities.ChartTool.java

License:Open Source License

/**
 * Adds a chart to the pdf file.//from   w  w w.j a  v a  2 s .  c om
 * 
 * @param chart
 * @param cb
 */
private void addChartPageToPDF(JFreeChart chart, PdfContentByte cb) {
    PdfTemplate tp = cb.createTemplate(this.width, this.height);
    Graphics2D g2 = tp.createGraphics(this.width, this.height, new DefaultFontMapper());
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, this.width, this.height);
    chart.draw(g2, r2D);
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
}