Example usage for com.lowagie.text Font getSize

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

Introduction

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

Prototype

public float getSize() 

Source Link

Document

Gets the size of this font.

Usage

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

License:Apache License

private void printElementHtml(Element element, Object parent, int depth, Font font, int parentLevel) {
    String tag = element.getName();
    Object av;//from   www. jav  a  2 s. c o  m
    if (element instanceof HTMLDocument.RunElement) {
        HTMLDocument.RunElement re = (HTMLDocument.RunElement) element;
        int start = re.getStartOffset();
        int end = re.getEndOffset();
        try {
            String content = re.getDocument().getText(start, end - start);
            printAttributesHtml(re);
            av = re.getAttribute(CSS.Attribute.FONT_SIZE);
            String fontsize = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.FONT_FAMILY);
            String fontfamily = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.COLOR);
            String fontcolor = av == null ? null : av.toString();
            if (fontcolor != null || fontsize != null || fontfamily != null) {
                if (fontfamily == null)
                    fontfamily = font.getFamilyname();
                float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 9);
                int style = font.getStyle();
                Color color;
                if (fontcolor != null) {
                    color = Color.decode(fontcolor);
                } else
                    color = font.getColor();
                font = FontFactory.getFont(fontfamily, size, style, color);
            }
            if (parent instanceof Paragraph) {
                ((Paragraph) parent).add(new Chunk(content, font));
            } else {
                System.err.println("chunk with parent "
                        + (parent == null ? "null" : parent.getClass().getName()) + ": " + content);
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    } else if (element instanceof HTMLDocument.BlockElement) {
        HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element;
        printAttributesHtml(be);
        av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN);
        String align = av == null ? null : av.toString();
        if (tag.equalsIgnoreCase("html")) {
            printElementChildrenHtml(element, parent, depth + 1, font, parentLevel);
        } else if (tag.equalsIgnoreCase("head")) {
            // do nothing
        } else if (tag.equalsIgnoreCase("body")) {
            printElementChildrenHtml(element, parent, depth + 1, font, parentLevel);
        } else if (tag.equalsIgnoreCase("p")) {
            if (parent instanceof Section) {
                Paragraph paragraph = new Paragraph();
                if (align != null) {
                    paragraph.setAlignment(align);
                }
                printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
                ((Section) parent).add(paragraph);
            } else {
                System.err.println("p with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("h1") || tag.equalsIgnoreCase("h2") || tag.equalsIgnoreCase("h3")) {
            if (parent instanceof Section) {
                Paragraph title = new Paragraph();
                printElementChildrenHtml(element, title, depth + 1, subSectionFont, parentLevel);
                ((Section) parent).addSection(title, parentLevel == 0 ? 0 : (parentLevel + 1));
            } else {
                System.err
                        .println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("ul")) {
            if (parent instanceof Section) {
                com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f);
                printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel);
                ((Section) parent).add(list);
            } else {
                System.err
                        .println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("ol")) {
            if (parent instanceof Section) {
                com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f);
                printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel);
                ((Section) parent).add(list);
            } else {
                System.err
                        .println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("li")) {
            ListItem li = new ListItem();
            li.setSpacingAfter(0.0f);
            printElementChildrenHtml(element, li, depth + 1, normalFont, parentLevel);
            ((com.lowagie.text.List) parent).add(li);
        } else if (tag.equalsIgnoreCase("p-implied")) {
            if (parent instanceof ListItem) {
                Paragraph paragraph = new Paragraph();
                printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
                ((ListItem) parent).add(paragraph);
            } else if (parent instanceof Cell) {
                Paragraph paragraph = new Paragraph();
                printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
                ((Cell) parent).add(paragraph);
            }
        } else if (tag.equalsIgnoreCase("table")) {
            try {
                Table table = new Table(3);
                table.setBorderWidth(1);
                table.setBorderColor(new Color(0, 128, 128));
                table.setPadding(1.0f);
                table.setSpacing(0.5f);
                Cell c = new Cell("header");
                c.setHeader(true);
                c.setColspan(3);
                table.addCell(c);
                table.endHeaders();
                printElementChildrenHtml(element, table, depth + 1, normalFont, parentLevel); // TODO
                ((Section) parent).add(table);
            } catch (BadElementException e) {
                e.printStackTrace();
            }
        } else if (tag.equalsIgnoreCase("tr")) {
            printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel); // TODO
        } else if (tag.equalsIgnoreCase("td")) {
            Cell cell = new Cell();
            printElementChildrenHtml(element, cell, depth + 1, normalFont, parentLevel); // TODO
            ((Table) parent).addCell(cell);
        } else {
            System.err.println("Unknown element " + element.getName());
            printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel);
        }
    } else {
        return; // could be BidiElement - not sure what it is
    }
}

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

