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

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

Introduction

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

Prototype

public void addTemplate(PdfTemplate template, float x, float y) 

Source Link

Document

Adds a template to this content.

Usage

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);/*from  w ww. j a  v  a 2s  .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  . j  a  v  a  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 {//www . j  a v a 2s .  c o 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);
        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 {/* w  ww  .j ava2 s  . c o  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:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java

License:GNU General Public License

/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
 *      com.lowagie.text.Document)/*from w w  w  .  ja v  a 2 s  .  c o  m*/
 */
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headertable
    table.setTotalWidth(document.right() - document.left());
    table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 20, cb);
    // compose the footer
    String text = writer.getPageNumber() + " / ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 49;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    float adjust = helv.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust - 5, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);
    cb.saveState();
    // draw a Rectangle around the page
    cb.setLineWidth(1);
    cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
    cb.stroke();
    cb.restoreState();
}

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

License:Open Source License

private void addTemplateToPage(PdfTemplate template, PdfWriter writer, String type) throws Exception {
    if (template != null) {

        if (type.equals(MessageConstants.LETTER_TYPE_PASSWORD)) {
            PdfContentByte cb = writer.getDirectContent();
            cb.addTemplate(template, getPointsFromMM(15f), getPointsFromMM(297 - 22));
        }/*from ww  w.  java 2 s . co m*/
    }

}

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

License:Open Source License

@Override
public void covertToPDF(OutputStream ouput) throws XPSError {
    try {//  ww  w . ja  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");
    }// www. j a v a2  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();
    }
}

From source file:jmbench.plots.UtilPlotPdf.java

License:Open Source License

public static void saveAsPdf(LegendTitle legend, String FILENAME, int width, int height) {
    Document document = new Document(new Rectangle(width, height));
    try {/*from ww  w  . ja  v  a  2s. c o m*/
        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);
        legend.draw(g2d, r2d);
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
        g2d.dispose();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:joelib2.io.types.PDF.java

License:Open Source License

/**
 *  Writes a molecule with his <tt>PairData</tt> .
 *
 * @param  mol              the molecule with additional data
 * @param  title            the molecule title or <tt>null</tt> if the title
 *      from the molecule should be used
 * @param  writePairData    if <tt>true</tt> then the additional molecule data
 *      is written//from  w  w w . j  av  a 2 s . c o  m
 * @param  attribs2write    Description of the Parameter
 * @return                  <tt>true</tt> if the molecule and the data has
 *      been succesfully written.
 * @exception  IOException  Description of the Exception
 */
public boolean write(Molecule mol, String title, boolean writePairData, List attribs2write,
        SMARTSPatternMatcher smarts) throws IOException {
    if (firstMoleculeWritten == false) {
        document.open();
        firstMoleculeWritten = true;
    }

    Dimension d = new Dimension(Mol2Image.instance().getDefaultWidth(),
            Mol2Image.instance().getDefaultHeight());
    RenderingAtoms container = new RenderingAtoms();
    container.add(mol);

    RenderHelper.translateAllPositive(container);
    RenderHelper.scaleMolecule(container, d, 0.8);
    RenderHelper.center(container, d);

    Renderer2D renderer = new Renderer2D();

    //BaseFont helvetica = null;
    try {
        BaseFont.createFont("Helvetica", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    } catch (DocumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    int w = d.width;
    int h = d.height;
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(w, h);
    Graphics2D g2 = tp.createGraphics(w, h);
    g2.setStroke(new BasicStroke(0.1f));
    tp.setWidth(w);
    tp.setHeight(h);

    g2.setColor(renderer.getRenderer2DModel().getBackColor());
    g2.fillRect(0, 0, d.width, d.height);

    if (smarts != null) {
        renderer.selectSMARTSPatterns(container, smarts);
    }

    renderer.paintMolecule(container, g2);

    g2.dispose();

    ////cb.addTemplate(tp, 72, 720 - h);
    //cb.addTemplate(tp, 12, 720 - h);
    cb.addTemplate(tp, 0, document.getPageSize().height() - h);

    //     Mol2Image.instance().mol2image(mol);
    BaseFont bf = null;

    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    } catch (DocumentException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    String string = "";

    //float myBorder = DEFAULT_BORDER;
    //float fontSize = 10;
    //float fontSizeDelta = DEFAULT_FONT_OFFSET;
    float hpos;

    if (writePairData) {
        PairData pairData;
        PairDataIterator gdit = mol.genericDataIterator();
        int index = 0;
        boolean firstPageWritten = false;

        List attributesV;

        if (attribs2write == null) {
            // write all descriptors
            attributesV = new Vector();

            //DescResult tmpPropResult;
            while (gdit.hasNext()) {
                pairData = gdit.nextPairData();

                attributesV.add(pairData.getKey());
            }
        } else {
            attributesV = attribs2write;
        }

        // sort descriptors by attribute name
        String[] attributes = new String[attributesV.size()];

        for (int i = 0; i < attributesV.size(); i++) {
            attributes[i] = (String) attributesV.get(i);
        }

        Arrays.sort(attributes);

        // write them
        for (int i = 0; i < attributes.length; i++) {
            pairData = mol.getData(attributes[i]);
            string = pairData.getKey() + " = " + pairData.toString();

            // reduce too complex data
            string = string.replace('\n', ' ');
            string = string.substring(0, Math.min(string.length(), WRITE_MAX_CHARACTERS));

            tp = cb.createTemplate(document.getPageSize().width() - pageBorder, fontSize + fontSizeDelta);
            tp.setFontAndSize(bf, fontSize);
            tp.beginText();
            tp.setTextMatrix(0, fontSizeDelta);
            tp.showText(string);
            tp.endText();
            cb.setLineWidth(1f);
            tp.moveTo(0, 0);
            tp.lineTo(document.getPageSize().width() - (2 * pageBorder), 0);
            tp.stroke();

            if (firstPageWritten) {
                hpos = document.getPageSize().height() - ((fontSize + fontSizeDelta) * (index + 1));
            } else {
                hpos = document.getPageSize().height() - h - ((fontSize + fontSizeDelta) * (index + 1));
            }

            if (hpos < pageBorder) {
                index = 1;
                firstPageWritten = true;
                hpos = document.getPageSize().height() - ((fontSize + fontSizeDelta) * (index + 1));

                try {
                    document.newPage();
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            cb.addTemplate(tp, pageBorder, hpos);

            index++;
        }
    }

    try {
        document.newPage();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return (true);
}