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

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

Introduction

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

Prototype

public java.awt.Graphics2D createGraphics(float width, float height, FontMapper fontMapper) 

Source Link

Document

Gets a Graphics2D to write on.

Usage

From source file:at.granul.mason.collector.ChartFileScalarDataWriter.java

License:Open Source License

public static void exportGraph(XYChartGenerator chart, String prefix, int width, int height) {
    try {//  ww  w  . j a  va2 s .  c om
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));

        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(new File(prefix + "_" + DataWriter.DF.format(new Date()) + ".pdf")));

        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        //PdfTemplate tp = cb.createTemplate(width, height);

        //Write the chart with all datasets
        chart.addLegend();
        /*LegendTitle title = new LegendTitle(chart.getChart().getPlot());
        title.setLegendItemGraphicPadding(new org.jfree.ui.RectangleInsets(0,8,0,4));
        chart.addLegend(title);*/
        LegendTitle legendTitle = chart.getChart().getLegend();
        legendTitle.setPosition(RectangleEdge.BOTTOM);

        Graphics2D g2 = cb.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
        chart.getChart().draw(g2, rectangle2D);
        g2.dispose();

        //PNG Output
        ChartUtilities.saveChartAsJPEG(new File(prefix + "_" + DataWriter.DF.format(new Date()) + ".png"),
                chart.getChart(), width, height);

        chart.getChart().removeLegend();
        //tp = cb.createTemplate(width, height);

        //All invisible
        final XYItemRenderer renderer = chart.getChartPanel().getChart().getXYPlot().getRenderer();
        for (int a = 0; a < chart.getSeriesCount(); a++) {
            renderer.setSeriesVisible(a, false);
        }

        final Dataset seriesDataset = chart.getSeriesDataset();
        XYSeriesCollection series = ((XYSeriesCollection) seriesDataset);
        for (int a = 0; a < chart.getSeriesCount(); a++) {
            renderer.setSeriesVisible(a, true);
            final String seriesName = series.getSeries(a).getKey() + "";
            chart.setYAxisLabel(seriesName);
            document.newPage();
            g2 = cb.createGraphics(width, height * (a + 2), new DefaultFontMapper());
            g2.translate(0, height * (a + 1));
            chart.getChart().draw(g2, rectangle2D);
            g2.dispose();

            //PNG Output
            ChartUtilities.saveChartAsJPEG(
                    new File(prefix + "_" + seriesName + DataWriter.DF.format(new Date()) + ".png"),
                    chart.getChart(), width, height);

            renderer.setSeriesVisible(a, false);
        }
        //cb.addTemplate(tp, 0, 0);

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

From source file:clusterMaker.treeview.dendroview.GraphicsExportPanel.java

License:Open Source License

private void pdfSave(String format) {
    com.lowagie.text.Rectangle pageSize = PageSize.LETTER;
    Document document = new Document(pageSize);
    try {/*from  w  w w  .j a v  a  2  s.com*/
        OutputStream output = new BufferedOutputStream(new FileOutputStream(getFile()));
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Graphics2D g = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight(), new DefaultFontMapper());

        double imageScale = Math.min(pageSize.getWidth() / ((double) estimateWidth() + getBorderPixels()),
                pageSize.getHeight() / ((double) estimateHeight() + getBorderPixels()));
        g.scale(imageScale, imageScale);
        drawAll(g, 1.0);
        g.dispose();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, new JTextArea("Dendrogram export had problem " + e));
        logger.error("Exception " + e);
        // e.printStackTrace();
    }

    document.close();
}

From source file:edu.ucsf.rbvi.clusterMaker2.internal.treeview.dendroview.GraphicsExportPanel.java

License:Open Source License