License:Apache License

private Object generateElementHtml(Element element, int depth, Font font) {
    String tag = element.getName();
    Object myself;//from   ww w.j av  a  2 s  .  co m
    Object av;
    if (element instanceof HTMLDocument.RunElement) {
        HTMLDocument.RunElement re = (HTMLDocument.RunElement) element;
        int start = re.getStartOffset();
        int end = re.getEndOffset();
        try {
            String content = re.getDocument().getText(start, end - start);
            HtmlAttr htmlattr = printAttributesHtml(re);
            av = re.getAttribute(CSS.Attribute.FONT_SIZE);
            String fontsize = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.FONT_FAMILY);
            String fontfamily = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.COLOR);
            String fontcolor = av == null ? null : av.toString();
            if (fontcolor != null || fontsize != null || fontfamily != null) {
                if (fontfamily == null)
                    fontfamily = font.getFamilyname();
                if (fontsize != null && fontsize.endsWith("pt"))
                    fontsize = fontsize.substring(0, fontsize.indexOf("pt"));
                float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 8);
                int style = font.getStyle();
                Color color;
                if (fontcolor != null) {
                    color = Color.decode(fontcolor);
                } else
                    color = font.getColor();
                font = FontFactory.getFont(fontfamily, size, style, color);
            } else if (htmlattr.bold || htmlattr.italic) {
                String family = font.getFamilyname();
                float size = font.getSize();
                Color color = font.getColor();
                if (htmlattr.bold && htmlattr.italic)
                    font = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
                else if (htmlattr.italic)
                    font = FontFactory.getFont(family, size, Font.ITALIC, color);
                else if (htmlattr.bold)
                    font = FontFactory.getFont(family, size, Font.BOLD);
            }
            myself = new Chunk(content, font);
        } catch (BadLocationException e) {
            e.printStackTrace();
            myself = null;
        }
    } else if (element instanceof HTMLDocument.BlockElement) {
        HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element;
        HtmlAttr htmlattr = printAttributesHtml(be);
        if (htmlattr.bold) {
            System.out.println("+++BOLD!!!");
        }
        av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN);
        String align = av == null ? null : av.toString();
        if (htmlattr.bold || htmlattr.italic) {
            String family = font.getFamilyname();
            float size = font.getSize();
            Color color = font.getColor();
            if (htmlattr.bold && htmlattr.italic)
                font = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
            else if (htmlattr.italic)
                font = FontFactory.getFont(family, size, Font.ITALIC, color);
            else if (htmlattr.bold)
                font = FontFactory.getFont(family, size, Font.BOLD, Color.blue);
        }
        if (tag.equalsIgnoreCase("html")) {
            myself = generateElementChildrenHtml(element, depth + 1, font);
        } else if (tag.equalsIgnoreCase("head")) {
            myself = null;
        } else if (tag.equalsIgnoreCase("body")) {
            myself = generateElementChildrenHtml(element, depth + 1, font);
        } else if (tag.equalsIgnoreCase("p") || tag.equalsIgnoreCase("p-implied")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            Paragraph paragraph = new Paragraph();
            paragraph.setFirstLineIndent(0F);
            for (Object child : children) {
                if (child instanceof Chunk) {
                    Chunk chunk = (Chunk) child;
                    /*if (!chunk.getContent().equals("\n"))*/ paragraph.add(chunk);
                } else
                    paragraph.add(child);
            }
            if (align != null)
                paragraph.setAlignment(align);
            myself = paragraph;
        } else if (tag.equalsIgnoreCase("h1") || tag.equalsIgnoreCase("h2") || tag.equalsIgnoreCase("h3")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, subSectionFont);
            Paragraph title = new Paragraph();
            for (Object child : children) {
                title.add(child);
            }
            myself = new TempSectionPdf(title);
        } else if (tag.equalsIgnoreCase("ul")) {
            com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f);
            list.setIndentationLeft(25.0f);
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                list.add(child);
            }
            myself = list;
        } else if (tag.equalsIgnoreCase("ol")) {
            com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f);
            list.setIndentationLeft(25.0f);
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                list.add(child);
            }
            myself = list;
        } else if (tag.equalsIgnoreCase("li")) {
            ListItem li = new ListItem();
            li.setSpacingAfter(0.0f);
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                li.add(child);
            }
            myself = li;
        } else if (tag.equalsIgnoreCase("table")) {
            List<Object> rows = generateElementChildrenHtml(element, depth + 1, normalFont);
            try {
                int ncols = 0;
                for (Object row : rows) {
                    if (row instanceof List<?>) {
                        int n = ((List<?>) row).size();
                        if (n > ncols)
                            ncols = n;
                    }
                }
                Table table = new Table(2);
                table.setBorderWidth(1);
                table.setBorderColor(new Color(0, 128, 128));
                table.setPadding(1.0f);
                table.setSpacing(0.5f);
                Cell c = new Cell("header");
                c.setHeader(true);
                c.setColspan(ncols);
                table.addCell(c);
                table.endHeaders();
                for (Object row : rows) {
                    if (row instanceof List<?>) {
                        for (Object cell : (List<?>) row) {
                            if (cell instanceof Cell)
                                table.addCell((Cell) cell);
                        }
                    }
                }
                myself = table;
            } catch (BadElementException e) {
                e.printStackTrace();
                myself = null;
            }
        } else if (tag.equalsIgnoreCase("tr")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            myself = children;
        } else if (tag.equalsIgnoreCase("td")) {
            Cell cell = new Cell();
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                cell.add(child);
            }
            myself = cell;
        } else if (tag.equalsIgnoreCase("div")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            Paragraph paragraph = new Paragraph();
            paragraph.setFirstLineIndent(0F);
            for (Object child : children) {
                paragraph.add(child);
            }
            if (align != null)
                paragraph.setAlignment(align);
            myself = paragraph;
        } else {
            System.err.println("Unknown element " + element.getName());
            myself = null;
        }
    } else {
        myself = null; // could be BidiElement - not sure what it is
    }
    return myself;
}

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

