Example usage for com.lowagie.text FontFactory getRegisteredFonts

List of usage examples for com.lowagie.text FontFactory getRegisteredFonts

Introduction

In this page you can find the example usage for com.lowagie.text FontFactory getRegisteredFonts.

Prototype


public static Set getRegisteredFonts() 

Source Link

Document

Gets a set of registered fontnames.

Usage

From source file:com.gtdfree.test.PDFTestFont.java

License:Open Source License

/**
 * Fonts and encoding.//from  w  w  w  .j a va  2 s  . c  o m
 * @param args no arguments needed
 */
public static void main(String[] args) {

    System.out.println("Encodings");

    String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    System.out.println(Arrays.toString(names));

    /*System.out.println("---");
    try {
    System.getProperties().store(System.out, "");
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    System.out.println("---");*/
    //System.out.println(System.getenv());
    //System.out.println("---");

    //String font= System.getProperty("java.home")+"/lib/fonts/LucidaBrightRegular.ttf";
    //String font= "fonts/DejaVuSans.ttf";

    //byte[] ttf= ApplicationHelper.loadResource(font);

    try {
        // step 1
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream("encodingfont.pdf"));
        // step 3
        document.open();
        // step 4
        String all[] = { "Symbol", "ZapfDingbats" };
        Font hex = new Font(Font.HELVETICA, 5);
        for (int z = 0; z < all.length; ++z) {
            String file = all[z];
            document.add(new Paragraph(
                    "Unicode equivalence for the font \"" + file + "\" with the encoding \"" + file + "\"\n"));
            /*char tb[];
            if (z == 0)
               tb = SYMBOL_TABLE;
            else
               tb = DINGBATS_TABLE;*/
            BaseFont bf;
            if (z == 2) {
                bf = BaseFont.createFont(file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, null, null);
                ;
            } else {
                bf = BaseFont.createFont(file, file, true);
            }
            Font f = new Font(bf, 12);
            PdfPTable table = new PdfPTable(16);
            table.setWidthPercentage(100);
            table.getDefaultCell().setBorderWidth(1);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            for (char k = 0; k < Character.MAX_VALUE; ++k) {
                char c = k;
                if (bf.charExists(c)) {
                    Phrase ph = new Phrase(12, new String(new char[] { c }), f);
                    ph.add(new Phrase(12, "\n" + Integer.toString(c) + "\n" + cst(c), hex));
                    table.addCell(ph);
                } /*
                  else {
                     Phrase ph = new Phrase("\u00a0");
                     ph.add(new Phrase(12, "\n\n" + cst(c), hex));
                     table.addCell(ph);
                  }*/
            }
            document.add(table);
            document.newPage();
        }
        // step 5
        document.close();

        FontFactory.registerDirectories();

        Set<?> s = FontFactory.getRegisteredFonts();

        System.out.println("Fonts: " + s);

        s = FontFactory.getRegisteredFamilies();

        System.out.println("Families: " + s);

        ArrayList<Font> f = new ArrayList<Font>(s.size());

        for (Object name : s) {

            try {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", true, 12, Font.NORMAL, Color.BLACK, true));
            } catch (Exception e) {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", false, 12, Font.NORMAL, Color.BLACK, true));
            }

        }

        Collections.sort(f, new Comparator<Font>() {
            @Override
            public int compare(Font o1, Font o2) {
                return o1.getFamilyname().compareTo(o2.getFamilyname());
            }
        });

        for (Font ff : f) {

            if (ff.getBaseFont() == null) {
                continue;
            }
            System.out.println(ff.getFamilyname() + " " + ff.getBaseFont().isEmbedded());

        }

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

From source file:jdraw.JDrawApplication.java

private void saveAsPDF() {
    JDocumentFrame frame = (JDocumentFrame) jDesktopPane1.getSelectedFrame();
    if (frame == null) {
        return;//ww  w  . j a v a  2s  .  c  o m
    }
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new FileNameExtensionFilter(
            java.util.ResourceBundle.getBundle("main").getString("filter_pdf"), "pdf"));
    chooser.setDialogTitle(java.util.ResourceBundle.getBundle("main").getString("dialog_export_as_pdf"));
    File f = chooser.getCurrentDirectory();
    f = new File(f.getPath(), frame.getDocument().getName() + ".pdf");
    chooser.setSelectedFile(f);
    if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
        return;
    }
    JDocument doc = frame.getDocument();
    f = chooser.getSelectedFile();
    if (f.exists() && JOptionPane.showConfirmDialog(this, f.getName() + java.util.ResourceBundle
            .getBundle("main").getString("msg_is_exist_overwrite")) != JOptionPane.OK_OPTION) {
        return;
    }
    frame.getViewer().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    com.lowagie.text.Document pDoc = new com.lowagie.text.Document();
    try {
        FileOutputStream wt = new FileOutputStream(f);
        BufferedOutputStream bout = new BufferedOutputStream(wt);
        com.lowagie.text.pdf.PdfWriter pwriter = com.lowagie.text.pdf.PdfWriter.getInstance(pDoc, bout);
        pDoc.open();
        FontFactory.registerDirectories();
        Set set = FontFactory.getRegisteredFonts();

        for (int i = 0; i < doc.size(); i++) {
            JPage cPage = doc.get(i);
            PageFormat pFormat = cPage.getPageFormat();
            com.lowagie.text.Rectangle rc = new com.lowagie.text.Rectangle((float) pFormat.getWidth(),
                    (float) pFormat.getHeight());
            float left = (float) (pFormat.getImageableX());
            float right = (float) (pFormat.getWidth() - pFormat.getImageableWidth() - left);
            float top = (float) pFormat.getImageableX();
            float bottom = (float) (pFormat.getHeight() - pFormat.getImageableHeight() - top);
            pDoc.newPage();

            com.lowagie.text.pdf.PdfContentByte cb = pdfContentByte = pwriter.getDirectContent();
            cb.saveState();

            Graphics2D g2 = (com.lowagie.text.pdf.PdfGraphics2D) cb.createGraphics((float) pFormat.getWidth(),
                    (float) pFormat.getHeight());

            boolean vg = cPage.getGuidLayer().isVisible();
            cPage.getGuidLayer().setVisible(false);
            cPage.paint(new Rectangle.Double(0, 0, pFormat.getWidth(), pFormat.getHeight()), g2);
            cPage.getGuidLayer().setVisible(vg);
            g2.dispose();
            cb.restoreState();
        }
        pDoc.close();
    } catch (Exception e) {
        JOptionPane.showConfirmDialog(this, e.getMessage(), "", JOptionPane.OK_OPTION,
                JOptionPane.ERROR_MESSAGE);
    }
    frame.getViewer().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

}

