Example usage for com.lowagie.text Font setFamily

List of usage examples for com.lowagie.text Font setFamily

Introduction

In this page you can find the example usage for com.lowagie.text Font setFamily.

Prototype

public void setFamily(String family) 

Source Link

Document

Sets the family using a String ("Courier", "Helvetica", "Times New Roman", "Symbol" or "ZapfDingbats").

Usage

From source file:com.conecta.sat.utils.BuildImportTokensPDF.java

@Override
protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter writer,
        HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
    //        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    System.out.println("Into BuildImportTokensPDF");
    hsr1.setContentType("application/pdf");
    DateFormat name = new SimpleDateFormat("ddMMyyyyhhmmss");
    hsr1.setHeader("Content-disposition",
            "attachment; filename=ImportTokens" + name.format(new Date()) + ".pdf");
    ImportTokensPDF list = (ImportTokensPDF) map.get("list");
    String nombreUsuario = (String) map.get("nombreUsuario");

    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(100);// w  ww  .  j  a  va  2 s  . com
    table.setSpacingBefore(10);

    Font font = FontFactory.getFont(FontFactory.HELVETICA);
    try {
        font.setFamily(fontName);
    } catch (Exception e) {
        font.setFamily("Verdana");
    }

    try {
        ServletContext servletContext = hsr.getSession().getServletContext();
        String relativeWebPath = logoPath;
        String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
        Image logo = Image.getInstance(absoluteDiskPath);
        //            Image logo = Image.getInstance("logo.png");
        System.out.println("La imagen se cargo correctamente");
        logo.scaleToFit(widthLogo, heightLogo);
        document.add(logo);
    } catch (Exception e) {
        System.err.println("ERROR" + Excepciones.getStackTrace(e));
        document.add(new Paragraph("Sin imagen " + logoPath));
    }
    font.setSize(fontSize + 3);
    Paragraph titulo = new Paragraph("Reporte de Tokens", font);
    titulo.setAlignment(Element.ALIGN_RIGHT);
    document.add(titulo);

    Chunk CONNECT = new Chunk(new LineSeparator(0.5f, 100, java.awt.Color.BLACK, Element.ALIGN_CENTER, 3.5f));
    document.add(CONNECT);

    DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    font.setSize(fontSize + 2);
    Paragraph fecha = new Paragraph(df.format(new Date()), font);
    fecha.setAlignment(Element.ALIGN_RIGHT);
    document.add(fecha);

    Paragraph pNombre = new Paragraph(nombreUsuario, font);
    pNombre.setAlignment(Element.ALIGN_RIGHT);
    document.add(pNombre);

    font.setSize(fontSize);
    font.setColor(java.awt.Color.white);

    // define table header cell
    PdfPCell cell = new PdfPCell();
    java.awt.Color color = java.awt.Color.LIGHT_GRAY;
    cell.setBackgroundColor(color);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);

    cell.setPadding(5);

    // write table header
    cell.setPhrase(new Phrase("Nmero de empleado que importo en token", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Fecha y Hora", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Tokens importados", font));
    table.addCell(cell);

    //        cell.setPhrase(new Phrase("Centro Financiero", font));
    //        table.addCell(cell);
    //         
    //        cell.setPhrase(new Phrase("Tipo", font));
    //        table.addCell(cell);
    //        
    //        cell.setPhrase(new Phrase("Cliente nico", font));
    //        table.addCell(cell);
    //        
    //        cell.setPhrase(new Phrase("Fecha de ultima modificacin", font));
    //        table.addCell(cell);

    Font font2 = FontFactory.getFont(FontFactory.HELVETICA);
    try {
        font2.setFamily(fontName);
    } catch (Exception e) {
        font2.setFamily("Verdana");
    }
    font2.setSize(fontSize);
    font2.setColor(java.awt.Color.black);
    // write table row data
    //        for (ImportTokensPDF pdf : list) {
    //        ImportTokensPDF pdf;
    cell = new PdfPCell();
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.addElement(new Phrase(list.getUsuario(), font2));
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.addElement(new Phrase(df.format(list.getFecha()), font2));
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.addElement(new Phrase(list.getTokens(), font2));
    table.addCell(cell);

    //            cell = new PdfPCell();
    //            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
    //            cell.addElement( new Phrase(pdf.getCentro(),font2) );
    //            table.addCell(cell);
    //            
    //            cell = new PdfPCell();
    //            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
    //            cell.addElement( new Phrase(pdf.getTipo(),font2) );
    //            table.addCell(cell);
    //            
    //            cell = new PdfPCell();
    //            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
    //            cell.addElement( new Phrase(pdf.getCliente(),font2) );
    //            table.addCell(cell);
    //            
    //            cell = new PdfPCell();
    //            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
    //            cell.addElement( new Phrase(pdf.getLastUpdate(),font2) );
    //            table.addCell(cell);
    //        }

    document.add(table);
}