License:Open Source License

private static void makeTestPage(Font tableFont, Rectangle borders,
        @SuppressWarnings("unused") PdfWriter writer, Document document, float padding, boolean ascender,
        boolean descender) throws BadElementException, DocumentException {
    document.newPage();/*from   ww w  .  ja  va2  s. c  o m*/
    PdfPTable table = null;
    table = new PdfPTable(4);
    table.setWidthPercentage(100f);

    float leading = tableFont.getSize() * 1.2f;

    table.addCell(makeCell("1-Top", Element.ALIGN_TOP, Element.ALIGN_LEFT, tableFont, leading, padding, borders,
            ascender, descender));
    table.addCell(makeCell("2-Middle", Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, tableFont, leading, padding,
            borders, ascender, descender));
    table.addCell(makeCell("3-Bottom", Element.ALIGN_BOTTOM, Element.ALIGN_LEFT, tableFont, leading, padding,
            borders, ascender, descender));
    table.addCell(makeCell("4-Has a y", Element.ALIGN_TOP, Element.ALIGN_LEFT, tableFont, leading, padding,
            borders, ascender, descender));

    table.addCell(makeCell("5-Abcdy", Element.ALIGN_TOP, Element.ALIGN_LEFT, tableFont, leading, padding,
            borders, ascender, descender));
    table.addCell(makeCell("6-Abcdy", Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, tableFont, leading, padding,
            borders, ascender, descender));
    table.addCell(makeCell("7-Abcdy", Element.ALIGN_BOTTOM, Element.ALIGN_LEFT, tableFont, leading, padding,
            borders, ascender, descender));
    table.addCell(makeCell("8-This\nis\na little\ntaller", Element.ALIGN_TOP, Element.ALIGN_LEFT, tableFont,
            leading, padding, borders, ascender, descender));
    document.add(table);
}

