Example usage for org.apache.pdfbox.pdmodel.font PDFont getFontDescriptor

List of usage examples for org.apache.pdfbox.pdmodel.font PDFont getFontDescriptor

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.font PDFont getFontDescriptor.

Prototype

@Override
    public PDFontDescriptor getFontDescriptor() 

Source Link

Usage

From source file:de.hrogge.CompactPDFExport.PDFSeite.java

License:Apache License

public void drawText(PDFont font, int x1, int x2, int y1, int y2, String text, boolean center)
        throws IOException {
    PDFontDescriptor descr = font.getFontDescriptor();
    float boxProp, textProp;
    float boxWidth, boxHeight;
    float textWidth, textHeight;
    float shiftX, shiftY;

    boxHeight = (getY(y1) - getY(y2)) - 2 * randText;
    boxWidth = (getX(x2) - getX(x1)) - 2 * randText;
    boxProp = boxWidth / boxHeight;/*from ww w. j a  v  a 2s . c  om*/

    textHeight = (descr.getFontBoundingBox().getHeight()) / 1000f;
    textWidth = font.getStringWidth(text) / 1000f;
    textProp = textWidth / textHeight;

    /* begrenze vertikale Skalierung */
    while (textProp / boxProp > 1.75 && text.contains(" ")) {
        text = text.substring(0, text.lastIndexOf(' ')) + "...";
        textWidth = font.getStringWidth(text) / 1000f;
        textProp = textWidth / textHeight;
    }

    stream.beginText();

    if (textProp > boxProp) {
        /* scale over width */
        stream.setFont(font, boxWidth / textWidth);

        shiftX = 0f;
        shiftY = boxHeight / 2 - (textHeight * boxWidth / textWidth) / 2;
        shiftY += -descr.getDescent() / 1000 * (boxWidth / textWidth);
    } else {
        /* scale over height */
        stream.setFont(font, boxHeight / textHeight);

        if (center) {
            shiftX = boxWidth / 2 - textWidth * (boxHeight / textHeight) / 2;
        } else {
            shiftX = 0f;
        }
        shiftY = -descr.getDescent() / 1000 * (boxHeight / textHeight);
    }

    stream.moveTextPositionByAmount(getX(x1) + randText + shiftX, getY(y2) + randText + shiftY);
    stream.drawString(text);
    stream.endText();
}

From source file:gov.samhsa.c2s.common.pdfbox.enhance.HexPdf.java

License:Apache License

/**
 * Set font type to use.//from   w  ww .  j  av a2  s .  co  m
 *
 * @param font font to use, PDFont type
 * @see PDFont
 */
public void setFont(PDFont font) {
    this.font = font;
    try {
        cs.setFont(font, fontSize);
    } catch (IOException ex) {
        Logger.getLogger(HexPdf.class.getName()).log(Level.SEVERE, null, ex);
    }
    lineSep = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
}

From source file:javaexample.RadialTextPdf.java

License:Open Source License

private void generatePage(PDDocument document) throws IOException {
    // Creates a new page.
    PDPage page = new PDPage(pageRect);
    document.addPage(page);/*from   w ww . j a  va  2  s .  c  om*/

    // Gets boundings of the page.
    PDRectangle rect = page.getMediaBox();

    // Calculates the side of the square that fits into the page.
    float squareSide = Math.min(rect.getWidth(), rect.getHeight());

    // Calculates the center point of the page.
    float centerX = (rect.getLowerLeftX() + rect.getUpperRightX()) / 2;
    float centerY = (rect.getLowerLeftY() + rect.getUpperRightY()) / 2;

    PDPageContentStream cos = new PDPageContentStream(document, page);

    // Creates the font for the radial text.
    PDFont font = PDType1Font.HELVETICA_BOLD; // Standard font
    float fontSize = squareSide / 30;
    float fontAscent = font.getFontDescriptor().getAscent() / 1000 * fontSize;

    // Calculates key values for the drawings.
    float textX = squareSide / 3.4F; // x of the text.
    float textY = -fontAscent / 2; // y of the text (for vertical centering of text).
    float lineToX = textX * 0.97F; // x destination for the line.
    float lineWidth = squareSide / 900; // width of lines.

    // Moves the origin (0,0) of the axes to the center of the page.
    cos.concatenate2CTM(AffineTransform.getTranslateInstance(centerX, centerY));

    for (float degrees = 0; degrees < 360; degrees += 7.5) {
        double radians = degrees2Radians(degrees);

        // Creates a pure color with the hue based on the angle.
        Color textColor = Color.getHSBColor(degrees / 360.0F, 1, 1);

        // Saves the graphics state because the angle changes on each iteration.
        cos.saveGraphicsState();

        // Rotates the axes by the angle expressed in radians.
        cos.concatenate2CTM(AffineTransform.getRotateInstance(radians));

        // Draws a line from the center of the page.
        cos.setLineWidth(lineWidth);
        cos.moveTo(0, 0);
        cos.lineTo(lineToX, 0);
        cos.stroke();

        // Draws the radial text.
        cos.beginText();
        cos.setNonStrokingColor(textColor);
        cos.setFont(font, fontSize);
        cos.moveTextPositionByAmount(textX, textY);
        cos.drawString("PDF");
        cos.endText();

        // Restores the graphics state to remove rotation transformation.
        cos.restoreGraphicsState();
    }

    cos.close();
}

From source file:net.heksemann.hexpdf.HexPDF.java

License:Apache License

/**
 * Set font type to use./*from w w  w  .j  a  v  a 2  s .  c  om*/
 *
 * @param font font to use, PDFont type
 *
 * @see PDFont
 */
