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

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

Introduction

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

Prototype


public BaseFont awtToPdf(Font font) 

Source Link

Document

Returns a BaseFont which can be used to represent the given AWT Font

Usage

From source file:org.geomajas.plugin.print.component.PdfContext.java

License:Open Source License

/**
 * Return the text box for the specified text and font.
 *
 * @param text text/*from  www  .  j ava 2s . co  m*/
 * @param font font
 * @return text box
 */
public Rectangle getTextSize(String text, Font font) {
    template.saveState();
    // get the font
    DefaultFontMapper mapper = new DefaultFontMapper();
    BaseFont bf = mapper.awtToPdf(font);
    template.setFontAndSize(bf, font.getSize());
    // calculate text width and height
    float textWidth = template.getEffectiveStringWidth(text, false);
    float ascent = bf.getAscentPoint(text, font.getSize());
    float descent = bf.getDescentPoint(text, font.getSize());
    float textHeight = ascent - descent;
    template.restoreState();
    return new Rectangle(0, 0, textWidth, textHeight);
}

From source file:org.geomajas.plugin.print.component.PdfContext.java

License:Open Source License

/**
 * Draw text in the center of the specified box.
 *
 * @param text text/*from  w  w w.  j a  v  a 2 s  . c o  m*/
 * @param font font
 * @param box box to put text int
 * @param fontColor colour
 */
public void drawText(String text, Font font, Rectangle box, Color fontColor) {
    template.saveState();
    // get the font
    DefaultFontMapper mapper = new DefaultFontMapper();
    BaseFont bf = mapper.awtToPdf(font);
    template.setFontAndSize(bf, font.getSize());

    // calculate descent
    float descent = 0;
    if (text != null) {
        descent = bf.getDescentPoint(text, font.getSize());
    }

    // calculate the fitting size
    Rectangle fit = getTextSize(text, font);

    // draw text if necessary
    template.setColorFill(fontColor);
    template.beginText();
    template.showTextAligned(PdfContentByte.ALIGN_LEFT, text,
            origX + box.getLeft() + 0.5f * (box.getWidth() - fit.getWidth()),
            origY + box.getBottom() + 0.5f * (box.getHeight() - fit.getHeight()) - descent, 0);
    template.endText();
    template.restoreState();
}