Search in sources :

Example 1 with COSString

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

the class BaseParser method parseCOSString.

/**
 * This will parse a PDF string.
 *
 * @return The parsed PDF string.
 *
 * @throws IOException If there is an error reading from the stream.
 */
protected COSString parseCOSString() throws IOException {
    char nextChar = (char) seqSource.read();
    if (nextChar == '<') {
        return parseCOSHexString();
    } else if (nextChar != '(') {
        throw new IOException("parseCOSString string should start with '(' or '<' and not '" + nextChar + "' at offset " + seqSource.getPosition());
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // This is the number of braces read
    int braces = 1;
    int c = seqSource.read();
    while (braces > 0 && c != -1) {
        char ch = (char) c;
        // not yet read
        int nextc = -2;
        if (ch == ')') {
            braces--;
            braces = checkForEndOfString(braces);
            if (braces != 0) {
                out.write(ch);
            }
        } else if (ch == '(') {
            braces++;
            out.write(ch);
        } else if (ch == '\\') {
            // patched by ram
            char next = (char) seqSource.read();
            switch(next) {
                case 'n':
                    out.write('\n');
                    break;
                case 'r':
                    out.write('\r');
                    break;
                case 't':
                    out.write('\t');
                    break;
                case 'b':
                    out.write('\b');
                    break;
                case 'f':
                    out.write('\f');
                    break;
                case ')':
                    // PDFBox 276 /Title (c:\)
                    braces = checkForEndOfString(braces);
                    if (braces != 0) {
                        out.write(next);
                    } else {
                        out.write('\\');
                    }
                    break;
                case '(':
                case '\\':
                    out.write(next);
                    break;
                case ASCII_LF:
                case ASCII_CR:
                    // this is a break in the line so ignore it and the newline and continue
                    c = seqSource.read();
                    while (isEOL(c) && c != -1) {
                        c = seqSource.read();
                    }
                    nextc = c;
                    break;
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                    {
                        StringBuilder octal = new StringBuilder();
                        octal.append(next);
                        c = seqSource.read();
                        char digit = (char) c;
                        if (digit >= '0' && digit <= '7') {
                            octal.append(digit);
                            c = seqSource.read();
                            digit = (char) c;
                            if (digit >= '0' && digit <= '7') {
                                octal.append(digit);
                            } else {
                                nextc = c;
                            }
                        } else {
                            nextc = c;
                        }
                        int character = 0;
                        try {
                            character = Integer.parseInt(octal.toString(), 8);
                        } catch (NumberFormatException e) {
                            throw new IOException("Error: Expected octal character, actual='" + octal + "'", e);
                        }
                        out.write(character);
                        break;
                    }
                default:
                    {
                        // dropping the backslash
                        // see 7.3.4.2 Literal Strings for further information
                        out.write(next);
                    }
            }
        } else {
            out.write(ch);
        }
        if (nextc != -2) {
            c = nextc;
        } else {
            c = seqSource.read();
        }
    }
    if (c != -1) {
        seqSource.unread(c);
    }
    return new COSString(out.toByteArray());
}
Also used : IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 2 with COSString

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

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(OperatorName.BEGIN_INLINE_IMAGE)) {
            output.write(OperatorName.BEGIN_INLINE_IMAGE.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(OperatorName.BEGIN_INLINE_IMAGE_DATA.getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
            output.write(op.getImageData());
            output.write(EOL);
            output.write(OperatorName.END_INLINE_IMAGE.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(com.tom_roush.pdfbox.contentstream.operator.Operator) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) IOException(java.io.IOException) COSBoolean(com.tom_roush.pdfbox.cos.COSBoolean) COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) Map(java.util.Map) COSFloat(com.tom_roush.pdfbox.cos.COSFloat)

Example 3 with COSString

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

the class PDSeedValueCertificate method removeOID.

/**
 * removes an OID from the list
 *
 * @param oid the object identifier to be removed.
 */
public void removeOID(byte[] oid) {
    COSBase base = this.dictionary.getDictionaryObject(COSName.OID);
    if (base instanceof COSArray) {
        COSArray array = (COSArray) base;
        array.remove(new COSString(oid));
    }
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 4 with COSString

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

the class PDSeedValueCertificate method addIssuer.

/**
 * array of bytes containing DER-encoded X.509v3 certificates of acceptable issuers. If the
 * signer’s certificate chains up to any of the specified issuers (either directly or
 * indirectly), the certificate is considered acceptable for signing.
 *
 * @param issuer A byte array containing DER-encoded X.509v3 certificate
 */
public void addIssuer(byte[] issuer) {
    COSBase base = this.dictionary.getDictionaryObject(COSName.ISSUER);
    COSArray array;
    if (base instanceof COSArray) {
        array = (COSArray) base;
    } else {
        array = new COSArray();
    }
    COSString string = new COSString(issuer);
    array.add(string);
    this.dictionary.setItem(COSName.ISSUER, array);
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 5 with COSString

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

the class PDSeedValueCertificate method removeIssuer.

/**
 * Removes an issuer from the issuers list
 *
 * @param issuer A byte array containing DER-encoded X.509v3 certificate
 */
public void removeIssuer(byte[] issuer) {
    COSBase base = this.dictionary.getDictionaryObject(COSName.ISSUER);
    if (base instanceof COSArray) {
        COSArray array = (COSArray) base;
        array.remove(new COSString(issuer));
    }
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString)

Aggregations

COSString (com.tom_roush.pdfbox.cos.COSString)65 COSArray (com.tom_roush.pdfbox.cos.COSArray)33 COSBase (com.tom_roush.pdfbox.cos.COSBase)24 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)5 COSName (com.tom_roush.pdfbox.cos.COSName)5 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)4 Map (java.util.Map)4 COSBoolean (com.tom_roush.pdfbox.cos.COSBoolean)2 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)2 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)2 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)2 MessageDigest (java.security.MessageDigest)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)1