Search in sources :

Example 16 with COSName

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

the class PDFontDescriptor method getFontStretch.

/**
 * A string representing the preferred font stretch.
 * According to the PDF Spec:
 * The font stretch value; it must be one of the following (ordered from
 * narrowest to widest): UltraCondensed, ExtraCondensed, Condensed, SemiCondensed,
 * Normal, SemiExpanded, Expanded, ExtraExpanded or UltraExpanded.
 *
 * @return The stretch of the font.
 */
public String getFontStretch() {
    String retval = null;
    COSName name = (COSName) dic.getDictionaryObject(COSName.FONT_STRETCH);
    if (name != null) {
        retval = name.getName();
    }
    return retval;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 17 with COSName

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

the class PageDrawer method isHiddenOCMD.

private boolean isHiddenOCMD(PDOptionalContentMembershipDictionary ocmd) {
    if (ocmd.getCOSObject().getCOSArray(COSName.VE) != null) {
        // support seems to be optional, and is approximated by /P and /OCGS
        Log.i("PdfBox-Android", "/VE entry ignored in Optional Content Membership Dictionary");
    }
    List<Boolean> visibles = new ArrayList<Boolean>();
    for (PDPropertyList prop : ocmd.getOCGs()) {
        visibles.add(!isHiddenOCG(prop));
    }
    COSName visibilityPolicy = ocmd.getVisibilityPolicy();
    if (COSName.ANY_OFF.equals(visibilityPolicy)) {
        for (boolean visible : visibles) {
            if (!visible) {
                return true;
            }
        }
        return false;
    }
    if (COSName.ALL_ON.equals(visibilityPolicy)) {
        for (boolean visible : visibles) {
            if (!visible) {
                return true;
            }
        }
        return false;
    }
    if (COSName.ALL_OFF.equals(visibilityPolicy)) {
        for (boolean visible : visibles) {
            if (visible) {
                return false;
            }
        }
        return true;
    }
    // AnyOn is default
    for (boolean visible : visibles) {
        if (visible) {
            return false;
        }
    }
    return true;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) ArrayList(java.util.ArrayList) PDPropertyList(com.tom_roush.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList)

Example 18 with COSName

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

the class PageDrawer method hasBlendMode.

private boolean hasBlendMode(PDTransparencyGroup group, Set<COSBase> groupsDone) {
    if (groupsDone.contains(group.getCOSObject())) {
        // The group was already processed. Avoid endless recursion.
        return false;
    }
    groupsDone.add(group.getCOSObject());
    PDResources resources = group.getResources();
    if (resources == null) {
        return false;
    }
    for (COSName name : resources.getExtGStateNames()) {
        PDExtendedGraphicsState extGState = resources.getExtGState(name);
        if (extGState == null) {
            continue;
        }
        BlendMode blendMode = extGState.getBlendMode();
        if (blendMode != BlendMode.NORMAL) {
            return true;
        }
    }
    // Recursively process nested transparency groups
    for (COSName name : resources.getXObjectNames()) {
        PDXObject xObject;
        try {
            xObject = resources.getXObject(name);
        } catch (IOException ex) {
            continue;
        }
        if (xObject instanceof PDTransparencyGroup && hasBlendMode((PDTransparencyGroup) xObject, groupsDone)) {
            return true;
        }
    }
    return false;
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSName(com.tom_roush.pdfbox.cos.COSName) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) BlendMode(com.tom_roush.pdfbox.pdmodel.graphics.blend.BlendMode) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 19 with COSName

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

the class MergeAnnotationsTest method testAnnotationsMatch.

/*
     * Source and target annotations are lĂ­nked by name with the target annotation's name
     * being the source annotation's name prepended with 'annoRef_'
     */
private boolean testAnnotationsMatch(List<PDAnnotation> sourceAnnots, List<PDAnnotation> targetAnnots) {
    Map<String, PDAnnotation> targetAnnotsByName = new HashMap<String, PDAnnotation>();
    COSName destinationName;
    // fill the map with the annotations destination name
    for (PDAnnotation targetAnnot : targetAnnots) {
        destinationName = (COSName) targetAnnot.getCOSObject().getDictionaryObject(COSName.DEST);
        targetAnnotsByName.put(destinationName.getName(), targetAnnot);
    }
    // try to lookup the target annotation for the source annotation by destination name
    for (PDAnnotation sourceAnnot : sourceAnnots) {
        destinationName = (COSName) sourceAnnot.getCOSObject().getDictionaryObject(COSName.DEST);
        if (targetAnnotsByName.get("annoRef_" + destinationName.getName()) == null) {
            return false;
        }
    }
    return true;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) HashMap(java.util.HashMap) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)