From source file:com.conecta.sat.utils.BuildPDF.java

@Override
protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter writer,
        HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
    //        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    hsr1.setContentType("application/pdf");
    DateFormat name = new SimpleDateFormat("ddMMyyyyhhmmss");
    hsr1.setHeader("Content-disposition", "attachment; filename=Reporte" + name.format(new Date()) + ".pdf");
    List<PdfDTO> list = (List<PdfDTO>) map.get("list");
    String nombreUsuario = (String) map.get("nombreUsuario");

    PdfPTable table = new PdfPTable(9);
    table.setWidthPercentage(100);//from  ww w  . j  a  v a  2  s.co  m
    table.setSpacingBefore(10);

    Font font = FontFactory.getFont(FontFactory.HELVETICA);
    try {
        font.setFamily(fontName);
    } catch (Exception e) {
        font.setFamily("Verdana");
    }

    try {
        ServletContext servletContext = hsr.getSession().getServletContext();
        String relativeWebPath = logoPath;
        String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
        Image logo = Image.getInstance(absoluteDiskPath);
        //            Image logo = Image.getInstance("logo.png");
        System.out.println("La imagen se cargo correctamente");
        logo.scaleToFit(widthLogo, heightLogo);
        document.add(logo);
    } catch (Exception e) {
        System.err.println("ERROR" + Excepciones.getStackTrace(e));
        document.add(new Paragraph("Sin imagen " + logoPath));
    }
    font.setSize(fontSize + 3);
    Paragraph titulo = new Paragraph("Reporte de Tokens", font);
    titulo.setAlignment(Element.ALIGN_RIGHT);
    document.add(titulo);

    Chunk CONNECT = new Chunk(new LineSeparator(0.5f, 100, java.awt.Color.BLACK, Element.ALIGN_CENTER, 3.5f));
    document.add(CONNECT);

    DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    font.setSize(fontSize + 2);
    Paragraph fecha = new Paragraph(df.format(new Date()), font);
    fecha.setAlignment(Element.ALIGN_RIGHT);
    document.add(fecha);

    Paragraph pNombre = new Paragraph(nombreUsuario, font);
    pNombre.setAlignment(Element.ALIGN_RIGHT);
    document.add(pNombre);

    font.setSize(fontSize);
    font.setColor(java.awt.Color.white);

    // define table header cell
    PdfPCell cell = new PdfPCell();
    java.awt.Color color = java.awt.Color.LIGHT_GRAY;
    cell.setBackgroundColor(color);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);

    cell.setPadding(5);

    // write table header
    cell.setPhrase(new Phrase("Serial Token", font));
    table.addCell(cell);

    //        cell.setPhrase(new Phrase("Estatus Entrega", font));
    cell.setPhrase(new Phrase("Estatus Entrega al Centro Financiero", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Estatus Entrega al Cliente", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Estatus Activacin", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Centro Financiero", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Tipo", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Cliente nico", font));
    table.addCell(cell);

    cell.setPhrase(new Phrase("Folio Pivotal", font));
    table.addCell(cell);

    //        cell.setPhrase(new Phrase("ID nico", font));
    //        table.addCell(cell);

    cell.setPhrase(new Phrase("Fecha de ltima modificacin", font));
    table.addCell(cell);

    Font font2 = FontFactory.getFont(FontFactory.HELVETICA);
    try {
        font2.setFamily(fontName);
    } catch (Exception e) {
        font2.setFamily("Verdana");
    }
    font2.setSize(fontSize);
    font2.setColor(java.awt.Color.black);
    // write table row data
    for (PdfDTO pdf : list) {
        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getSerial(), font2));
        table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getEntrega(), font2));
        table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getEntrega(), font2));
        table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getActivacion(), font2));
        table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getCentro(), font2));
        table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getTipo(), font2));
        table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getCliente(), font2));
        table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getFolioPivotal(), font2));
        table.addCell(cell);

        //            cell = new PdfPCell();
        //            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
        //            cell.addElement( new Phrase(pdf.getIdUnico(),font2) );
        //            table.addCell(cell);

        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(new Phrase(pdf.getLastUpdate(), font2));
        table.addCell(cell);
    }

    document.add(table);
}