private void pdfSave(String format) {
    com.lowagie.text.Rectangle pageSize = PageSize.LETTER;
    Document document = new Document(pageSize);
    try {//  w  ww .j av  a  2  s  . co m
        OutputStream output = new BufferedOutputStream(new FileOutputStream(getFile()));
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Graphics2D g = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight(), new DefaultFontMapper());

        double imageScale = Math.min(pageSize.getWidth() / ((double) estimateWidth() + getBorderPixels()),
                pageSize.getHeight() / ((double) estimateHeight() + getBorderPixels()));
        g.scale(imageScale, imageScale);
        drawAll(g, 1.0);
        g.dispose();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, new JTextArea("Dendrogram export had problem " + e));
        // logger.error("Exception " + e);
        // e.printStackTrace();
    }

    document.close();
}

From source file:org.cytoscape.io.internal.write.graphics.PDFWriter.java

License:Open Source License

@Override
public void run(TaskMonitor taskMonitor) throws Exception {
    // TODO should be accomplished with renderer properties
    // view.setPrintingTextAsShape(!exportTextAsFont);

    taskMonitor.setProgress(0.0);/* w  ww. j a  v a  2 s. c om*/
    taskMonitor.setStatusMessage("Creating PDF image...");

    logger.debug("PDF Rendering start");
    final Rectangle pageSize = PageSize.LETTER;
    final Document document = new Document(pageSize);

    logger.debug("Document created: " + document);

    final PdfWriter writer = PdfWriter.getInstance(document, stream);
    document.open();

    taskMonitor.setProgress(0.1);

    final PdfContentByte canvas = writer.getDirectContent();
    logger.debug("CB0 created: " + canvas.getClass());

    final float pageWidth = pageSize.getWidth();
    final float pageHeight = pageSize.getHeight();

    logger.debug("Page W: " + pageWidth + " Page H: " + pageHeight);
    final DefaultFontMapper fontMapper = new DefaultFontMapper();
    logger.debug("FontMapper created = " + fontMapper);
    Graphics2D g = null;
    logger.debug("!!!!! Enter block 2");

    engine.getProperties().setProperty("exportTextAsShape", new Boolean(!exportTextAsFont).toString());

    taskMonitor.setProgress(0.2);

    if (exportTextAsFont) {
        g = canvas.createGraphics(pageWidth, pageHeight, new DefaultFontMapper());
    } else {
        g = canvas.createGraphicsShapes(pageWidth, pageHeight);
    }

    taskMonitor.setProgress(0.4);

    logger.debug("##### G2D created: " + g);

    double imageScale = Math.min(pageSize.getWidth() / width, pageSize.getHeight() / height);
    g.scale(imageScale, imageScale);

    logger.debug("##### Start Rendering Phase 2: " + engine.toString());
    engine.printCanvas(g);
    logger.debug("##### Canvas Rendering Done: ");

    taskMonitor.setProgress(0.8);

    g.dispose();
    document.close();
    writer.close();

    stream.close();

    logger.debug("PDF rendering finished.");
    taskMonitor.setProgress(1.0);
}

From source file:org.forester.archaeopteryx.PdfExporter.java

License:Open Source License

