Search in sources :

Example 6 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class PDStandardAttributeObject method getNumberOrArrayOfNumber.

/**
 * Gets a number or an array of numbers.
 *
 * @param name the attribute name
 * @param defaultValue the default value
 * @return a Float or an array of floats
 */
protected Object getNumberOrArrayOfNumber(String name, float defaultValue) {
    COSBase v = this.getCOSObject().getDictionaryObject(name);
    if (v instanceof COSArray) {
        COSArray array = (COSArray) v;
        float[] values = new float[array.size()];
        for (int i = 0; i < array.size(); i++) {
            COSBase item = array.getObject(i);
            if (item instanceof COSNumber) {
                values[i] = ((COSNumber) item).floatValue();
            }
        }
        return values;
    }
    if (v instanceof COSNumber) {
        return ((COSNumber) v).floatValue();
    }
    if (Float.compare(defaultValue, UNSPECIFIED) == 0) {
        return null;
    }
    return defaultValue;
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase)

Example 7 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class PDFont method getAverageFontWidth.

/**
 * This will get the average font width for all characters.
 *
 * @return The width is in 1000 unit of text space, ie 333 or 777
 */
// todo: this method is highly suspicious, the average glyph width is not usually a good metric
@Override
public float getAverageFontWidth() {
    float average;
    if (Float.compare(avgFontWidth, 0.0f) != 0) {
        average = avgFontWidth;
    } else {
        float totalWidth = 0.0f;
        float characterCount = 0.0f;
        COSArray widths = (COSArray) dict.getDictionaryObject(COSName.WIDTHS);
        if (widths != null) {
            for (int i = 0; i < widths.size(); i++) {
                COSNumber fontWidth = (COSNumber) widths.getObject(i);
                if (fontWidth.floatValue() > 0) {
                    totalWidth += fontWidth.floatValue();
                    characterCount += 1;
                }
            }
        }
        if (totalWidth > 0) {
            average = totalWidth / characterCount;
        } else {
            average = 0;
        }
        avgFontWidth = average;
    }
    return average;
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSNumber(org.apache.pdfbox.cos.COSNumber)

Example 8 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class FDFField method getSetWidgetFieldFlags.

/**
 * This will get the SetF entry of the cos dictionary. If it it not present then this method will return null.
 *
 * @return The field flags.
 */
public Integer getSetWidgetFieldFlags() {
    Integer retval = null;
    COSNumber ff = (COSNumber) field.getDictionaryObject(COSName.SET_F);
    if (ff != null) {
        retval = ff.intValue();
    }
    return retval;
}
Also used : COSInteger(org.apache.pdfbox.cos.COSInteger) COSNumber(org.apache.pdfbox.cos.COSNumber)

Example 9 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class FDFField method getWidgetFieldFlags.

/**
 * This will get the F entry of the cos dictionary. If it it not present then this method will return null.
 *
 * @return The widget field flags.
 */
public Integer getWidgetFieldFlags() {
    Integer retval = null;
    COSNumber f = (COSNumber) field.getDictionaryObject("F");
    if (f != null) {
        retval = f.intValue();
    }
    return retval;
}
Also used : COSInteger(org.apache.pdfbox.cos.COSInteger) COSNumber(org.apache.pdfbox.cos.COSNumber)

Example 10 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class DictionaryEncoding method applyDifferences.

private void applyDifferences() {
    // now replace with the differences
    COSBase base = encoding.getDictionaryObject(COSName.DIFFERENCES);
    if (!(base instanceof COSArray)) {
        return;
    }
    COSArray diffArray = (COSArray) base;
    int currentIndex = -1;
    for (int i = 0; i < diffArray.size(); i++) {
        COSBase next = diffArray.getObject(i);
        if (next instanceof COSNumber) {
            currentIndex = ((COSNumber) next).intValue();
        } else if (next instanceof COSName) {
            COSName name = (COSName) next;
            overwrite(currentIndex, name.getName());
            this.differences.put(currentIndex, name.getName());
            currentIndex++;
        }
    }
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSName(org.apache.pdfbox.cos.COSName) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase)

Aggregations

COSNumber (org.apache.pdfbox.cos.COSNumber)61 COSBase (org.apache.pdfbox.cos.COSBase)29 MissingOperandException (org.apache.pdfbox.contentstream.operator.MissingOperandException)18 COSArray (org.apache.pdfbox.cos.COSArray)18 COSInteger (org.apache.pdfbox.cos.COSInteger)10 Point2D (java.awt.geom.Point2D)6 IOException (java.io.IOException)6 COSObject (org.apache.pdfbox.cos.COSObject)5 PDBorderStyleDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)5 ArrayList (java.util.ArrayList)4 COSDictionary (org.apache.pdfbox.cos.COSDictionary)4 COSName (org.apache.pdfbox.cos.COSName)4 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)4 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)4 COSFloat (org.apache.pdfbox.cos.COSFloat)3 COSStream (org.apache.pdfbox.cos.COSStream)2 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)2 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)2 Matrix (org.apache.pdfbox.util.Matrix)2 Paint (java.awt.Paint)1