Search in sources :

Example 21 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class CatalogValidationProcess method validateICCProfile.

/**
 * This method checks the destOutputProfile which must be a valid ICCProfile.
 *
 * If an other ICCProfile exists in the mapDestOutputProfile, a ValdiationError
 * (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE) is returned because of only one profile is authorized. If the
 * ICCProfile already exist in the mapDestOutputProfile, the method returns null. If the destOutputProfile contains
 * an invalid ICCProfile, a ValidationError (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID) is returned If the
 * destOutputProfile is an empty stream, a ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY) is returned.
 *
 * If the destOutputFile is valid, mapDestOutputProfile is updated, the ICCProfile is added to the document ctx and
 * null is returned.
 *
 * @param destOutputProfile
 * @param mapDestOutputProfile
 * @param ctx the preflight context.
 * @throws ValidationException
 */
protected void validateICCProfile(COSBase destOutputProfile, Map<COSObjectKey, Boolean> mapDestOutputProfile, PreflightContext ctx) throws ValidationException {
    try {
        if (destOutputProfile == null) {
            return;
        }
        // destOutputProfile should be an instance of COSObject because of this is a object reference
        if (destOutputProfile instanceof COSObject) {
            if (mapDestOutputProfile.containsKey(new COSObjectKey((COSObject) destOutputProfile))) {
                // the profile is already checked. continue
                return;
            } else if (!mapDestOutputProfile.isEmpty()) {
                // A DestOutputProfile exits but it isn't the same, error
                addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE, "More than one ICCProfile is defined"));
                return;
            }
        // else the profile will be kept in the tmpDestOutputProfile if it is valid
        }
        // keep reference to avoid multiple profile definition
        mapDestOutputProfile.put(new COSObjectKey((COSObject) destOutputProfile), true);
        COSDocument cosDocument = ctx.getDocument().getDocument();
        COSStream stream = COSUtils.getAsStream(destOutputProfile, cosDocument);
        if (stream == null) {
            addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "OutputIntent object uses a NULL Object"));
            return;
        }
        InputStream is = stream.createInputStream();
        ICC_Profile iccp = null;
        try {
            iccp = ICC_Profile.getInstance(is);
        } finally {
            is.close();
        }
        if (!validateICCProfileNEntry(stream, ctx, iccp)) {
            return;
        }
        if (!validateICCProfileVersion(iccp, ctx)) {
            return;
        }
        if (ctx.getIccProfileWrapper() == null) {
            ctx.setIccProfileWrapper(new ICCProfileWrapper(iccp));
        }
    } catch (IllegalArgumentException e) {
        // this is not a ICC_Profile
        addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID, "DestOutputProfile isn't a valid ICCProfile: " + e.getMessage(), e));
    } catch (IOException e) {
        throw new ValidationException("Unable to parse the ICC Profile.", e);
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) COSObject(org.apache.pdfbox.cos.COSObject) InputStream(java.io.InputStream) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) ICC_Profile(java.awt.color.ICC_Profile) ICCProfileWrapper(org.apache.pdfbox.preflight.graphic.ICCProfileWrapper)

Example 22 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class ExtGStateValidationProcess method checkLowerCA.

/**
 * This method checks the "ca" value of the ExtGState dictionary. It is optional but must be 1.0
 * if present.
 *
 * @param context the preflight context.
 * @param egs the graphic state to check
 */
private void checkLowerCA(PreflightContext context, COSDictionary egs) {
    COSBase lCA = egs.getItem(TRANSPARENCY_DICTIONARY_KEY_LOWER_CA);
    if (lCA != null) {
        // ---- If ca is present only the value 1.0 is authorized
        COSDocument cosDocument = context.getDocument().getDocument();
        Float fca = COSUtils.getAsFloat(lCA, cosDocument);
        Integer ica = COSUtils.getAsInteger(lCA, cosDocument);
        if (!(fca != null && Float.compare(fca, 1.0f) == 0) && !(ica != null && ica == 1)) {
            context.addValidationError(new ValidationError(ERROR_TRANSPARENCY_EXT_GS_CA, "ca entry in a ExtGState is invalid"));
        }
    }
}
Also used : COSBase(org.apache.pdfbox.cos.COSBase) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 23 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class ExtGStateValidationProcess method checkFont.

/**
 * This method checks a Font array in the ExtGState dictionary.
 *
 * @param context the preflight context.
 * @param egs the Graphic state to check
 * @throws ValidationException
 */
