Example usage for org.apache.pdfbox.pdmodel.font PDFontFactory createFont

List of usage examples for org.apache.pdfbox.pdmodel.font PDFontFactory createFont

Introduction

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

Prototype

public static PDFont createFont(COSDictionary dictionary) throws IOException 

Source Link

Document

Creates a new PDFont instance with the appropriate subclass.

Usage

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox.placeholder.SignaturePlaceholderExtractor.java

License:EUPL

@Override
public Map<String, PDFont> getFonts() {
    if (fonts == null) {
        // at least an empty map will be returned
        // TODO we should return null instead of an empty map
        fonts = new HashMap<String, PDFont>();
        if (this.getResources() != null && this.getResources().getCOSDictionary() != null) {
            COSDictionary fontsDictionary = (COSDictionary) this.getResources().getCOSDictionary()
                    .getDictionaryObject(COSName.FONT);
            if (fontsDictionary == null) {
                // ignore we do not want to set anything, never when creating a signature!!!!!
                //fontsDictionary = new COSDictionary();
                //this.getResources().getCOSDictionary().setItem(COSName.FONT, fontsDictionary);
            } else {
                for (COSName fontName : fontsDictionary.keySet()) {
                    COSBase font = fontsDictionary.getDictionaryObject(fontName);
                    // data-000174.pdf contains a font that is a COSArray, looks to be an error in the
                    // PDF, we will just ignore entries that are not dictionaries.
                    if (font instanceof COSDictionary) {
                        PDFont newFont = null;
                        try {
                            newFont = PDFontFactory.createFont((COSDictionary) font);
                        } catch (IOException exception) {
                            logger.error("error while creating a font", exception);
                        }/*from   w  w  w .ja v  a2s  .  c om*/
                        if (newFont != null) {
                            fonts.put(fontName.getName(), newFont);
                        }
                    }
                }
            }
        }
    }
    return fonts;
}

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignaturePlaceholderExtractor.java

License:EUPL

public Map<String, PDFont> getFonts() {
    if (fonts == null) {
        // at least an empty map will be returned
        // TODO we should return null instead of an empty map
        fonts = new HashMap<String, PDFont>();
        if (this.getResources() != null && this.getResources().getCOSObject() != null) {
            COSDictionary fontsDictionary = (COSDictionary) this.getResources().getCOSObject()
                    .getDictionaryObject(COSName.FONT);
            if (fontsDictionary == null) {
                // ignore we do not want to set anything, never when creating a signature!!!!!
                //fontsDictionary = new COSDictionary();
                //this.getResources().getCOSDictionary().setItem(COSName.FONT, fontsDictionary);
            } else {
                for (COSName fontName : fontsDictionary.keySet()) {
                    COSBase font = fontsDictionary.getDictionaryObject(fontName);
                    // data-000174.pdf contains a font that is a COSArray, looks to be an error in the
                    // PDF, we will just ignore entries that are not dictionaries.
                    if (font instanceof COSDictionary) {
                        PDFont newFont = null;
                        try {
                            newFont = PDFontFactory.createFont((COSDictionary) font);
                        } catch (IOException exception) {
                            logger.error("error while creating a font", exception);
                        }//from   ww  w.  j ava  2 s  .c om
                        if (newFont != null) {
                            fonts.put(fontName.getName(), newFont);
                        }
                    }
                }
            }
        }
    }
    return fonts;
}

From source file:net.padaf.preflight.font.AbstractFontValidator.java

License:Apache License

/**
 * Abstract Constructor/*from ww w.  java2s. co  m*/
 * @param handler the handled document
 * @param cObj The cos object representing the font
 * @throws ValidationException when object creation fails
 */
public AbstractFontValidator(DocumentHandler handler, COSObject cObj) throws ValidationException {
    try {
        this.handler = handler;
        this.cObj = cObj;
        this.fDictionary = (COSDictionary) cObj.getObject();
        this.pFont = PDFontFactory.createFont(fDictionary);

        this.fontContainer = instanciateContainer(this.pFont);
        this.handler.addFont(this.pFont.getCOSObject(), this.fontContainer);
    } catch (IOException e) {
        throw new ValidationException("Unable to instantiate a FontValidator object : " + e.getMessage());
    }
}

From source file:net.padaf.preflight.font.Type3FontValidator.java

License:Apache License

/**
 * If the Resources entry is present, this method check its content. Only
 * fonts and Images are checked because this resource describes glyphs. REMARK
 * : The font and the image aren't validated because they will be validated by
 * an other ValidationHelper.//from   w  w  w .ja  v  a 2 s. c o m
 * 
 * @return
 */
