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:org.mabb.fontverter.pdf.PdfFontExtractor.java

License:Open Source License

public static FVFont convertFont(PDFont font, FontFormat format) throws IOException {
    FVFont readFont = null;//from w w w  . ja  va  2  s.c om

    if (font instanceof PDTrueTypeFont) {
        byte[] data = font.getFontDescriptor().getFontFile2().toByteArray();
        readFont = FontVerter.readFont(data);

    } else if (font instanceof PDType0Font) {
        readFont = convertType0FontToOpenType((PDType0Font) font);
        readFont.normalize();

    } else if (font instanceof PDType1CFont) {
        byte[] data = font.getFontDescriptor().getFontFile3().toByteArray();
        readFont = FontVerter.readFont(data);

    } else
        log.warn("Skipped font: '{}'. FontVerter does not support font type: '{}'", font.getName(),
                font.getType());

    if (readFont == null)
        return null;

    if (!readFont.isValid())
        readFont.normalize();

    return FontVerter.convertFont(readFont, format);
}

From source file:org.nuxeo.pdf.PDFPageNumbering.java

License:Open Source License

/**
 * Add page numbers and returns a <i>new</i> Blob. Original blob is not
 * modified. This code assumes:/* w  w  w.  j a  v  a 2 s.co  m*/
 * <ul>
 * <li>There is no page numbers already (it always draw the numbers)</li>
 * <li>The pdf is not rotated</li>
 * <li>Default values apply:
 * <ul>
 * <li><code>inStartAtPage</code> and <code>inStartAtNumber</code> are set
 * to 1 if they are passed < 1.</li>
 * <li>If <code>inStartAtPage</code> is > number of pages it also is reset
 * to 1</li>
 * <li><code>inFontName</code> is set to "Helvetica" if "" or null</li>
 * <li><code>inFontSize</code> is <= 0, it is set to 16</li>
 * <li><code>inHex255Color</code> is set to black if "", null or if its
 * length < 6. Expected format is 0xrrggbb, #rrggbb or just rrggbb</li>
 * <li><code>inPosition</code> is set to <code>BOTTOM_RIGHT</code> if null</li>
 * </ul>
 * </li>
 * <li></li>
 * </ul>
 *
 * @param inBlob
 * @param inStartAtPage
 * @param inStartAtNumber
 * @param inFontName
 * @param inFontSize
 * @param inHex255Color
 * @param inPosition
 * @return Blob
 * @throws IOException
 * @throws COSVisitorException
 *
 * @since 5.9.5
 */