From source file:com.qubit.terra.docs.util.FontProvider.java

License:Open Source License

@Override
public Font getFont(String familyName, String encoding, float size, int style, Color color) {
    try {//w w w . j  a  v  a 2 s  .co  m
        BaseFont baseFont = fontManager.findBaseFont(familyName, style);

        Font font = new Font(baseFont, size, style, color);
        if (!StringUtils.isEmpty(familyName)) {
            font.setFamily(familyName);
        }

        return font;
    } catch (Throwable e) {
        e.printStackTrace();
        return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
    }
}

From source file:org.apache.maven.doxia.module.itext.ITextFont.java

License:Apache License

/**
 * Convenience method to get a defined font depending the wanted style and size.
 *
 * @param style the font style.//w  ww . ja  v a2  s . com
 * @param size the font size.
 * @param color the font color.
 * @return a font the font.
 */
public static Font getFont(int style, float size, Color color) {
    Font font = new Font();
    font.setFamily(DEFAULT_FONT_NAME);
    font.setStyle(style);
    font.setSize(size);
    font.setColor(color);
    return font;
}

From source file:org.areasy.common.doclet.document.tags.HtmlTag.java

License:Open Source License

/**
 * Returns the appropriate PDF font for this
 * HTML tag. The font is created based on the
 * type and attributes of the tag.//www.j  a  v  a2s  .com
 *
 * @return The PDF document font.
 */
public Font getFont() {
    Font font;

    int faceId = TEXT_FONT;
    boolean parentIsFixedFont = false;

    if (isCode() || isPre())
        faceId = CODE_FONT;

    if (parent != null) {
        if (getFontSize() == DEFAULT_FONT_SIZE)
            setFontSize(parent.getFontSize());
        parentIsFixedFont = parent.isCode() || parent.isPre();
    }

    // Pre-formatted text parts tend to appear bigger, so make them 1 point smaller
    if ((isCode() || isPre()) && !parentIsFixedFont)
        setFontSize(getFontSize() - 1);

    if (type == TAG_I)
        setItalic(true);
    if (type == TAG_B)
        setBold(true);
    if (type == TAG_U)
        setUnderline(true);

    if (type == TAG_H1)
        setFontSize(26);
    if (type == TAG_H2)
        setFontSize(22);
    if (type == TAG_H3)
        setFontSize(19);
    if (type == TAG_H4)
        setFontSize(16);
    if (type == TAG_H5)
        setFontSize(13);
    if (type == TAG_H6)
        setFontSize(10);

    int style = 0;

    if (isBold() && isItalic())
        style = BOLD + ITALIC;
    else if (isBold())
        style = BOLD;
    else if (isItalic())
        style = ITALIC;

    if (isLink())
        style = style + LINK;
    if (isUnderline())
        style = style + UNDERLINE;
    if (isStrikethrough())
        style = style + STRIKETHROUGH;

    if (type == TAG_BODY && Locale.getDefault().getLanguage().equals(Locale.JAPANESE.getLanguage()))
        font = FontFactory.getFont("HeiseiMin-W3", "UniJIS-UCS2-HW-H", getFontSize(), style, getFontColor());
    else
        font = Fonts.getFont(faceId, style, getFontSize());

    if (getFontColor() == null && parent != null && parent.getFontColor() != null)
        setFontColor(parent.getFontColor());
    if (getFontFace() == null && parent != null && parent.getFontFace() != null)
        setFontFace(parent.getFontFace());

    if (getFontColor() != null)
        font.setColor(getFontColor());
    if (StringUtility.isNotEmpty(getFontFace()))
        font.setFamily(getFontFace());

    return font;
}

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  w w  .  j av  a  2s  . c  om
    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;
}

