Example usage for com.lowagie.text Chunk getFont

List of usage examples for com.lowagie.text Chunk getFont

Introduction

In this page you can find the example usage for com.lowagie.text Chunk getFont.

Prototype

public Font getFont() 

Source Link

Document

Gets the font of this Chunk.

Usage

From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java

License:Open Source License

private static Chunk getChunk(Workbook wb, Cell cell) {
    Chunk phrase = null;

    switch (cell.getCellType() == Cell.CELL_TYPE_FORMULA ? cell.getCachedFormulaResultType()
            : cell.getCellType()) {/*w w w.  ja v a2s .  com*/
    case Cell.CELL_TYPE_STRING:
        phrase = new Chunk("" + cell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        String format = cell.getCellStyle().getDataFormatString();
        if (cell.getCellStyle().getDataFormat() > 0) {
            try {
                if (format.contains("0")) {
                    //Decimal
                    DecimalFormat df = new DecimalFormat(format);
                    phrase = new Chunk(df.format(cell.getNumericCellValue()));
                } else if (format.contains("h:")) {
                    phrase = new Chunk(FormatterUtils.formatDateTimeShort(cell.getDateCellValue()));
                } else if (format.contains("yy")) {
                    phrase = new Chunk(FormatterUtils.formatDate(cell.getDateCellValue()));
                }
            } catch (Exception e) {
                System.err.println(e);
            }
        }
        if (phrase == null) {
            phrase = new Chunk("" + (int) cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BLANK:
        phrase = new Chunk("");
        break;
    default:
        phrase = new Chunk("" + cell.getCellType());
    }
    Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());
    short[] rgb = HSSFColor.getIndexHash().get((int) font.getColor()).getTriplet();

    phrase.setFont(new com.lowagie.text.Font(phrase.getFont().getBaseFont(), font.getFontHeightInPoints() - 3,
            (font.getBold() ? com.lowagie.text.Font.BOLD : com.lowagie.text.Font.NORMAL),
            new Color(rgb[0], rgb[1], rgb[2])));
    return phrase;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public Element getElement() {
    // underline font if not explicitly set
    ArrayList<Chunk> chunks = getChunks();
    for (Chunk chunk : chunks) {
        Font f = chunk.getFont();
        if (f != null && !f.isUnderlined()) {
            f = new Font(f);
            f.setStyle(f.getStyle() | Font.UNDERLINE);
            chunk.setFont(f);//from w  ww. j  a  va  2 s  .  c o  m
        }
    }
    return this;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void addElement(Element element, boolean addLabel) {
    if (element instanceof Chunk) {
        // may it happen?
        Chunk ch = (Chunk) element;
        StylableParagraph p = new StylableParagraph(null, null);
        p.setFont(ch.getFont());
        p.addElement(ch);// www . j  a  va2s.c  o  m
        element = p.getElement();
    }
    if (element instanceof Phrase) {
        Phrase p = (Phrase) element;
        StylableListItem li = new StylableListItem(p);
        // determine font, it may be set explicitly or use paragraph font
        Font symbolFont = symbol.getFont();
        if (symbolFont.isStandardFont()) {
            ArrayList<Chunk> chunks = p.getChunks();
            for (Chunk chunk : chunks) {
                // use first specified font
                if (!chunk.getFont().isStandardFont()) {
                    symbolFont = chunk.getFont();
                    break;
                }
            }
            if (symbolFont.isStandardFont()) {
                // use paragraph font
                symbolFont = p.getFont();
            }
        }
        // determine line height
        float lineHeight = StylableParagraph.DEFAULT_LINE_HEIGHT;
        boolean lineHeightProportional = true;
        if (element instanceof IStylableElement) {
            IStylableElement stylableElement = (IStylableElement) element;
            Style style = stylableElement.getLastStyleApplied();
            if (style != null) {
                StyleParagraphProperties paragraphProperties = style.getParagraphProperties();
                StyleLineHeight lineHeightObj = paragraphProperties.getLineHeight();
                if (lineHeightObj != null && lineHeightObj.getLineHeight() != null) {
                    lineHeight = lineHeightObj.getLineHeight();
                    lineHeightProportional = lineHeightObj.isLineHeightProportional();
                }
            }
        }
        if (addLabel) {
            if (numbered || lettered || romanNumbered) {
                StringBuilder sbuf = new StringBuilder(preSymbol);
                int index = first + list.size();
                if (lettered) {
                    sbuf.append(RomanAlphabetFactory.getString(index, lowercase));
                } else if (romanNumbered) {
                    sbuf.append(RomanNumberFactory.getString(index, lowercase));
                } else {
                    sbuf.append(index);
                }
                sbuf.append(postSymbol);
                li.setListSymbol(sbuf.toString(), symbolFont, lineHeight, lineHeightProportional);
            } else {
                li.setListSymbol(symbol.getContent(), symbolFont, lineHeight, lineHeightProportional);
            }
        } else {
            li.setListSymbol("", symbolFont, lineHeight, lineHeightProportional);
        }
        li.setIndentationLeft(symbolIndent);
        li.setIndentationRight(0.0f);
        list.add(li);
    } else if (element instanceof List) {
        List l = (List) element;
        // open office specifies absolute list indentation
        // but iText computes indentation relative to parent list
        // so we have to set difference
        l.setIndentationLeft(l.getIndentationLeft() - this.getIndentationLeft());
        first--;
        list.add(l);
    }
}

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

License:Open Source License

public void applyStyles(Style style) {
    this.lastStyleApplied = style;

    Map<Integer, StyleListProperties> listPropertiesMap = style.getListPropertiesMap();
    if (listPropertiesMap != null) {
        StyleListProperties listProperties = getListProperties(listPropertiesMap, listLevel);
        if (listProperties != null) {
            String bulletChar = listProperties.getBulletChar();
            if (bulletChar != null) {
                // list item label is a char
                Chunk symbol = new Chunk(bulletChar);

                StyleTextProperties textProperties = listProperties.getTextProperties();
                if (textProperties != null) {
                    Font font = textProperties.getFont();
                    if (font != null) {
                        symbol.setFont(font);
                    }/*from   w  ww.ja  v  a  2s. c  om*/
                }

                super.setListSymbol(symbol);
            }

            Image image = listProperties.getImage();
            if (image != null) {
                // list item label is an image
                Float width = listProperties.getWidth();
                if (width != null) {
                    image.scaleAbsoluteWidth(width);
                }

                Float height = listProperties.getHeight();
                if (height != null) {
                    image.scaleAbsoluteHeight(height);
                }

                super.setListSymbol(new Chunk(image, 0.0f, 0.0f));
            }

            if (bulletChar == null && image == null) {
                // list item label is a number
                Chunk symbol = new Chunk("");

                StyleTextProperties textProperties = listProperties.getTextProperties();
                if (textProperties != null) {
                    Font font = textProperties.getFont();
                    if (font != null) {
                        symbol.setFont(font);
                    }
                }

                Integer startValue = listProperties.getStartValue();
                if (startValue != null) {
                    super.setFirst(startValue);
                }

                StyleNumFormat numFormat = listProperties.getNumFormat();
                if (numFormat != null) {
                    String numPrefix = listProperties.getNumPrefix();
                    if (numPrefix != null) {
                        super.setPreSymbol(numPrefix);
                        symbol = new Chunk(numPrefix, symbol.getFont());
                    }

                    String numSuffix = listProperties.getNumSuffix();
                    if (numSuffix != null) {
                        super.setPostSymbol(numSuffix);
                        symbol.append(numSuffix);
                    }

                    super.setNumbered(true);
                    super.setLettered(numFormat.isAlphabetical());
                    this.romanNumbered = numFormat.isRoman();
                    super.setLowercase(numFormat.isLowercase());
                }

                super.setListSymbol(symbol);
            }

            // set indentation, it is specified in different way by Open Office and MsWord
            Float marginLeft = listProperties.getMarginLeft();
            Float textIndent = listProperties.getTextIndent();
            Float spaceBefore = listProperties.getSpaceBefore();
            Float minLabelWidth = listProperties.getMinLabelWidth();
            if (marginLeft != null && textIndent != null) {
                // ODT generated by Open Office
                super.setIndentationLeft(Math.max(marginLeft + textIndent, 0.0f));
                super.setSymbolIndent(Math.max(-textIndent, 0.0f));
            } else if (spaceBefore != null && minLabelWidth != null) {
                // ODT generated by MsWord
                super.setIndentationLeft(Math.max(spaceBefore, 0.0f));
                super.setSymbolIndent(Math.max(minLabelWidth, 0.0f));
            }
        }
    }
}

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;
    }/*  ww w  . j  a v a 2  s  . c o 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;//from  www  .  ja  va2s .  co  m
    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:org.displaytag.render.ItextTableWriter.java

License:Artistic License

/**
 * Makes chunk content bold.//  w  w  w .j  ava 2  s  .  c  om
 * @param chunk The chunk whose content is to be rendered bold.
 * @param color The font color desired.
 */
private void setBoldStyle(Chunk chunk, Color color) {
    Font font = chunk.getFont();
    chunk.setFont(FontFactory.getFont(font.getFamilyname(), font.getSize(), Font.BOLD, color));
}

From source file:org.gtdfree.addons.PDFExportAddOn.java

License:Open Source License

/**
 * @param model/*from  w  w w  .j  a  v  a  2  s.  co  m*/
 * @param doc
 * @param a
 * @throws DocumentException
 * @throws IOException
 * @throws BadElementException
 */
private void addSingleActionTable(GTDModel model, Document doc, Action a)
        throws DocumentException, IOException, BadElementException {
    Chunk ch;
    PdfPTable t;
    PdfPCell c;
    t = new PdfPTable(3);
    t.setSpacingBefore(5f);
    t.setWidthPercentage(100f);

    Paragraph ph = newParagraph();
    ch = newChunk(ID);
    ph.add(ch);
    ch = newChunk(String.valueOf(a.getId()));
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(CREATED);
    ph.add(ch);
    ch = newChunk(ApplicationHelper.toISODateTimeString(a.getCreated()));
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    if (a.isOpen()) {
        ch = newChunk(OPEN);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_OPEN);
        ph.add(ch);
        ch = newOpenChunk();
        ph.add(ch);
    } else if (a.isResolved()) {
        ch = newChunk(RESOLVED);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_RESOLVED);
        ph.add(ch);
        ch = newResolvedChunk();
        ph.add(ch);
    } else if (a.isDeleted()) {
        ch = newChunk(DELETED);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_DELETED);
        ph.add(ch);
        ch = newDeletedChunk();
        ph.add(ch);
    } else {
        ch = newChunk(STALLED);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_DELETED);
        ph.add(ch);
        ch = newStalledChunk();
        ph.add(ch);
    }
    c = newCell(ph);
    c.setHorizontalAlignment(Cell.ALIGN_RIGHT);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(PRIORITY);
    ph.add(ch);
    ch = newChunk(a.getPriority() == null ? NONE : a.getPriority().toString());
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    ch = newChunk(" ");
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);

    if (a.getPriority() == null || a.getPriority() == Priority.None) {
        ch = newNoneStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
    } else if (a.getPriority() == Priority.Low) {
        ch = newLowStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
    } else if (a.getPriority() == Priority.Medium) {
        ch = newLowStarChunk();
        ph.add(ch);
        ch = newMediumStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
    } else if (a.getPriority() == Priority.High) {
        ch = newLowStarChunk();
        ph.add(ch);
        ch = newMediumStarChunk();
        ph.add(ch);
        ch = newHighStarChunk();
        ph.add(ch);
    }

    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(REMINDER);
    ph.add(ch);
    ch = newChunk(a.getRemind() != null ? ApplicationHelper.toISODateString(a.getRemind()) : NONE);
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(PROJECT);
    ph.add(ch);
    if (a.getProject() != null) {
        ch = newChunk(model.getProject(a.getProject()).getName());
    } else {
        ch = newChunk(NONE);
    }
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    c = newDescriptionCell(a.getDescription());
    c.setColspan(3);
    t.addCell(c);

    if (a.getUrl() != null) {

        ch = newChunk(a.getUrl().toString());
        ch.setAnchor(a.getUrl());
        ch.getFont().setColor(Color.BLUE);

        c = newCell(new Paragraph(ch));
        c.setColspan(3);
        t.addCell(c);
    }

    doc.add(t);
}

From source file:org.gtdfree.addons.PDFExportAddOn.java

License:Open Source License

private Chunk newResolvedChunk() throws DocumentException, IOException {
    Chunk c = newChunk(CHECK_RESOLVED);
    c.getFont().setSize(baseFontSize + 2);
    c.getFont().setColor(COLOR_RESOLVED);
    return c;/*from  w ww  .  ja  va  2 s.  com*/
}

From source file:org.gtdfree.addons.PDFExportAddOn.java

License:Open Source License

private Chunk newOpenChunk() throws DocumentException, IOException {
    Chunk c = newChunk(CHECK_OPEN);
    c.getFont().setSize(baseFontSize + 2);
    c.getFont().setColor(COLOR_OPEN);//from   w  w w.  j  av  a2 s  .  co  m
    return c;
}