private void checkFont(PreflightContext context, COSDictionary egs) throws ValidationException {
    COSBase base = egs.getItem(COSName.FONT);
    if (base == null) {
        return;
    }
    if (!(base instanceof COSArray) || ((COSArray) base).size() != 2) {
        context.addValidationError(new ValidationError(ERROR_SYNTAX_COMMON, "/Font entry in /ExtGState must be an array with 2 elements"));
        return;
    }
    COSArray ar = (COSArray) base;
    COSBase base0 = ar.get(0);
    if (!(base0 instanceof COSObject)) {
        context.addValidationError(new ValidationError(ERROR_SYNTAX_COMMON, "1st element in /Font entry in /ExtGState must be an indirect object"));
        return;
    }
    COSBase base1 = ar.getObject(1);
    if (!(base1 instanceof COSNumber)) {
        context.addValidationError(new ValidationError(ERROR_SYNTAX_COMMON, "2nd element in /Font entry in /ExtGState must be a number"));
        return;
    }
    COSNumber fontSize = (COSNumber) ar.getObject(1);
    if (fontSize.floatValue() > MAX_POSITIVE_FLOAT || fontSize.floatValue() < MAX_NEGATIVE_FLOAT) {
        context.addValidationError(new ValidationError(ERROR_SYNTAX_NUMERIC_RANGE, "invalid float range in 2nd element in /Font entry in /ExtGState"));
    }
    if (ar.getObject(0) instanceof COSDictionary) {
        COSDictionary fontDict = (COSDictionary) ar.getObject(0);
        try {
            PDFont newFont = PDFontFactory.createFont(fontDict);
            ContextHelper.validateElement(context, newFont, FONT_PROCESS);
        } catch (IOException e) {
            addFontError(fontDict, context, e);
        }
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) COSArray(org.apache.pdfbox.cos.COSArray) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSObject(org.apache.pdfbox.cos.COSObject) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException)

Example 24 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class GraphicObjectPageValidationProcess method validate.

@Override
public void validate(PreflightContext context) throws ValidationException {
    PreflightPath vPath = context.getValidationPath();
    XObjectValidator validator = null;
    if (!vPath.isEmpty() && vPath.isExpectedType(PDImageXObject.class)) {
        validator = new XObjImageValidator(context, (PDImageXObject) vPath.peek());
    } else if (!vPath.isEmpty() && vPath.isExpectedType(PDFormXObject.class)) {
        validator = new XObjFormValidator(context, (PDFormXObject) vPath.peek());
    } else if (!vPath.isEmpty() && vPath.isExpectedType(PDPostScriptXObject.class)) {
        validator = new XObjPostscriptValidator(context, (PDPostScriptXObject) vPath.peek());
    } else if (!vPath.isEmpty() && vPath.isExpectedType(COSStream.class)) {
        context.addValidationError(new ValidationError(PreflightConstants.ERROR_GRAPHIC_XOBJECT_INVALID_TYPE, "Invalid XObject subtype"));
    } else {
        context.addValidationError(new ValidationError(PreflightConstants.ERROR_GRAPHIC_MISSING_OBJECT, "Graphic validation process needs at least one PDXObject"));
    }
    if (validator != null) {
        validator.validate();
    }
}
Also used : XObjFormValidator(org.apache.pdfbox.preflight.xobject.XObjFormValidator) PDImageXObject(org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) PDPostScriptXObject(org.apache.pdfbox.pdmodel.graphics.PDPostScriptXObject) XObjImageValidator(org.apache.pdfbox.preflight.xobject.XObjImageValidator) XObjectValidator(org.apache.pdfbox.preflight.xobject.XObjectValidator) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) XObjPostscriptValidator(org.apache.pdfbox.preflight.xobject.XObjPostscriptValidator) PreflightPath(org.apache.pdfbox.preflight.PreflightPath)

Example 25 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class ResourcesValidationProcess method validateXObjects.

protected void validateXObjects(PreflightContext context, PDResources resources) throws ValidationException {
    COSDocument cosDocument = context.getDocument().getDocument();
    COSDictionary mapOfXObj = COSUtils.getAsDictionary(resources.getCOSObject().getItem(COSName.XOBJECT), cosDocument);
    if (mapOfXObj != null) {
        for (Entry<COSName, COSBase> entry : mapOfXObj.entrySet()) {
            COSBase xobj = entry.getValue();
            if (xobj != null && COSUtils.isStream(xobj, cosDocument)) {
                try {
                    COSStream stream = COSUtils.getAsStream(xobj, cosDocument);
                    PDXObject pdXObject = PDXObject.createXObject(stream, resources);
                    if (pdXObject != null) {
                        ContextHelper.validateElement(context, pdXObject, GRAPHIC_PROCESS);
                    } else {
                        ContextHelper.validateElement(context, stream, GRAPHIC_PROCESS);
                    }
                } catch (IOException e) {
                    context.addValidationError(new ValidationError(ERROR_GRAPHIC_MAIN, e.getMessage() + " for entry '" + entry.getKey().getName() + "'", e));
                }
            }
        }
    }
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSName(org.apache.pdfbox.cos.COSName) COSDocument(org.apache.pdfbox.cos.COSDocument) COSBase(org.apache.pdfbox.cos.COSBase) IOException(java.io.IOException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDXObject(org.apache.pdfbox.pdmodel.graphics.PDXObject)

Aggregations

ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)108 COSBase (org.apache.pdfbox.cos.COSBase)36 COSDictionary (org.apache.pdfbox.cos.COSDictionary)28 IOException (java.io.IOException)27 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSObject (org.apache.pdfbox.cos.COSObject)13 COSArray (org.apache.pdfbox.cos.COSArray)10 COSStream (org.apache.pdfbox.cos.COSStream)10 COSString (org.apache.pdfbox.cos.COSString)8 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)8 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)8 COSName (org.apache.pdfbox.cos.COSName)7 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)7 PreflightPath (org.apache.pdfbox.preflight.PreflightPath)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)5 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)5 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)5 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)4