public Blob addPageNumbers(int inStartAtPage, int inStartAtNumber, String inFontName, float inFontSize,
        String inHex255Color, PAGE_NUMBER_POSITION inPosition) throws IOException, COSVisitorException {

    Blob result = null;
    PDDocument doc = null;

    inStartAtPage = inStartAtPage < 1 ? 1 : inStartAtPage;
    int pageNumber = inStartAtNumber < 1 ? 1 : inStartAtNumber;
    inFontSize = inFontSize <= 0 ? DEFAULT_FONT_SIZE : inFontSize;

    int[] rgb = PDFUtils.hex255ToRGB(inHex255Color);

    try {
        doc = PDDocument.load(blob.getStream());
        List<?> allPages;
        PDFont font;
        int max;

        if (inFontName == null || inFontName.isEmpty()) {
            font = PDType1Font.HELVETICA;
        } else {
            font = PDType1Font.getStandardFont(inFontName);
            if (font == null) {
                font = new PDType1Font(inFontName);
            }
        }

        allPages = doc.getDocumentCatalog().getAllPages();
        max = allPages.size();
        inStartAtPage = inStartAtPage > max ? 1 : inStartAtPage;
        for (int i = inStartAtPage; i <= max; i++) {
            String pageNumAsStr = "" + pageNumber;
            pageNumber += 1;

            PDPage page = (PDPage) allPages.get(i - 1);
            PDPageContentStream footercontentStream = new PDPageContentStream(doc, page, true, true);

            float stringWidth = font.getStringWidth(pageNumAsStr) * inFontSize / 1000f;
            float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * inFontSize / 1000;
            PDRectangle pageRect = page.findMediaBox();

            float xMoveAmount, yMoveAmount;

            if (inPosition == null) {
                inPosition = PAGE_NUMBER_POSITION.BOTTOM_RIGHT;
            }
            switch (inPosition) {
            case BOTTOM_LEFT:
                xMoveAmount = 10;
                yMoveAmount = pageRect.getLowerLeftY() + 10;
                break;

            case BOTTOM_CENTER:
                xMoveAmount = (pageRect.getUpperRightX() / 2) - (stringWidth / 2);
                yMoveAmount = pageRect.getLowerLeftY() + 10;
                break;

            case TOP_LEFT:
                xMoveAmount = 10;
                yMoveAmount = pageRect.getHeight() - stringHeight - 10;
                break;

            case TOP_CENTER:
                xMoveAmount = (pageRect.getUpperRightX() / 2) - (stringWidth / 2);
                yMoveAmount = pageRect.getHeight() - stringHeight - 10;
                break;

            case TOP_RIGHT:
                xMoveAmount = pageRect.getUpperRightX() - 10 - stringWidth;
                yMoveAmount = pageRect.getHeight() - stringHeight - 10;
                break;

            // Bottom-right is the default
            default:
                xMoveAmount = pageRect.getUpperRightX() - 10 - stringWidth;
                yMoveAmount = pageRect.getLowerLeftY() + 10;
                break;
            }

            footercontentStream.beginText();
            footercontentStream.setFont(font, inFontSize);
            footercontentStream.moveTextPositionByAmount(xMoveAmount, yMoveAmount);
            footercontentStream.setNonStrokingColor(rgb[0], rgb[1], rgb[2]);
            footercontentStream.drawString(pageNumAsStr);
            footercontentStream.endText();
            footercontentStream.close();
        }

        File tempFile = File.createTempFile("pdfutils-", ".pdf");
        doc.save(tempFile);
        result = new FileBlob(tempFile);
        Framework.trackFile(tempFile, result);

    } finally {
        if (doc != null) {
            doc.close();
        }
    }

    return result;
}

From source file:org.wangwei.pdf.hexpdf.HexPDF2.java

License:Apache License

/**
 * Set font type to use.//from   w w  w .  ja v a2 s  .c o 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(HexPDF2.class.getName()).log(Level.SEVERE, null, ex);
    }
    lineSep = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
}

From source file:org.xmlcml.pdf2svg.AMIFont.java

License:Apache License

public static PDFontDescriptor getDescendantFontDescriptor(PDFont pdFont) {
    PDFontDescriptor fd = null;// w  w w.  ja  v a  2  s  . c  om
    PDFont descendantFont = getFirstDescendantFont(pdFont);
    fd = (descendantFont == null) ? null : descendantFont.getFontDescriptor();
    LOG.trace("fd (" + fd.getFontName() + ") " + fd);
    return fd;
}

From source file:org.xmlcml.pdf2svg.AMIFont.java

License:Apache License

public static PDFontDescriptor getFontDescriptorOrDescendantFontDescriptor(PDFont pdFont) {
    PDFontDescriptor fd = pdFont.getFontDescriptor();
    //      getToUnicode(pdFont);
    if (fd == null && pdFont instanceof PDType0Font) {
        fd = AMIFont.getDescendantFontDescriptor(pdFont);
    }/* w  ww  .  java2 s .  c  o m*/
    return fd;
}

From source file:uia.pdf.PDFUtil.java

License:Apache License

/**
 * Get height./*w  w  w  .j  a v  a  2s .  c  om*/
 * @param content
 * @param font
 * @param fontSize
 * @return
 */
public static int getContentHeight(String content, PDFont font, int fontSize) {
    return content == null ? 0 : (int) (font.getFontDescriptor().getAscent() / 1000 * fontSize);
}

From source file:ve.zoonosis.utils.PDFCreator.java

License:Apache License

public void addCenterText(PDFont font, int fontSize, String text) throws IOException {
    if (contentStream == null) {
        addPage();//  w w  w.  ja v  a 2s  . c o m
    }
    float titleWidth = font.getStringWidth(text) / 1000 * fontSize;
    float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
    contentStream.setFont(font, fontSize);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount((pageActual.getMediaBox().getWidth() - titleWidth) / 2,
            750 - (20 * linePos++));
    contentStream.drawString(text);
    contentStream.endText();
}