Example 20 with COSName

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

the class StandardSecurityHandler method prepareForDecryption.

/**
 * Prepares everything to decrypt the document.
 *
 * Only if decryption of single objects is needed this should be called.
 *
 * @param encryption  encryption dictionary
 * @param documentIDArray  document id
 * @param decryptionMaterial Information used to decrypt the document.
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException If there is an error accessing data.
 */
@Override
public void prepareForDecryption(PDEncryption encryption, COSArray documentIDArray, DecryptionMaterial decryptionMaterial) throws IOException {
    if (!(decryptionMaterial instanceof StandardDecryptionMaterial)) {
        throw new IOException("Decryption material is not compatible with the document");
    }
    // This is only used with security version 4 and 5.
    if (encryption.getVersion() >= 4) {
        setStreamFilterName(encryption.getStreamFilterName());
        setStringFilterName(encryption.getStreamFilterName());
    }
    setDecryptMetadata(encryption.isEncryptMetaData());
    StandardDecryptionMaterial material = (StandardDecryptionMaterial) decryptionMaterial;
    String password = material.getPassword();
    if (password == null) {
        password = "";
    }
    int dicPermissions = encryption.getPermissions();
    int dicRevision = encryption.getRevision();
    int dicLength = encryption.getVersion() == 1 ? 5 : encryption.getLength() / 8;
    byte[] documentIDBytes = getDocumentIDBytes(documentIDArray);
    // we need to know whether the meta data was encrypted for password calculation
    boolean encryptMetadata = encryption.isEncryptMetaData();
    byte[] userKey = encryption.getUserKey();
    byte[] ownerKey = encryption.getOwnerKey();
    byte[] ue = null, oe = null;
    Charset passwordCharset = Charsets.ISO_8859_1;
    if (dicRevision == 6 || dicRevision == 5) {
        passwordCharset = Charsets.UTF_8;
        ue = encryption.getUserEncryptionKey();
        oe = encryption.getOwnerEncryptionKey();
    }
    if (dicRevision == 6) {
        // PDFBOX-4155
        password = SaslPrep.saslPrepQuery(password);
    }
    AccessPermission currentAccessPermission;
    if (isOwnerPassword(password.getBytes(passwordCharset), userKey, ownerKey, dicPermissions, documentIDBytes, dicRevision, dicLength, encryptMetadata)) {
        currentAccessPermission = AccessPermission.getOwnerAccessPermission();
        setCurrentAccessPermission(currentAccessPermission);
        byte[] computedPassword;
        if (dicRevision == 6 || dicRevision == 5) {
            computedPassword = password.getBytes(passwordCharset);
        } else {
            computedPassword = getUserPassword(password.getBytes(passwordCharset), ownerKey, dicRevision, dicLength);
        }
        encryptionKey = computeEncryptedKey(computedPassword, ownerKey, userKey, oe, ue, dicPermissions, documentIDBytes, dicRevision, dicLength, encryptMetadata, true);
    } else if (isUserPassword(password.getBytes(passwordCharset), userKey, ownerKey, dicPermissions, documentIDBytes, dicRevision, dicLength, encryptMetadata)) {
        currentAccessPermission = new AccessPermission(dicPermissions);
        currentAccessPermission.setReadOnly();
        setCurrentAccessPermission(currentAccessPermission);
        encryptionKey = computeEncryptedKey(password.getBytes(passwordCharset), ownerKey, userKey, oe, ue, dicPermissions, documentIDBytes, dicRevision, dicLength, encryptMetadata, false);
    } else {
        throw new InvalidPasswordException("Cannot decrypt PDF, the password is incorrect");
    }
    if (dicRevision == 6 || dicRevision == 5) {
        validatePerms(encryption, dicPermissions, encryptMetadata);
    }
    if (encryption.getVersion() == 4 || encryption.getVersion() == 5) {
        // detect whether AES encryption is used. This assumes that the encryption algo is
        // stored in the PDCryptFilterDictionary
        // However, crypt filters are used only when V is 4 or 5.
        PDCryptFilterDictionary stdCryptFilterDictionary = encryption.getStdCryptFilterDictionary();
        if (stdCryptFilterDictionary != null) {
            COSName cryptFilterMethod = stdCryptFilterDictionary.getCryptFilterMethod();
            setAES(COSName.AESV2.equals(cryptFilterMethod) || COSName.AESV3.equals(cryptFilterMethod));
        }
    }
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) Charset(java.nio.charset.Charset) IOException(java.io.IOException) COSString(com.tom_roush.pdfbox.cos.COSString)

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