From source file:ro.nextreports.engine.exporter.RtfExporter.java

License:Apache License

private void updateFont(Font fnt, Map<String, Object> style) {
    if (style != null) {
        if (style.containsKey(StyleFormatConstants.FONT_FAMILY_KEY)) {
            String val = (String) style.get(StyleFormatConstants.FONT_FAMILY_KEY);
            fnt.setFamily(val);
        }//from  ww  w.  j a  va 2s  .  co m
        if (style.containsKey(StyleFormatConstants.FONT_SIZE)) {
            Float val = (Float) style.get(StyleFormatConstants.FONT_SIZE);
            fnt.setSize(val);
        }
        if (style.containsKey(StyleFormatConstants.FONT_COLOR)) {
            Color val = (Color) style.get(StyleFormatConstants.FONT_COLOR);
            fnt.setColor(val);
        }
        if (style.containsKey(StyleFormatConstants.FONT_STYLE_KEY)) {
            if (StyleFormatConstants.FONT_STYLE_NORMAL.equals(style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
                fnt.setStyle(com.lowagie.text.Font.NORMAL);
            }
            if (StyleFormatConstants.FONT_STYLE_BOLD.equals(style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
                fnt.setStyle(com.lowagie.text.Font.BOLD);
            }
            if (StyleFormatConstants.FONT_STYLE_ITALIC.equals(style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
                fnt.setStyle(com.lowagie.text.Font.ITALIC);
            }
            if (StyleFormatConstants.FONT_STYLE_BOLDITALIC
                    .equals(style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
                fnt.setStyle(com.lowagie.text.Font.BOLDITALIC);
            }
        }
    }
}

From source file:test.poi.ConvertDocxResumeToPDF.java

License:LGPL

private static void create() {
    long startTime = System.currentTimeMillis();

    try {//from w  w  w. j a  v a 2 s . co  m
        // 1) Load docx with POI XWPFDocument
        XWPFDocument document = new XWPFDocument(
                ConvertDocxResumeToPDF.class.getClassLoader().getResourceAsStream("DocxResume.docx"));

        // 2) Convert POI XWPFDocument 2 PDF with iText
        File outFile = new File("d:/DocxResume.pdf");
        outFile.getParentFile().mkdirs();

        OutputStream out = new FileOutputStream(outFile);
        PdfOptions options = PdfOptions.create();
        //
        options.fontProvider(new IFontProvider() {

            @Override
            public Font getFont(String familyName, String encoding, float size, int style, Color color) {
                try {
                    BaseFont bfChinese = BaseFont.createFont("c:/Windows/Fonts/arialuni.ttf",
                            BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null)
                        fontChinese.setFamily(familyName);
                    return fontChinese;
                } catch (Throwable e) {
                    e.printStackTrace();
                    return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
                }
            }
        });

        PdfConverter.getInstance().convert(document, out, options);
    } catch (Throwable e) {
        e.printStackTrace();
    }

    System.out.println("Generate DocxResume.pdf with " + (System.currentTimeMillis() - startTime) + " ms.");
}