Search in sources :

Example 6 with Operator

use of com.tom_roush.pdfbox.contentstream.operator.Operator in project PdfBox-Android by TomRoush.

the class PDDefaultAppearanceString method processAppearanceStringOperators.

/**
 * Processes the operators of the given content stream.
 *
 * @param content the content to parse.
 * @throws IOException if there is an error reading or parsing the content stream.
 */
private void processAppearanceStringOperators(byte[] content) throws IOException {
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(content);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            processOperator((Operator) token, arguments);
            arguments = new ArrayList<COSBase>();
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSObject(com.tom_roush.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject)

Example 7 with Operator

use of com.tom_roush.pdfbox.contentstream.operator.Operator 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 8 with Operator

use of com.tom_roush.pdfbox.contentstream.operator.Operator in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method processOperator.

/**
 * This is used to handle an operation.
 *
 * @param operation The operation to perform.
 * @param arguments The list of arguments.
 * @throws IOException If there is an error processing the operation.
 */
public void processOperator(String operation, List<COSBase> arguments) throws IOException {
    Operator operator = Operator.getOperator(operation);
    processOperator(operator, arguments);
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator)

Example 9 with Operator

use of com.tom_roush.pdfbox.contentstream.operator.Operator in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method processStreamOperators.

/**
 * Processes the operators of the given content stream.
 *
 * @param contentStream to content stream to parse.
 * @throws IOException if there is an error reading or parsing the content stream.
 */
private void processStreamOperators(PDContentStream contentStream) throws IOException {
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(contentStream);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            processOperator((Operator) token, arguments);
            arguments = new ArrayList<COSBase>();
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSObject(com.tom_roush.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) COSObject(com.tom_roush.pdfbox.cos.COSObject)

Aggregations

Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)9 COSBase (com.tom_roush.pdfbox.cos.COSBase)8 COSObject (com.tom_roush.pdfbox.cos.COSObject)7 PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 COSArray (com.tom_roush.pdfbox.cos.COSArray)3 COSName (com.tom_roush.pdfbox.cos.COSName)3 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)2 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)2 COSBoolean (com.tom_roush.pdfbox.cos.COSBoolean)1 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)1 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)1 COSString (com.tom_roush.pdfbox.cos.COSString)1 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)1 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)1 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Map (java.util.Map)1