Search in sources :

Example 66 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDFreeTextAppearanceHandler method extractFontDetails.

// TODO extractNonStrokingColor and extractFontDetails
// might somehow be replaced with PDDefaultAppearanceString, which is quite similar.
private void extractFontDetails(PDAnnotationMarkup annotation) {
    String defaultAppearance = annotation.getDefaultAppearance();
    if (defaultAppearance == null && document != null && document.getDocumentCatalog().getAcroForm() != null) {
        defaultAppearance = document.getDocumentCatalog().getAcroForm().getDefaultAppearance();
    }
    if (defaultAppearance == null) {
        return;
    }
    try {
        // not sure if charset is correct, but we only need numbers and simple characters
        PDFStreamParser parser = new PDFStreamParser(defaultAppearance.getBytes(Charsets.US_ASCII));
        COSArray arguments = new COSArray();
        COSArray fontArguments = new COSArray();
        for (Object token = parser.parseNextToken(); token != null; token = parser.parseNextToken()) {
            if (token instanceof COSObject) {
                arguments.add(((COSObject) token).getObject());
            } else if (token instanceof Operator) {
                Operator op = (Operator) token;
                String name = op.getName();
                if (OperatorName.SET_FONT_AND_SIZE.equals(name)) {
                    fontArguments = arguments;
                }
                arguments = new COSArray();
            } else {
                arguments.add((COSBase) token);
            }
        }
        if (fontArguments.size() >= 2) {
            COSBase base = fontArguments.get(0);
            if (base instanceof COSName) {
                fontName = (COSName) base;
            }
            base = fontArguments.get(1);
            if (base instanceof COSNumber) {
                fontSize = ((COSNumber) base).floatValue();
            }
        }
    } catch (IOException ex) {
        Log.w("PdfBox-Android", "Problem parsing /DA, will use default 'Helv 10'", ex);
    }
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSName(com.tom_roush.pdfbox.cos.COSName) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) IOException(java.io.IOException)

Example 67 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDAnnotation method getNormalAppearanceStream.

/**
 * Returns the appearance stream for this annotation, if any. The annotation state is taken into account, if
 * present.
 */
public PDAppearanceStream getNormalAppearanceStream() {
    PDAppearanceDictionary appearanceDict = getAppearance();
    if (appearanceDict == null) {
        return null;
    }
    PDAppearanceEntry normalAppearance = appearanceDict.getNormalAppearance();
    if (normalAppearance == null) {
        return null;
    }
    if (normalAppearance.isSubDictionary()) {
        COSName state = getAppearanceState();
        return normalAppearance.getSubDictionary().get(state);
    } else {
        return normalAppearance.getAppearanceStream();
    }
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName)

Example 68 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDAppearanceEntry method getSubDictionary.

/**
 * Returns the entry as an appearance subdictionary.
 *
 * @throws IllegalStateException if this entry is not an appearance subdictionary
 */
public Map<COSName, PDAppearanceStream> getSubDictionary() {
    if (!isSubDictionary()) {
        throw new IllegalStateException("This entry is not an appearance subdictionary");
    }
    COSDictionary dict = (COSDictionary) entry;
    Map<COSName, PDAppearanceStream> map = new HashMap<COSName, PDAppearanceStream>();
    for (COSName name : dict.keySet()) {
        COSBase value = dict.getDictionaryObject(name);
        // the file from PDFBOX-1599 contains /null as its entry, so we skip non-stream entries
        if (value instanceof COSStream) {
            map.put(name, new PDAppearanceStream((COSStream) value));
        }
    }
    return new COSDictionaryMap<COSName, PDAppearanceStream>(map, dict);
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) HashMap(java.util.HashMap) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSDictionaryMap(com.tom_roush.pdfbox.pdmodel.common.COSDictionaryMap)

Example 69 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class DrawObject method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    if (arguments.isEmpty()) {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName)) {
        return;
    }
    COSName name = (COSName) base0;
    if (context.getResources().isImageXObject(name)) {
        // we're done here, don't decode images when doing text extraction
        return;
    }
    PDXObject xobject = context.getResources().getXObject(name);
    if (xobject instanceof PDFormXObject) {
        try {
            context.increaseLevel();
            if (context.getLevel() > 25) {
                Log.e("PdfBox-Android", "recursion is too deep, skipping form XObject");
                return;
            }
            PDFormXObject form = (PDFormXObject) xobject;
            if (form instanceof PDTransparencyGroup) {
                context.showTransparencyGroup((PDTransparencyGroup) form);
            } else {
                context.showForm(form);
            }
        } finally {
            context.decreaseLevel();
        }
    }
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSName(com.tom_roush.pdfbox.cos.COSName) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 70 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class DrawObject method process.

@Override
public void process(Operator operator, List<COSBase> operands) throws IOException {
    if (operands.isEmpty()) {
        throw new MissingOperandException(operator, operands);
    }
    COSBase base0 = operands.get(0);
    if (!(base0 instanceof COSName)) {
        return;
    }
    COSName objectName = (COSName) base0;
    PDXObject xobject = context.getResources().getXObject(objectName);
    if (xobject == null) {
        throw new MissingResourceException("Missing XObject: " + objectName.getName());
    } else if (xobject instanceof PDImageXObject) {
        PDImageXObject image = (PDImageXObject) xobject;
        context.drawImage(image);
    } else if (xobject instanceof PDFormXObject) {
        try {
            context.increaseLevel();
            if (context.getLevel() > 25) {
                Log.e("PdfBox-Android", "recursion is too deep, skipping form XObject");
                return;
            }
            PDFormXObject form = (PDFormXObject) xobject;
            if (form instanceof PDTransparencyGroup) {
                context.showTransparencyGroup((PDTransparencyGroup) form);
            } else {
                context.showForm(form);
            }
        } finally {
            context.decreaseLevel();
        }
    }
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) COSName(com.tom_roush.pdfbox.cos.COSName) MissingOperandException(com.tom_roush.pdfbox.contentstream.operator.MissingOperandException) MissingResourceException(com.tom_roush.pdfbox.pdmodel.MissingResourceException) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Aggregations

COSName (com.tom_roush.pdfbox.cos.COSName)83 COSBase (com.tom_roush.pdfbox.cos.COSBase)50 COSArray (com.tom_roush.pdfbox.cos.COSArray)27 IOException (java.io.IOException)24 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)11 COSString (com.tom_roush.pdfbox.cos.COSString)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)9 COSStream (com.tom_roush.pdfbox.cos.COSStream)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)5 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)5 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)5 PDXObject (com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)4 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)4 OutputStream (java.io.OutputStream)4 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)3