From source file:com.krawler.accounting.fontsetting.FontFamily.java

License:Open Source License

public void addFont(FontContext context, Font font) {
    if (font.getBaseFont() == null) {
        BaseFont bf = font.getCalculatedBaseFont(true);
        font = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
    }//from  w  w w.ja  va  2 s  .  c o  m
    fontsMap.put(context, font);
    if (primaryFont == null)
        primaryFont = font;
}

From source file:com.krawler.accounting.fontsetting.FontFamily.java

License:Open Source License

public void addFont(FontContext context, Font font, boolean isPrimary) {
    if (font.getBaseFont() == null) {
        BaseFont bf = font.getCalculatedBaseFont(true);
        font = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
    }//from  w  ww.j a  v a  2s  .co  m
    fontsMap.put(context, font);
    if (isPrimary)
        primaryFont = font;
}

From source file:com.krawler.accounting.fontsetting.FontFamilySelector.java

License:Open Source License

public void addFontFamily(FontFamily fontFamily) {
    Font font = fontFamily.getPrimaryFont();

    if (font.getBaseFont() == null) {
        BaseFont bf = font.getCalculatedBaseFont(true);
        font = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
    }/* w ww .j  a  v a 2  s  .c  o m*/

    families.add(fontFamily);
}

From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableParagraph.java

License:Open Source License

@SuppressWarnings("unchecked")
private void postProcessLineHeightAndBaseline() {
    // adjust line height and baseline
    Font font = getMostOftenUsedFont();
    if (font == null || font.getBaseFont() == null) {
        font = this.font;
    }/*from   w  w  w .ja v  a 2 s  .co  m*/
    if (font != null && font.getBaseFont() != null) {
        // iText and open office computes proportional line height differently
        // [iText] line height = coefficient * font size
        // [open office] line height = coefficient * (font ascender + font descender + font extra margin)
        // we have to increase paragraph line height to generate pdf similar to open office document
        // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph
        float size = font.getSize();
        float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size);
        float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value
        float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size);
        float multiplier = (ascender + descender + margin) / size;
        if (multipliedLeading > 0.0f) {
            setMultipliedLeading(getMultipliedLeading() * multiplier);
        }

        // iText seems to output text with baseline lower than open office
        // we raise all paragraph text by some amount
        // again this may be inaccurate if fonts with different size are used in this paragraph
        float itextdescender = -font.getBaseFont().getFontDescriptor(BaseFont.DESCENT, size); // negative
        float textRise = itextdescender + getTotalLeading() - font.getSize() * multiplier;
        ArrayList<Chunk> chunks = getChunks();
        for (Chunk chunk : chunks) {
            Font f = chunk.getFont();
            if (f != null) {
                // have to raise underline and strikethru as well
                float s = f.getSize();
                if (f.isUnderlined()) {
                    f.setStyle(f.getStyle() & ~Font.UNDERLINE);
                    chunk.setUnderline(s * 1 / 17, s * -1 / 7 + textRise);
                }
                if (f.isStrikethru()) {
                    f.setStyle(f.getStyle() & ~Font.STRIKETHRU);
                    chunk.setUnderline(s * 1 / 17, s * 1 / 4 + textRise);
                }
            }
            chunk.setTextRise(chunk.getTextRise() + textRise);
        }
    }
}

From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableParagraph.java

License:Open Source License