public void setFont(PDFont font) {
    this.font = font;
    try {
        cs.setFont(font, fontSize);
    } catch (IOException ex) {
        Logger.getLogger(HexPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    lineSep = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
}

From source file:net.timendum.pdf.StatisticParser.java

License:Open Source License

@Override
protected void writeLineStart(List<TextPosition> line) {
    if (isLineEmpty(line)) {
        return;//from   www  .  java  2s.c  om
    }
    lines++;

    float lineY = getFirstTrimmed(line).getY();
    if (prevLineY >= 0f) {
        incrementOrAdd(lineSpacing, lineY - prevLineY);
    }
    prevLineY = lineY;

    float start = getFirstTrimmed(line).getX();
    //      leftMargin += start;
    incrementOrAdd(leftMargin, start);
    TextPosition lastTrimmed = getLastTrimmed(line);
    float end = lastTrimmed.getX() + lastTrimmed.getWidth();
    //      rightMargin += end;
    incrementOrAdd(rightMargin, end);

    Float fontSize;
    for (TextPosition t : line) {
        PDFont font = t.getFont();
        if (font != null) {
            PDFontDescriptor fontDescriptor = font.getFontDescriptor();
            if (fontDescriptor != null)
                incrementOrAdd(fontWeight, fontDescriptor.getFontWeight());
        }
        fontSize = t.getFontSizeInPt();
        if (fontSize > 0) {
            incrementOrAdd(linesFontSize, fontSize);
        }
    }
}

From source file:no.digipost.print.validate.PdfFontValidator.java

License:Apache License

public List<PDFont> findNonSupportedFonts(Iterable<PDFont> fonter) {
    List<PDFont> nonSupported = new ArrayList<>();
    for (PDFont font : fonter) {
        PDFontDescriptor fontDescriptor = font.getFontDescriptor();
        if (fontDescriptor != null) {
            if (!erFontDescriptorAkseptabelForPrint(fontDescriptor)) {
                nonSupported.add(font);/*w w  w.  j a v a2  s  .c  o m*/
            }
        } else {
            if (!(font instanceof PDType0Font)) {
                if (!erAkseptabelForPrint(font.getBaseFont())) {
                    nonSupported.add(font);
                }
            }
        }
    }
    return unmodifiableList(nonSupported);
}

From source file:org.apache.fop.render.pdf.pdfbox.FOPPDFMultiByteFont.java

License:Apache License

private InputStream readFontFile(PDFont font) throws IOException {
    PDFontDescriptor fd = font.getFontDescriptor();
    if (font instanceof PDType0Font) {
        PDCIDFont cidFont = ((PDType0Font) font).getDescendantFont();
        fd = cidFont.getFontDescriptor();
    }/*from w ww  . j av a2  s  .  c om*/
    PDStream ff = fd.getFontFile3();
    if (ff == null) {
        ff = fd.getFontFile2();
        if (ff == null) {
            ff = fd.getFontFile();
        }
    }
    if (ff == null) {
        throw new IOException(font.getName() + " no fontfile");
    }
    InputStream is = ff.createInputStream();
    return new ByteArrayInputStream(IOUtils.toByteArray(is));
}

From source file:org.apache.fop.render.pdf.pdfbox.FOPPDFMultiByteFont.java

License:Apache License

protected static void setProperties(CustomFont cFont, PDFont font) {
    if (font.getFontDescriptor() != null) {
        cFont.setCapHeight((int) font.getFontDescriptor().getCapHeight());
        cFont.setAscender((int) font.getFontDescriptor().getAscent());
        cFont.setDescender((int) font.getFontDescriptor().getDescent());
        cFont.setXHeight((int) font.getFontDescriptor().getXHeight());
        cFont.setStemV((int) font.getFontDescriptor().getStemV());
    }/*from  www  .  j ava  2s .c om*/
}

From source file:org.apache.fop.render.pdf.pdfbox.FOPPDFSingleByteFont.java

License:Apache License

private PDStream readFontFile(PDFont font) throws IOException {
    PDFontDescriptor fd = font.getFontDescriptor();
    setFlags(fd.getFlags());//from w w  w  .j a v  a 2s.  c  o m
    PDStream ff = fd.getFontFile3();
    if (ff == null) {
        ff = fd.getFontFile2();
        if (ff == null) {
            ff = fd.getFontFile();
        }
    } else {
        setFontType(FontType.TYPE1C);
    }
    if (ff == null) {
        throw new IOException(font.getName() + " no font file");
    }
    return ff;
}

From source file:org.fit.pdfdom.PDFBoxTree.java

License:Open Source License

private void processFontResources(PDResources resources, FontTable table) throws IOException {
    String fontNotSupportedMessage = "Font: {} skipped because type '{}' is not supported.";

    for (COSName key : resources.getFontNames()) {
        PDFont font = resources.getFont(key);
        if (font instanceof PDTrueTypeFont) {
            table.addEntry(font.getName(), font.getFontDescriptor());
            log.debug("Font: " + font.getName() + " TTF");
        } else if (font instanceof PDType0Font) {
            PDCIDFont descendantFont = ((PDType0Font) font).getDescendantFont();
            if (descendantFont instanceof PDCIDFontType2)
                table.addEntry(font.getName(), descendantFont.getFontDescriptor());
            else/*from   w  ww  .j  a  va2s.c  om*/
                log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
        } else if (font instanceof PDType1CFont)
            table.addEntry(font.getName(), font.getFontDescriptor());
        else
            log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
    }

    for (COSName name : resources.getXObjectNames()) {
        PDXObject xobject = resources.getXObject(name);
        if (xobject instanceof PDFormXObject) {
            PDFormXObject xObjectForm = (PDFormXObject) xobject;
            PDResources formResources = xObjectForm.getResources();
            if (formResources != null)
                processFontResources(formResources, table);
        }
    }

}