private boolean checkResources() throws ValidationException {
    if (this.resources == null) {
        // ---- No resources dictionary.
        return true;
    }

    COSDocument cDoc = this.handler.getDocument().getDocument();
    COSDictionary dictionary = COSUtils.getAsDictionary(this.resources, cDoc);
    if (dictionary == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                "The Resources element isn't a dictionary"));
        return false;
    }

    COSBase cbImg = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_XOBJECT));
    COSBase cbFont = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_FONT));

    if (cbImg == null && cbFont == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                "The Resources element doesn't have Glyph information"));
        return false;
    }

    if (cbImg != null) {
        // ---- the referenced objects must be present in the PDF file
        COSDictionary dicImgs = COSUtils.getAsDictionary(cbImg, cDoc);
        Set<COSName> keyList = dicImgs.keySet();
        for (Object key : keyList) {

            COSBase item = dictionary.getItem((COSName) key);
            COSDictionary xObjImg = COSUtils.getAsDictionary(item, cDoc);
            if (xObjImg == null) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            if (!XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG
                    .equals(xObjImg.getString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE)))) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }
        }
    }

    if (cbFont != null) {
        // ---- the referenced object must be present in the PDF file
        COSDictionary dicFonts = COSUtils.getAsDictionary(cbFont, cDoc);
        Set<COSName> keyList = dicFonts.keySet();
        for (Object key : keyList) {

            COSBase item = dictionary.getItem((COSName) key);
            COSDictionary xObjFont = COSUtils.getAsDictionary(item, cDoc);
            if (xObjFont == null) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            if (!FONT_DICTIONARY_VALUE_FONT
                    .equals(xObjFont.getString(COSName.getPDFName(DICTIONARY_KEY_TYPE)))) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            try {
                PDFont aFont = PDFontFactory.createFont(xObjFont);
                // FontContainer aContainer = this.handler.retrieveFontContainer(aFont);
                AbstractFontContainer aContainer = this.handler.getFont(aFont.getCOSObject());
                // ---- another font is used in the Type3, check if the font is valid.
                if (aContainer.isValid() != State.VALID) {
                    this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                            "The Resources dictionary of type 3 font contains invalid font"));
                    return false;
                }
            } catch (IOException e) {
                throw new ValidationException("Unable to valid the Type3 : " + e.getMessage());
            }
        }
    }

    List<ValidationError> errors = new ArrayList<ValidationError>();
    ExtGStateContainer extGStates = new ExtGStateContainer(dictionary, cDoc);
    boolean res = extGStates.validateTransparencyRules(errors);
    for (ValidationError err : errors) {
        this.fontContainer.addError(err);
    }

    return res && validateShadingPattern(dictionary, errors);
}

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

License:Apache License

FontContainer(COSDictionary fontData) throws IOException {
    dict = fontData;
    font = PDFontFactory.createFont(fontData);
}

From source file:org.apache.padaf.preflight.font.Type3FontValidator.java

License:Apache License

/**
 * If the Resources entry is present, this method check its content. Only
 * fonts and Images are checked because this resource describes glyphs. REMARK
 * : The font and the image aren't validated because they will be validated by
 * an other ValidationHelper./*from  w w  w .  ja v a  2s  . c o m*/
 * 
 * @return
 */
private boolean checkResources() throws ValidationException {
    if (this.resources == null) {
        // ---- No resources dictionary.
        return true;
    }

    COSDocument cDoc = this.handler.getDocument().getDocument();
    COSDictionary dictionary = COSUtils.getAsDictionary(this.resources, cDoc);
    if (dictionary == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                "The Resources element isn't a dictionary"));
        return false;
    }

    COSBase cbImg = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_XOBJECT));
    COSBase cbFont = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_FONT));

    if (cbImg != null) {
        // ---- the referenced objects must be present in the PDF file
        COSDictionary dicImgs = COSUtils.getAsDictionary(cbImg, cDoc);
        Set<COSName> keyList = dicImgs.keySet();
        for (Object key : keyList) {

            COSBase item = dictionary.getItem((COSName) key);
            COSDictionary xObjImg = COSUtils.getAsDictionary(item, cDoc);
            if (xObjImg == null) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            if (!XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG
                    .equals(xObjImg.getString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE)))) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }
        }
    }

    if (cbFont != null) {
        // ---- the referenced object must be present in the PDF file
        COSDictionary dicFonts = COSUtils.getAsDictionary(cbFont, cDoc);
        Set<COSName> keyList = dicFonts.keySet();
        for (Object key : keyList) {

            COSBase item = dictionary.getItem((COSName) key);
            COSDictionary xObjFont = COSUtils.getAsDictionary(item, cDoc);
            if (xObjFont == null) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            if (!FONT_DICTIONARY_VALUE_FONT
                    .equals(xObjFont.getString(COSName.getPDFName(DICTIONARY_KEY_TYPE)))) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            try {
                PDFont aFont = PDFontFactory.createFont(xObjFont);
                // FontContainer aContainer = this.handler.retrieveFontContainer(aFont);
                AbstractFontContainer aContainer = this.handler.getFont(aFont.getCOSObject());
                // ---- another font is used in the Type3, check if the font is valid.
                if (aContainer.isValid() != State.VALID) {
                    this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                            "The Resources dictionary of type 3 font contains invalid font"));
                    return false;
                }
            } catch (IOException e) {
                throw new ValidationException("Unable to valid the Type3 : " + e.getMessage());
            }
        }
    }

    List<ValidationError> errors = new ArrayList<ValidationError>();
    ExtGStateContainer extGStates = new ExtGStateContainer(dictionary, cDoc);
    boolean res = extGStates.validateTransparencyRules(errors);
    for (ValidationError err : errors) {
        this.fontContainer.addError(err);
    }

    return res && validateShadingPattern(dictionary, errors);
}

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

License:Apache License

public static PDFont getFirstDescendantFont(PDFont pdFont) {
    COSDictionary dict = (COSDictionary) pdFont.getCOSObject();
    COSArray array = dict == null ? null : (COSArray) dict.getDictionaryObject(COSName.DESCENDANT_FONTS);
    PDFont descendantFont = null;/*from   w  w w  . j a  v  a 2  s  .  c om*/
    try {
        descendantFont = array == null ? null : PDFontFactory.createFont((COSDictionary) array.getObject(0));
    } catch (IOException e) {
        LOG.error("****************** Can't create descendant font! for " + pdFont);
    }
    return descendantFont;
}