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

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

Introduction

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

Prototype

public PdfTemplate createTemplate(float width, float height) 

Source Link

Document

Creates a new template.

Usage

From source file:de.xirp.chart.ChartUtil.java

License:Open Source License

/**
 * Exports the given chart as PDF in the specified size. The
 * export is written to the given path./*from   w ww.  j  a  v a 2s .c om*/
 * 
 * @param chart
 *            The chart to export.
 * @param width
 *            The desired width of the PDF.
 * @param height
 *            The desired height of the PDF.
 * @param path
 *            The path to write the PDF to.
 * @see org.jfree.chart.JFreeChart
 */
private static void exportPDF(JFreeChart chart, int width, int height, String path) {
    Document document = new Document(new Rectangle(width, height));
    try {
        PdfWriter writer;
        writer = PdfWriter.getInstance(document, new FileOutputStream(path));
        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);
    } catch (Exception e) {
        logClass.error("Error: " + e.getMessage() //$NON-NLS-1$
                + Constants.LINE_SEPARATOR, e);
    }
    document.close();
}

From source file:dr.app.tracer.analysis.TemporalAnalysisFrame.java

License:Open Source License

public final void doExportPDF() {
    FileDialog dialog = new FileDialog(this, "Export PDF Image...", FileDialog.SAVE);

    dialog.setVisible(true);/*from   w  w  w.  j  av a  2  s .  c o m*/
    if (dialog.getFile() != null) {
        File file = new File(dialog.getDirectory(), dialog.getFile());

        Rectangle2D bounds = temporalAnalysisPlotPanel.getExportableComponent().getBounds();
        Document document = new Document(
                new com.lowagie.text.Rectangle((float) bounds.getWidth(), (float) bounds.getHeight()));
        try {
            // step 2
            PdfWriter writer;
            writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            // step 3
            document.open();
            // step 4
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate((float) bounds.getWidth(), (float) bounds.getHeight());
            Graphics2D g2d = tp.createGraphics((float) bounds.getWidth(), (float) bounds.getHeight(),
                    new DefaultFontMapper());
            temporalAnalysisPlotPanel.getExportableComponent().print(g2d);
            g2d.dispose();
            cb.addTemplate(tp, 0, 0);
        } catch (DocumentException de) {
            JOptionPane.showMessageDialog(this, "Error writing PDF file: " + de, "Export PDF Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(this, "Error writing PDF file: " + e, "Export PDF Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        document.close();
    }
}

From source file:dr.app.tracer.application.TracerFrame.java

License:Open Source License

public final void doExportPDF() {
    FileDialog dialog = new FileDialog(this, "Export PDF Image...", FileDialog.SAVE);

    dialog.setVisible(true);//w w w. j  ava 2  s. c  o m
    if (dialog.getFile() != null) {
        File file = new File(dialog.getDirectory(), dialog.getFile());

        Rectangle2D bounds = tracePanel.getExportableComponent().getBounds();
        Document document = new Document(
                new com.lowagie.text.Rectangle((float) bounds.getWidth(), (float) bounds.getHeight()));
        try {
            // step 2
            PdfWriter writer;
            writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            // step 3
            document.open();
            // step 4
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate((float) bounds.getWidth(), (float) bounds.getHeight());
            Graphics2D g2d = tp.createGraphics((float) bounds.getWidth(), (float) bounds.getHeight(),
                    new DefaultFontMapper());
            tracePanel.getExportableComponent().print(g2d);
            g2d.dispose();
            cb.addTemplate(tp, 0, 0);
        } catch (DocumentException de) {
            JOptionPane.showMessageDialog(this, "Error writing PDF file: " + de, "Export PDF Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(this, "Error writing PDF file: " + e, "Export PDF Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        document.close();
    }
}

From source file:ec.display.chart.StatisticsChartPaneTab.java

License:Academic Free License

/**
 * This method initializes jButton  /*from   w  w w. ja  va 2 s .  co m*/
 *  
 * @return javax.swing.JButton      
 */
private JButton getPrintButton() {
    if (printButton == null) {
        printButton = new JButton();
        printButton.setText("Export to PDF...");
        final JFreeChart chart = chartPane.getChart();
        printButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                try

                {
                    int width = chartPane.getWidth();
                    int height = chartPane.getHeight();

                    FileDialog fileDialog = new FileDialog(new Frame(), "Export...", FileDialog.SAVE);
                    fileDialog.setDirectory(System.getProperty("user.dir"));
                    fileDialog.setFile("*.pdf");
                    fileDialog.setVisible(true);
                    String fileName = fileDialog.getFile();
                    if (fileName != null)

                    {
                        if (!fileName.endsWith(".pdf")) {
                            fileName = fileName + ".pdf";
                        }
                        File f = new File(fileDialog.getDirectory(), fileName);
                        Document document = new Document(new com.lowagie.text.Rectangle(width, height));
                        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
                        document.addAuthor("ECJ Console");
                        document.open();
                        PdfContentByte cb = writer.getDirectContent();
                        PdfTemplate tp = cb.createTemplate(width, height);
                        Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
                        Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
                        chart.draw(g2, rectangle2D);
                        g2.dispose();
                        cb.addTemplate(tp, 0, 0);
                        document.close();
                    }
                } catch (Exception ex)

                {
                    ex.printStackTrace();
                }
            }
        });
    }
    return printButton;
}

From source file:edu.gmu.cs.sim.util.media.PDFEncoder.java

License:Academic Free License

public static void generatePDF(Component component, File file) {
    int width = component.getWidth();
    int height = component.getHeight();
    try {//from   ww w.  j  ava2  s.  c  om
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics g2 = tp.createGraphics(width, height, new DefaultFontMapper());
        component.paint(g2);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.gmu.cs.sim.util.media.PDFEncoder.java

License:Academic Free License

public static void generatePDF(JFreeChart chart, int width, int height, File file) {
    try {/*from   w  w w.  j a va2 s  .  co  m*/
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2, rectangle2D);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:gov.noaa.pfel.coastwatch.sgt.SgtUtil.java

License:Open Source License

/**
 * This creates a file to capture the pdf output generated by calls to 
 * graphics2D (e.g., use makeMap).//  w  ww  .j  a  va  2  s.c  om
 * This will overwrite an existing file.
 * 
 * @param pageSize e.g, PageSize.LETTER or PageSize.LETTER.rotate() (or A4, or, ...)
 * @param width the bounding box width, in 1/144ths of an inch
 * @param height the bounding box height, in 1/144ths of an inch
 * @param outputStream
 * @return an object[] with 0=g2D, 1=document, 2=pdfContentByte, 3=pdfTemplate
 * @throws Exception if trouble
 */
public static Object[] createPdf(com.lowagie.text.Rectangle pageSize, int bbWidth, int bbHeight,
        OutputStream outputStream) throws Exception {
    //currently, this uses itext
    //see the sample program:
    //  file://localhost/C:/programs/iText/examples/com/lowagie/examples/directcontent/graphics2D/G2D.java
    //Document.compress = false; //for test purposes only
    Document document = new Document(pageSize);
    document.addCreationDate();
    document.addCreator("gov.noaa.pfel.coastwatch.SgtUtil.createPdf");

    document.setPageSize(pageSize);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();

    //create contentByte and template and Graphics2D objects
    PdfContentByte pdfContentByte = writer.getDirectContent();
    PdfTemplate pdfTemplate = pdfContentByte.createTemplate(bbWidth, bbHeight);
    Graphics2D g2D = pdfTemplate.createGraphics(bbWidth, bbHeight);

    return new Object[] { g2D, document, pdfContentByte, pdfTemplate };
}

From source file:is.idega.idegaweb.egov.printing.business.DocumentBusinessBean.java

License:Open Source License

private PdfTemplate createPasswordLetterTemplate(PdfWriter writer) throws Exception {
    IWBundle iwb = getIWApplicationContext().getIWMainApplication()
            .getBundle(is.idega.idegaweb.egov.message.business.MessageConstants.IW_BUNDLE_IDENTIFIER);
    PdfContentByte cb = writer.getDirectContent();

    float tempLength = 511f;
    float tempHeight = getPointsFromMM(40);
    PdfTemplate template = cb.createTemplate(tempLength, tempHeight);

    if (addTemplateHeader()) {
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        String mail_zip = iwb.getProperty("commune.mail_zip");
        String mail_name = iwb.getProperty("commune.mail_name");

        float convLengt = 100f;
        float convHeight = 60f;
        template.rectangle(0f, 0f, convLengt, convHeight);
        template.moveTo(0f, 0f);//from  w  w w  .j  a v  a 2  s  .c o m
        template.lineTo(convLengt, convHeight);
        template.moveTo(convLengt, 0f);
        template.lineTo(0f, convHeight);
        template.stroke();
        template.beginText();
        template.setFontAndSize(bf, 11f);
        template.setTextMatrix(5f, 40f);
        template.showText(mail_name);

        template.endText();
        template.beginText();
        template.setFontAndSize(bf, 11f);
        template.setTextMatrix(5f, 25f);
        template.showText(mail_zip);
        template.endText();

        Image porto = Image.getInstance(iwb.getResourcesRealPath() + "/shared/porto_betalt.jpg");
        porto.scaleAbsolute(60f, 60f);

        // Image portoA =Image.getInstance(iwb.getResourcesRealPath()+
        // "/shared/porto_a_logo.jpg");
        // float Awidth = 2.3f*60f;
        // portoA.scaleToFit(Awidth,60f);

        float portoXPos = tempLength - 90f;
        // float portoAXPos = portoXPos-Awidth-5f;
        template.addImage(porto, 60f, 0f, 0f, 60f, portoXPos, 0);
        // template.addImage(portoA,Awidth,0f,0f,60f,portoAXPos,0);
    }

    return template;
}

From source file:javaaxp.xps2pdf.service.impl.PDFConverterImpl.java

License:Open Source License

@Override
public void covertToPDF(OutputStream ouput) throws XPSError {
    try {//from  w ww.j a  v a 2s . co m
        int firstPage = fPageController.getXPSAccess().getPageAccess(0).getFirstPageNum();
        int lastPage = fPageController.getXPSAccess().getPageAccess(0).getLastPageNum();

        Document document = new Document();
        document.setPageCount(lastPage - firstPage + 1);
        document.setPageSize(PageSize.LETTER);
        PdfWriter writer = PdfWriter.getInstance(document, ouput);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        for (int i = firstPage; i < 1; i++) {
            System.out.println("Converting page " + i);
            fPageController.setPage(i);
            PdfTemplate tp = cb.createTemplate((float) fPageController.getPage().getWidth(),
                    (float) fPageController.getPage().getHeight());
            Graphics g = tp.createGraphics((float) fPageController.getPage().getWidth(),
                    (float) fPageController.getPage().getHeight());
            JComponent toReturn = fPageViewer.getPageRenderer().getRendererComponent();
            toReturn.paint(g);
            cb.addTemplate(tp, 0, 0);
            document.newPage();
        }
        document.close();
    } catch (DocumentException e) {
        //rethrow
    }
}

From source file:jmbench.plots.UtilPlotPdf.java

License:Open Source License

public static void saveAsPdf(JFreeChart chart, String FILENAME, int width, int height) {
    File parent = new File(new File(FILENAME).getParent());
    if (!parent.exists()) {
        if (!parent.mkdirs())
            throw new RuntimeException("Can't make directory path");
    }//from  w w w .j av a 2  s.c  o  m

    Document document = new Document(new Rectangle(width, height));
    try {
        FileOutputStream file = new FileOutputStream(FILENAME);
        PdfWriter writer = PdfWriter.getInstance(document, file);
        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();
        g2d.dispose();
    } catch (Exception e) {
        e.printStackTrace();
    }
}