Search in sources :

Example 1 with DefaultFontMapper

use of com.itextpdf.awt.DefaultFontMapper in project clusterMaker2 by RBVI.

the class GraphicsExportPanel method pdfSave.

private void pdfSave(String format) {
    com.itextpdf.text.Rectangle pageSize = PageSize.LETTER;
    Document document = new Document(pageSize);
    try {
        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();
}
Also used : JTextArea(javax.swing.JTextArea) PdfWriter(com.itextpdf.text.pdf.PdfWriter) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) Document(com.itextpdf.text.Document) SVGGraphics2D(org.freehep.graphicsio.svg.SVGGraphics2D) Graphics2D(java.awt.Graphics2D) PSGraphics2D(org.freehep.graphicsio.ps.PSGraphics2D) FileOutputStream(java.io.FileOutputStream) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with DefaultFontMapper

use of com.itextpdf.awt.DefaultFontMapper in project propane by ruby-processing.

the class PGraphicsPDF method getMapper.

protected static DefaultFontMapper getMapper() {
    if (mapper == null) {
        mapper = new DefaultFontMapper();
        switch(PApplet.platform) {
            case PConstants.MACOS:
                try {
                    String homeLibraryFonts = System.getProperty("user.home") + "/Library/Fonts";
                    mapper.insertDirectory(homeLibraryFonts);
                } catch (Exception e) {
                // might be a security issue with getProperty() and user.home
                // if this sketch is running from the web
                }
                // add the system font paths
                mapper.insertDirectory("/System/Library/Fonts");
                mapper.insertDirectory("/Library/Fonts");
                break;
            case PConstants.WINDOWS:
                // how to get the windows fonts directory?
                // could be c:\winnt\fonts or c:\windows\fonts or not even c:
                // maybe do a Runtime.exec() on echo %WINDIR% ?
                // Runtime.exec solution might be a mess on systems where the
                // the backslash/colon characters not really used (i.e. JP)
                // find the windows fonts folder
                File[] roots = File.listRoots();
                for (File root : roots) {
                    if (root.toString().startsWith("A:")) {
                        // If not, will need to use the other fileExists() code below.
                        continue;
                    }
                    File folder = new File(root, "WINDOWS/Fonts");
                    if (folder.exists()) {
                        mapper.insertDirectory(folder.getAbsolutePath());
                        break;
                    }
                    folder = new File(root, "WINNT/Fonts");
                    if (folder.exists()) {
                        mapper.insertDirectory(folder.getAbsolutePath());
                        break;
                    }
                }
                break;
            case PConstants.LINUX:
                checkDir("/usr/share/fonts/", mapper);
                checkDir("/usr/local/share/fonts/", mapper);
                checkDir(System.getProperty("user.home") + "/.fonts", mapper);
                break;
            default:
                break;
        }
    }
    return mapper;
}
Also used : DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) DocumentException(com.itextpdf.text.DocumentException)

Example 3 with DefaultFontMapper

use of com.itextpdf.awt.DefaultFontMapper in project mzmine2 by mzmine.

the class ChartExportUtil method writeChartToPDF.

/**
 * This method saves a chart as a PDF with given dimensions
 *
 * @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();
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) Rectangle(com.itextpdf.text.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) SVGGraphics2DIOException(org.apache.batik.svggen.SVGGraphics2DIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EMFGraphics2D(org.freehep.graphicsio.emf.EMFGraphics2D) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D)

Example 4 with DefaultFontMapper

use of com.itextpdf.awt.DefaultFontMapper in project mzmine2 by mzmine.

the class SwingExportUtil method writeToPDF.

/**
 * Writes swing to pdf
 *
 * @param panel
 * @param fileName
 * @throws DocumentException
 * @throws Exception
 */
public static void writeToPDF(JComponent panel, File fileName) throws IOException, DocumentException {
    // print the panel to pdf
    int width = panel.getWidth();
    int height = panel.getHeight();
    logger.info(() -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}", width, height, fileName.getAbsolutePath()));
    Document document = new Document(new Rectangle(width, height));
    PdfWriter writer = null;
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper());
        panel.print(g2);
        g2.dispose();
        contentByte.addTemplate(template, 0, 0);
        document.close();
        writer.close();
    } finally {
        if (document.isOpen()) {
            document.close();
        }
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) Rectangle(com.itextpdf.text.Rectangle) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) EMFGraphics2D(org.freehep.graphicsio.emf.EMFGraphics2D) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D)

Aggregations

DefaultFontMapper (com.itextpdf.awt.DefaultFontMapper)4 Document (com.itextpdf.text.Document)3 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)3 PdfWriter (com.itextpdf.text.pdf.PdfWriter)3 Graphics2D (java.awt.Graphics2D)3 FileOutputStream (java.io.FileOutputStream)3 Rectangle (com.itextpdf.text.Rectangle)2 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)2 FileNotFoundException (java.io.FileNotFoundException)2 SVGGraphics2D (org.apache.batik.svggen.SVGGraphics2D)2 EMFGraphics2D (org.freehep.graphicsio.emf.EMFGraphics2D)2 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)1 DocumentException (com.itextpdf.text.DocumentException)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 JTextArea (javax.swing.JTextArea)1