From source file:org.lucee.extension.pdf.tag.PDF.java

License:Open Source License

private Font toFont(Struct sct) throws PageException {
    Cast caster = engine.getCastUtil();//from w ww  .  java 2s. co m
    Font f = getDefaultFont();
    // size
    float size = caster.toFloatValue(sct.get("size", null), 0);
    if (size > 0)
        f.setSize(size);

    // family
    Set fonts = FontFactory.getRegisteredFonts();
    String family = caster.toString(sct.get("family", null), null);
    if (!Util.isEmpty(family)) {
        String lc = family.toLowerCase();
        if (!fonts.contains(lc)) {
            StringBuilder sb = new StringBuilder();
            Iterator it = fonts.iterator();
            while (it.hasNext()) {
                if (sb.length() > 0)
                    sb.append(", ");
                sb.append(it.next());
            }
            throw engine.getExceptionUtil().createApplicationException(
                    "font family [" + family + "] is not available, available font families are [" + sb + "]");
        }
        f.setFamily(lc);
    }

    int style = 0;
    // bold
    boolean bold = caster.toBooleanValue(sct.get("bold", null), false);
    if (bold)
        style |= Font.BOLD;
    // italic
    boolean italic = caster.toBooleanValue(sct.get("italic", null), false);
    if (italic)
        style |= Font.ITALIC;
    // underline
    boolean underline = caster.toBooleanValue(sct.get("underline", null), false);
    if (underline)
        style |= Font.UNDERLINE;
    // strike
    boolean strike = caster.toBooleanValue(sct.get("strike", null), false);
    if (strike)
        style |= Font.STRIKETHRU;
    if (style != 0)
        f.setStyle(style);

    return f;
}