Example usage for com.lowagie.text.pdf DefaultFontMapper insertDirectory

List of usage examples for com.lowagie.text.pdf DefaultFontMapper insertDirectory

Introduction

In this page you can find the example usage for com.lowagie.text.pdf DefaultFontMapper insertDirectory.

Prototype

public int insertDirectory(String dir) 

Source Link

Document

Inserts all the fonts recognized by iText in the directory into the map.

Usage

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

License:Apache License

public void printImagePdf(String filename, DesignerCanvas canvas, Dimension graphsize) {
    try {/* ww  w. ja v  a  2 s  .  c  om*/
        DefaultFontMapper mapper = new DefaultFontMapper();
        FontFactory.registerDirectories();
        mapper.insertDirectory("c:\\winnt\\fonts");
        // mapper.insertDirectory("c:\\windows\\fonts");
        // we create a template and a Graphics2D object that corresponds
        // with it
        int margin = 72; // 1 inch
        float scale = 0.5f;
        boolean multiple_page = true;
        Rectangle page_size;
        if (multiple_page) {
            page_size = PageSize.LETTER.rotate();
        } else {
            page_size = new Rectangle((int) (graphsize.getWidth() * scale) + margin,
                    (int) (graphsize.getHeight() * scale) + margin);
        }
        Document document = new Document(page_size);
        DocWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        document.setPageSize(page_size);
        int image_w = (int) page_size.getWidth() - margin;
        int image_h = (int) page_size.getHeight() - margin;
        boolean edsave = canvas.editable;
        canvas.editable = false;
        Color bgsave = canvas.getBackground();
        canvas.setBackground(Color.white);
        if (multiple_page) {
            int horizontal_pages = (int) (graphsize.width * scale) / image_w + 1;
            int vertical_pages = (int) (graphsize.height * scale) / image_h + 1;
            for (int i = 0; i < horizontal_pages; i++) {
                for (int j = 0; j < vertical_pages; j++) {
                    Image img;
                    PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
                    PdfTemplate tp = cb.createTemplate(image_w, image_h);
                    Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper);
                    tp.setWidth(image_w);
                    tp.setHeight(image_h);
                    g2.scale(scale, scale);
                    g2.translate(-i * image_w / scale, -j * image_h / scale);
                    canvas.paintComponent(g2);
                    g2.dispose();
                    img = new ImgTemplate(tp);
                    document.add(img);
                }
            }
        } else {
            Image img;
            PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
            PdfTemplate tp = cb.createTemplate(image_w, image_h);
            Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper);
            tp.setWidth(image_w);
            tp.setHeight(image_h);
            g2.scale(scale, scale);
            canvas.paintComponent(g2);
            g2.dispose();
            img = new ImgTemplate(tp);
            document.add(img);
        }
        canvas.setBackground(bgsave);
        canvas.editable = edsave;
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

License:Apache License

private void printGraphPdf(DocWriter writer, CanvasCommon canvas, Graph process, Rectangle page_size,
        String type, String filename, Chapter chapter, int chapter_number) throws Exception {
    Dimension graphsize = process.getGraphSize();
    // we create a fontMapper and read all the fonts in the font directory
    DefaultFontMapper mapper = new DefaultFontMapper();
    FontFactory.registerDirectories();/*from w w  w. ja va  2 s  .c o m*/
    mapper.insertDirectory("c:\\winnt\\fonts");
    // mapper.insertDirectory("c:\\windows\\fonts");
    // we create a template and a Graphics2D object that corresponds with it
    int w, h;
    float scale;
    if ((float) graphsize.width < page_size.getWidth() * 0.8
            && (float) graphsize.height < page_size.getHeight() * 0.8 || type.equals(HTML)) {
        w = graphsize.width + 36;
        h = graphsize.height + 36;
        scale = -1f;
    } else {
        scale = page_size.getWidth() * 0.8f / (float) graphsize.width;
        if (scale > page_size.getHeight() * 0.8f / (float) graphsize.height)
            scale = page_size.getHeight() * 0.8f / (float) graphsize.height;
        w = (int) (graphsize.width * scale) + 36;
        h = (int) (graphsize.height * scale) + 36;
    }
    Image img;
    int zoomSave = process.zoom;
    process.zoom = 100;
    Color bgsave = canvas.getBackground();
    boolean edsave = canvas.editable;
    canvas.editable = false;
    canvas.setBackground(Color.white);

    if (type.equals(PDF)) {
        PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2 = tp.createGraphics(w, h, mapper);
        if (scale > 0)
            g2.scale(scale, scale);
        tp.setWidth(w);
        tp.setHeight(h);
        canvas.paintComponent(g2);
        g2.dispose();
        // cb.addTemplate(tp, 50, 400);
        img = new ImgTemplate(tp);
    } else {
        String imgfilename = filename + "." + process.getName() + "_ch" + chapter_number + ".jpg";
        printImage(imgfilename, -1f, canvas, graphsize);
        img = Image.getInstance(imgfilename);
        if (scale > 0)
            img.scalePercent(scale * 100);
    }
    process.zoom = zoomSave;
    canvas.setBackground(bgsave);
    canvas.editable = edsave;
    if (img != null)
        chapter.add(img);
}

