Example usage for org.apache.pdfbox.pdmodel.font PDType0Font load

List of usage examples for org.apache.pdfbox.pdmodel.font PDType0Font load

Introduction

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

Prototype

public static PDType0Font load(PDDocument doc, TrueTypeFont ttf, boolean embedSubset) throws IOException 

Source Link

Document

Loads a TTF to be embedded into a document as a Type 0 font.

Usage

From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer.java

License:Apache License

/**
 * Try to map the java.awt.Font to a PDFont.
 * //from  ww w . ja v  a 2  s . c o m
 * @param font
 *            the java.awt.Font for which a mapping should be found
 * @param env
 *            environment of the font mapper
 * @return the PDFont or null if none can be found.
 * @throws IOException
 *             when the font can not be loaded
 * @throws FontFormatException
 *             when the font file can not be loaded
 */
@SuppressWarnings("WeakerAccess")
protected PDFont mapFont(final Font font, final IFontTextDrawerEnv env)
        throws IOException, FontFormatException {
    /*
     * If we have any font registering's, we must perform them now
     */
    for (final FontEntry fontEntry : fontFiles) {
        if (fontEntry.overrideName == null) {
            Font javaFont = Font.createFont(Font.TRUETYPE_FONT, fontEntry.file);
            fontEntry.overrideName = javaFont.getFontName();
        }
        if (fontEntry.file.getName().toLowerCase(Locale.US).endsWith(".ttc")) {
            TrueTypeCollection collection = new TrueTypeCollection(fontEntry.file);
            collection.processAllFonts(new TrueTypeCollection.TrueTypeFontProcessor() {
                @Override
                public void process(TrueTypeFont ttf) throws IOException {
                    PDFont pdFont = PDType0Font.load(env.getDocument(), ttf, true);
                    fontMap.put(fontEntry.overrideName, pdFont);
                    fontMap.put(pdFont.getName(), pdFont);
                }
            });
        } else {
            /*
             * We load the font using the file.
             */
            PDFont pdFont = PDType0Font.load(env.getDocument(), fontEntry.file);
            fontMap.put(fontEntry.overrideName, pdFont);
        }
    }
    fontFiles.clear();

    return fontMap.get(font.getFontName());
}

From source file:jgnash.report.pdf.Report.java

License:Open Source License

private PDFont loadFont(final String name, final PDDocument document) {

    final String path = FontRegistry.getRegisteredFontPath(name);

    if (path != null && !path.isEmpty()) {
        try {//  w w w .j a v  a 2s .c  om
            if (path.toLowerCase(Locale.ROOT).endsWith(".ttf") || path.toLowerCase(Locale.ROOT).endsWith(".otf")
                    || path.toLowerCase(Locale.ROOT).indexOf(".ttc,") > 0) {
                return PDType0Font.load(document, new FileInputStream(path), false);
            } else if (path.toLowerCase(Locale.ROOT).endsWith(".afm")
                    || path.toLowerCase(Locale.ROOT).endsWith(".pfm")) {
                return new PDType1Font(document, new FileInputStream(path));
            }
        } catch (final Exception ignored) {
        }
    }

    return PDType1Font.COURIER;
}

From source file:jp.qpg.PDFPrinter.java

License:Apache License

/**
 * @param path .ttf file path//from  w  w w  .j av  a  2s .  c om
 * @return this
 */
public PDFPrinter setFont(String path) {
    Objects.requireNonNull(path);
    setFont(Try.to(() -> {
        ttf = Optional.of(new TTFParser().parse(new File(path)));
        return PDType0Font.load(getDocument(), ttf.get(), true);
    }).get());
    return this;
}

From source file:jp.qpg.PDFPrinter.java

License:Apache License

/**
 * @param path .ttc file path//from   w  w  w .  j a va  2  s .  c  om
 * @param name font name
 * @return this
 */
public PDFPrinter setFont(String path, String name) {
    Objects.requireNonNull(path);
    Objects.requireNonNull(name);
    setFont(Try.to(() -> {
        ttc = Optional.of(new TrueTypeCollection(new File(path)));
        ttf = Optional.of(ttc.get().getFontByName(name));
        return PDType0Font.load(getDocument(), ttf.get(), true);
    }).get());
    return this;
}