static String writePhylogenyToPdf(final String file_name, final TreePanel tree_panel, int width, int height)
        throws IOException {
    if (height < HEIGHT_LIMIT) {
        height = HEIGHT_LIMIT;//w w  w .jav a 2 s.c o m
    }
    if (width < WIDTH_LIMIT) {
        width = WIDTH_LIMIT;
    }
    final Phylogeny phylogeny = tree_panel.getPhylogeny();
    if ((phylogeny == null) || phylogeny.isEmpty()) {
        return "";
    }
    if (tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1) {
        throw new IOException("fonts are too small for PDF export");
    }
    final File file = new File(file_name);
    if (file.isDirectory()) {
        throw new IOException("[" + file_name + "] is a directory");
    }
    final Document document = new Document();
    document.setPageSize(new Rectangle(width, height));
    document.setMargins(WIDTH_LIMIT / 2, WIDTH_LIMIT / 2, HEIGHT_LIMIT / 2, HEIGHT_LIMIT / 2);
    PdfWriter writer = null;
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(file_name));
    } catch (final DocumentException e) {
        throw new IOException(e);
    }
    document.open();
    final DefaultFontMapper mapper = new DefaultFontMapper();
    FontFactory.registerDirectories();
    if (Util.isWindows()) {
        mapper.insertDirectory("C:\\WINDOWS\\Fonts\\");
    } else if (Util.isMac()) {
        mapper.insertDirectory("/Library/Fonts/");
        mapper.insertDirectory("/System/Library/Fonts/");
    } else {
        mapper.insertDirectory("/usr/X/lib/X11/fonts/TrueType/");
        mapper.insertDirectory("/usr/X/lib/X11/fonts/Type1/");
        mapper.insertDirectory("/usr/share/fonts/default/TrueType/");
        mapper.insertDirectory("/usr/share/fonts/default/Type1/");
    }
    final PdfContentByte cb = writer.getDirectContent();
    final Graphics2D g2 = cb.createGraphics(width, height, mapper);
    try {
        tree_panel.paintPhylogeny(g2, true, false, width - WIDTH_LIMIT, height - HEIGHT_LIMIT, 0, 0);
    } catch (final Exception e) {
        Util.unexpectedException(e);
    } finally {
        try {
            g2.dispose();
            document.close();
        } catch (final Exception e) {
            //Do nothing.
        }
    }
    String msg = file.toString();
    if ((width > 0) && (height > 0)) {
        msg += " [size: " + width + ", " + height + "]";
    }
    return msg;
}

From source file:questions.graphics2D.ArabicText.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {//  w w w.jav  a 2s  . c  o m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        String text1 = "\u0634\u0627\u062f\u062c\u0645\u0647\u0648\u0631";
        String text2 = "\u0634";
        java.awt.Font font = new java.awt.Font("arial", 0, 12);
        PdfContentByte cb = writer.getDirectContent();

        java.awt.Graphics2D g2Shapes = cb.createGraphicsShapes(PageSize.A4.getWidth(), PageSize.A4.getHeight());
        g2Shapes.setFont(font);
        g2Shapes.drawString("text1, expected to render RTL", 50, 100);
        g2Shapes.drawString(text1, 50, 120);
        g2Shapes.drawString("text2, expected to match right-most glyph above", 50, 140);
        g2Shapes.drawString(text2, 50, 160);
        g2Shapes.dispose();

        ColumnText text = new ColumnText(cb);
        Font f = new Font(
                BaseFont.createFont("c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED),
                12);
        text.setSimpleColumn(50, 620, 545, 50);
        text.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        text.setText(new Phrase(text1, f));
        text.go();
        text.setText(new Phrase(text2, f));
        text.go();

        FontMapper arialuni = new FontMapper() {
            public BaseFont awtToPdf(java.awt.Font font) {
                try {
                    return BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H,
                            BaseFont.EMBEDDED);
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            public java.awt.Font pdfToAwt(BaseFont font, int size) {
                return null;
            }

        };
        java.awt.Graphics2D g = cb.createGraphics(PageSize.A4.getWidth(), PageSize.A4.getHeight(), arialuni);
        g.setFont(null);
        g.drawString("text1, not expected to render RTL", 50, 180);
        g.drawString(text1, 50, 200);
        g.drawString("text2, not expected to match right-most glyph above", 50, 220);
        g.drawString(text2, 50, 240);
        g.drawString("to your right you see what it SHOULD look like:", 50, 260);
        g.drawString("If it doesn't, the problem is in the JDK, it's not an iText problem.", 50, 280);
        g.dispose();
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:questions.graphics2D.SwingForceArialUni.java

public static void main(String[] args) {
    Document document = new Document(new Rectangle(210, 25));
    try {/*from w w  w .ja v  a2  s  . co m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        FontMapper arialuni = new FontMapper() {
            public BaseFont awtToPdf(Font font) {
                try {
                    return BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H,
                            BaseFont.EMBEDDED);
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            public Font pdfToAwt(BaseFont font, int size) {
                return null;
            }

        };
        Graphics2D g2 = cb.createGraphics(200, 50, arialuni);
        g2.setFont(null);
        g2.drawString("Greek mu: \u03bc - \u039c; degree symbol: \u00b0", 0, 40);
        g2.dispose();
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
}