From source file:org.adempiere.pdf.Document.java

License:Open Source License

private static void writePDF(Pageable pageable, OutputStream output) {
    try {//from w w  w  .  j av a2  s.  c om
        final PageFormat pf = pageable.getPageFormat(0);

        final com.lowagie.text.Document document = new com.lowagie.text.Document(
                new Rectangle((int) pf.getWidth(), (int) pf.getHeight()));
        final PdfWriter writer = PdfWriter.getInstance(document, output);
        writer.setPdfVersion(PdfWriter.VERSION_1_2);
        document.open();
        final DefaultFontMapper mapper = new DefaultFontMapper();

        //Elaine 2009/02/17 - load additional font from directory set in PDF_FONT_DIR of System Configurator 
        String pdfFontDir = MSysConfig.getValue(PDF_FONT_DIR, "");
        if (pdfFontDir != null && pdfFontDir.trim().length() > 0) {
            pdfFontDir = pdfFontDir.trim();
            File dir = new File(pdfFontDir);
            if (dir.exists() && dir.isDirectory())
                mapper.insertDirectory(pdfFontDir);
        }
        //

        final float w = (float) pf.getWidth();
        final float h = (float) pf.getHeight();
        final PdfContentByte cb = writer.getDirectContent();
        for (int page = 0; page < pageable.getNumberOfPages(); page++) {
            if (page != 0) {
                document.newPage();
            }

            final PdfTemplate tp = cb.createTemplate(w, h);
            final Graphics2D g2 = tp.createGraphics(w, h, mapper);
            tp.setWidth(w);
            tp.setHeight(h);
            pageable.getPrintable(page).print(g2, pf, page);
            g2.dispose();
            cb.addTemplate(tp, 0, 0);
        }
        document.close();

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

From source file:org.adempiere.util.Document.java

private static void writePDF(Pageable pageable, OutputStream output) {
    try {//from   w  w w. j  a v  a 2s.  c o m
        final PageFormat pf = pageable.getPageFormat(0);

        Rectangle pageSize = new Rectangle((int) pf.getWidth(), (int) pf.getHeight());
        final com.lowagie.text.Document document = new com.lowagie.text.Document(pageSize);
        final PdfWriter writer = PdfWriter.getInstance(document, output);
        writer.setPdfVersion(PdfWriter.VERSION_1_2);
        document.open();
        final DefaultFontMapper mapper = new DefaultFontMapper();

        String pdfFontDir = /* MSysConfig.getValue( */PDF_FONT_DIR/* , "") */;
        if (pdfFontDir != null && pdfFontDir.trim().length() > 0) {
            pdfFontDir = pdfFontDir.trim();
            File dir = new File(pdfFontDir);
            if (dir.exists() && dir.isDirectory())
                mapper.insertDirectory(pdfFontDir);
        }
        final float w = (float) pf.getWidth();
        final float h = (float) pf.getHeight();
        final PdfContentByte cb = writer.getDirectContent();
        for (int page = 0; page < pageable.getNumberOfPages(); page++) {
            if (page != 0) {
                document.newPage();
            }

            final PdfTemplate tp = cb.createTemplate(w, h);
            final Graphics2D g2 = tp.createGraphics(w, h, mapper);
            tp.setWidth(w);
            tp.setHeight(h);
            pageable.getPrintable(page).print(g2, pf, page);
            g2.dispose();
            cb.addTemplate(tp, 0, 0);
        }
        document.close();

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

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;/*from   ww w  .j  a  v  a  2s  .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:org.schreibubi.JCombinations.ui.GridChartPanel.java

License:Open Source License

/**
 * Create PDF//from  www .j av a  2  s . co m
 * 
 * @param name
 *            Filename
 * @param selection
 *            Selected Nodes
 * @param dm
 *            DataModel
 * @param pl
 *            ProgressListener
 */
@SuppressWarnings("unchecked")
public static void generatePDF(File name, DataModel dm, ArrayList<TreePath> selection, ProgressListener pl) {
    com.lowagie.text.Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
    try {
        ArrayList<ExtendedJFreeChart> charts = dm.getCharts(selection);
        if (charts.size() > 0) {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(name));
            writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
            document.addAuthor("Jrg Werner");
            document.addSubject("Created by JCombinations");
            document.addKeywords("JCombinations");
            document.addCreator("JCombinations using iText");

            // we define a header and a footer
            HeaderFooter header = new HeaderFooter(new Phrase("JCombinations by Jrg Werner"), false);
            HeaderFooter footer = new HeaderFooter(new Phrase("Page "), new Phrase("."));
            footer.setAlignment(Element.ALIGN_CENTER);
            document.setHeader(header);
            document.setFooter(footer);

            document.open();
            DefaultFontMapper mapper = new DefaultFontMapper();
            FontFactory.registerDirectories();
            mapper.insertDirectory("c:\\WINNT\\fonts");

            PdfContentByte cb = writer.getDirectContent();

            pl.progressStarted(new ProgressEvent(GridChartPanel.class, 0, charts.size()));
            for (int i = 0; i < charts.size(); i++) {
                ExtendedJFreeChart chart = charts.get(i);
                PdfTemplate tp = cb.createTemplate(document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN),
                        document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN));
                Graphics2D g2d = tp.createGraphics(document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN),
                        document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN), mapper);
                Rectangle2D r2d = new Rectangle2D.Double(0, 0,
                        document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN),
                        document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN));
                chart.draw(g2d, r2d);
                g2d.dispose();
                cb.addTemplate(tp, document.left(EXTRA_MARGIN), document.bottom(EXTRA_MARGIN));
                PdfDestination destination = new PdfDestination(PdfDestination.FIT);
                TreePath treePath = chart.getTreePath();
                PdfOutline po = cb.getRootOutline();
                for (int j = 0; j < treePath.getPathCount(); j++) {
                    ArrayList<PdfOutline> lpo = po.getKids();
                    PdfOutline cpo = null;
                    for (PdfOutline outline : lpo)
                        if (outline.getTitle().compareTo(treePath.getPathComponent(j).toString()) == 0)
                            cpo = outline;
                    if (cpo == null)
                        cpo = new PdfOutline(po, destination, treePath.getPathComponent(j).toString());
                    po = cpo;
                }
                document.newPage();
                pl.progressIncremented(new ProgressEvent(GridChartPanel.class, i));
            }
            document.close();
            pl.progressEnded(new ProgressEvent(GridChartPanel.class));

        }
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
}

From source file:processing.pdf.PGraphicsPDF.java

License:Open Source License

static protected void checkDir(String path, DefaultFontMapper mapper) {
    File folder = new File(path);
    if (folder.exists()) {
        mapper.insertDirectory(path);
        traverseDir(folder, mapper);//from ww  w  . j av a2 s . com
    }
}

From source file:processing.pdf.PGraphicsPDF.java

License:Open Source License

/**
 * Recursive walk to get all subdirectories for font fun.
 * Patch submitted by Matthias Breuer.//from   w w w  .ja  v a2s  .  c  o  m
 * (<a href="http://dev.processing.org/bugs/show_bug.cgi?id=1566">Bug 1566</a>)
 */
static protected void traverseDir(File folder, DefaultFontMapper mapper) {
    File[] files = folder.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            mapper.insertDirectory(files[i].getPath());
            traverseDir(new File(files[i].getPath()), mapper);
        }
    }
}