@SuppressWarnings("unchecked")
private Font getMostOftenUsedFont() {
    // determine font most often used in this paragraph
    // font with the highest count of non-whitespace characters
    // is considered to be most often used
    Map<String, Font> fontMap = new LinkedHashMap<String, Font>();
    Map<String, Integer> countMap = new LinkedHashMap<String, Integer>();
    Font mostUsedFont = null;//ww w  .  jav a 2 s .  c  om
    int mostUsedCount = -1;
    ArrayList<Chunk> chunks = getChunks();
    for (Chunk chunk : chunks) {
        Font font = chunk.getFont();
        int count = 0;
        String text = chunk.getContent();
        if (text != null) {
            // count non-whitespace characters in a chunk
            for (int i = 0; i < text.length(); i++) {
                char ch = text.charAt(i);
                if (!Character.isWhitespace(ch)) {
                    count++;
                }
            }
        }
        if (font != null) {
            // update font and its count
            String fontKey = font.getFamilyname() + "_" + (int) font.getSize();
            Font fontTmp = fontMap.get(fontKey);
            if (fontTmp == null) {
                fontMap.put(fontKey, font);
            }
            Integer countTmp = countMap.get(fontKey);
            int totalCount = countTmp == null ? count : countTmp + count;
            countMap.put(fontKey, totalCount);
            // update most used font
            if (totalCount > mostUsedCount) {
                mostUsedCount = totalCount;
                mostUsedFont = font;
            }
        }
    }
    return mostUsedFont;
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableParagraph.java

License:Open Source License

/**
 * Adjust iText multiplied leading according the given font.
 * //  w w  w. j  av  a 2  s . c o  m
 * @param font
 */
public void adjustMultipliedLeading(Font font) {
    if (originMultipliedLeading != null && font != null && font.getBaseFont() != null) {
        // iText and open office computes proportional line height differently
        // [iText] line height = coefficient * font size
        // [MS Word] line height = coefficient * (font ascender + font descender + font extra margin)
        // we have to increase paragraph line height to generate pdf similar to OOXML docx document
        // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph
        float size = font.getSize();
        float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size);
        float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value
        float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size);
        float multiplier = (ascender + descender + margin) / size;
        super.setMultipliedLeading(originMultipliedLeading * multiplier);
    }
}

From source file:mesquite.lib.MesquitePDFFile.java

License:Open Source License

/**
@arg s String holds the text that the pdf file will contain
@arg font java.awt.Font the font is specified this way for compatibility with the similar method in MesquitePrintJob
 *///  w w w  . ja  v  a  2 s  .c om
public void printText(String s, java.awt.Font font) {
    final String exceptionMessage = "Error, an exception occurred while creating the pdf text document: ";
    if (s == null || font == null)
        return;

    //do the translation from logical to physical font here      
    //currently, the only font this method ever gets called with is "Monospaced",PLAIN,10.  So the
    //translation effort here will be minimal      
    int desiredFontFamily; // "Monospaced" isn't defined in com.lowagie.text.Font
    if (font.getFamily().equals("Monospaced"))
        desiredFontFamily = com.lowagie.text.Font.COURIER;
    else
        desiredFontFamily = com.lowagie.text.Font.TIMES_ROMAN;
    com.lowagie.text.Font textFont;
    switch (font.getStyle()) {
    case java.awt.Font.BOLD: {
        textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(), com.lowagie.text.Font.BOLD);
        break;
    }
    case java.awt.Font.ITALIC: {
        textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(), com.lowagie.text.Font.ITALIC);
        break;
    }
    case java.awt.Font.PLAIN: {
        textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(), com.lowagie.text.Font.NORMAL);
        break;
    }
    default: {
        textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(),
                com.lowagie.text.Font.BOLDITALIC);
    }
    }
    document = new Document();
    try {
        PdfWriter.getInstance(document, new FileOutputStream(pdfPathString));
        addMetaData(document);
        document.open();
        document.add(new Paragraph(s, textFont));
    } catch (DocumentException de) {
        MesquiteTrunk.mesquiteTrunk.alert(exceptionMessage + de.getMessage());
    } catch (IOException ioe) {
        MesquiteTrunk.mesquiteTrunk.alert(exceptionMessage + ioe.getMessage());
    }
    this.end();
}