Search in sources :

Example 1 with Operator

use of org.apache.pdfbox.contentstream.operator.Operator in project pdfbox by apache.

the class ContentStreamWriter method writeObject.

private void writeObject(Object o) throws IOException {
    if (o instanceof COSString) {
        COSWriter.writeString((COSString) o, output);
        output.write(SPACE);
    } else if (o instanceof COSFloat) {
        ((COSFloat) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSInteger) {
        ((COSInteger) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSBoolean) {
        ((COSBoolean) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSName) {
        ((COSName) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSArray) {
        COSArray array = (COSArray) o;
        output.write(COSWriter.ARRAY_OPEN);
        for (int i = 0; i < array.size(); i++) {
            writeObject(array.get(i));
            output.write(SPACE);
        }
        output.write(COSWriter.ARRAY_CLOSE);
    } else if (o instanceof COSDictionary) {
        COSDictionary obj = (COSDictionary) o;
        output.write(COSWriter.DICT_OPEN);
        for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
            if (entry.getValue() != null) {
                writeObject(entry.getKey());
                output.write(SPACE);
                writeObject(entry.getValue());
                output.write(SPACE);
            }
        }
        output.write(COSWriter.DICT_CLOSE);
        output.write(SPACE);
    } else if (o instanceof Operator) {
        Operator op = (Operator) o;
        if (op.getName().equals("BI")) {
            output.write("BI".getBytes(Charsets.ISO_8859_1));
            COSDictionary dic = op.getImageParameters();
            for (COSName key : dic.keySet()) {
                Object value = dic.getDictionaryObject(key);
                key.writePDF(output);
                output.write(SPACE);
                writeObject(value);
                output.write(EOL);
            }
            output.write("ID".getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
            output.write(op.getImageData());
            output.write(EOL);
            output.write("EI".getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
        } else {
            output.write(op.getName().getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
        }
    } else {
        throw new IOException("Error:Unknown type in content stream:" + o);
    }
}
Also used : Operator(org.apache.pdfbox.contentstream.operator.Operator) COSDictionary(org.apache.pdfbox.cos.COSDictionary) IOException(java.io.IOException) COSBoolean(org.apache.pdfbox.cos.COSBoolean) COSInteger(org.apache.pdfbox.cos.COSInteger) COSName(org.apache.pdfbox.cos.COSName) COSArray(org.apache.pdfbox.cos.COSArray) COSBase(org.apache.pdfbox.cos.COSBase) COSString(org.apache.pdfbox.cos.COSString) Map(java.util.Map) COSFloat(org.apache.pdfbox.cos.COSFloat)

Example 2 with Operator

use of org.apache.pdfbox.contentstream.operator.Operator in project pdfbox by apache.

the class PDType3CharProc method getWidth.

/**
 * Get the width from a type3 charproc stream.
 *
 * @return the glyph width.
 * @throws IOException if the stream could not be read, or did not have d0 or d1 as first
 * operator, or if their first argument was not a number.
 */
public float getWidth() throws IOException {
    List<COSBase> arguments = new ArrayList<>();
    PDFStreamParser parser = new PDFStreamParser(this);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            return parseWidth((Operator) token, arguments);
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
    throw new IOException("Unexpected end of stream");
}
Also used : Operator(org.apache.pdfbox.contentstream.operator.Operator) PDFStreamParser(org.apache.pdfbox.pdfparser.PDFStreamParser) COSObject(org.apache.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(org.apache.pdfbox.cos.COSBase) COSObject(org.apache.pdfbox.cos.COSObject) IOException(java.io.IOException)

Example 3 with Operator

use of org.apache.pdfbox.contentstream.operator.Operator in project pdfbox by apache.

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<>();
    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<>();
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
}
Also used : Operator(org.apache.pdfbox.contentstream.operator.Operator) PDFStreamParser(org.apache.pdfbox.pdfparser.PDFStreamParser) COSObject(org.apache.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(org.apache.pdfbox.cos.COSBase) COSObject(org.apache.pdfbox.cos.COSObject)

Example 4 with Operator

use of org.apache.pdfbox.contentstream.operator.Operator in project pdfbox by apache.

the class AppearanceGenerationTest method rectangleFullStrokeNoFill.

// Test currently disabled as the content stream differs
@Test
public void rectangleFullStrokeNoFill() throws IOException {
    PDPage page = document.getPage(0);
    PDAnnotation annotation = page.getAnnotations().get(0);
    // get the tokens of the content stream generated by Adobe
    PDAppearanceStream appearanceContentStream = annotation.getNormalAppearanceStream();
    PDFStreamParser streamParser = new PDFStreamParser(appearanceContentStream);
    streamParser.parse();
    List<Object> tokensForOriginal = streamParser.getTokens();
    // get the tokens for the content stream generated by PDFBox
    annotation.getCOSObject().removeItem(COSName.AP);
    annotation.constructAppearances();
    appearanceContentStream = annotation.getNormalAppearanceStream();
    streamParser = new PDFStreamParser(appearanceContentStream);
    streamParser.parse();
    List<Object> tokensForPdfbox = streamParser.getTokens();
    assertEquals("The number of tokens in the content stream should be the same", tokensForOriginal.size(), tokensForPdfbox.size());
    int actualToken = 0;
    for (Object tokenForOriginal : tokensForOriginal) {
        Object tokenForPdfbox = tokensForPdfbox.get(actualToken);
        assertEquals("The tokens should have the same type", tokenForOriginal.getClass().getName(), tokenForPdfbox.getClass().getName());
        if (tokenForOriginal instanceof Operator) {
            assertEquals("The operator generated by PDFBox should be the same Operator", ((Operator) tokenForOriginal).getName(), ((Operator) tokenForPdfbox).getName());
        } else if (tokenForOriginal instanceof COSFloat) {
            assertTrue("The difference between the numbers should be smaller than " + DELTA, (Math.abs(((COSFloat) tokenForOriginal).floatValue() - ((COSFloat) tokenForPdfbox).floatValue()) < DELTA));
        }
        actualToken++;
    }
    // Save the file for manual comparison for now
    File file = new File(OUT_DIR, NAME_OF_PDF + "-newAP.pdf");
    document.save(file);
}
Also used : Operator(org.apache.pdfbox.contentstream.operator.Operator) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDFStreamParser(org.apache.pdfbox.pdfparser.PDFStreamParser) File(java.io.File) ScratchFile(org.apache.pdfbox.io.ScratchFile) COSFloat(org.apache.pdfbox.cos.COSFloat) Test(org.junit.Test)

Example 5 with Operator

use of org.apache.pdfbox.contentstream.operator.Operator in project pdfbox by apache.

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(org.apache.pdfbox.contentstream.operator.Operator)

Aggregations

Operator (org.apache.pdfbox.contentstream.operator.Operator)9 COSBase (org.apache.pdfbox.cos.COSBase)6 PDFStreamParser (org.apache.pdfbox.pdfparser.PDFStreamParser)6 ArrayList (java.util.ArrayList)5 COSObject (org.apache.pdfbox.cos.COSObject)5 IOException (java.io.IOException)3 COSDictionary (org.apache.pdfbox.cos.COSDictionary)2 COSFloat (org.apache.pdfbox.cos.COSFloat)2 COSName (org.apache.pdfbox.cos.COSName)2 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 Map (java.util.Map)1 COSArray (org.apache.pdfbox.cos.COSArray)1 COSBoolean (org.apache.pdfbox.cos.COSBoolean)1 COSInteger (org.apache.pdfbox.cos.COSInteger)1 COSNumber (org.apache.pdfbox.cos.COSNumber)1 COSString (org.apache.pdfbox.cos.COSString)1 ScratchFile (org.apache.pdfbox.io.ScratchFile)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1