Search in sources :

Example 16 with ValidationError

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

the class AcroFormValidationProcess method validateField.

/**
 * A and AA field are forbidden, this method checks if they are present and checks all children of this field. If the
 * an Additional Action is present the error code ERROR_ACTION_FORBIDDEN_ADDITIONAL_ACTIONS_FIELD (6.2.3) is added
 * to the error list If the an Action is present (in the Widget Annotation) the error
 * ERROR_ACTION_FORBIDDEN_WIDGET_ACTION_FIELD (6.2.4) is added to the error list. (Remark : The widget validation
 * will be done by the AnnotationValidationHelper, but some actions are authorized in a standard Widget)
 *
 * @param ctx the preflight context.
 * @param field an acro forms field.
 * @return the result of the check for A or AA entries.
 * @throws IOException
 */
protected boolean validateField(PreflightContext ctx, PDField field) throws IOException {
    boolean res = true;
    PDFormFieldAdditionalActions aa = field.getActions();
    if (aa != null) {
        addValidationError(ctx, new ValidationError(ERROR_ACTION_FORBIDDEN_ADDITIONAL_ACTIONS_FIELD, "\"AA\" must not be used in a Field dictionary"));
        res = false;
    }
    if (field instanceof PDTerminalField) {
        // The widget validation will be done by the widget annotation, a widget contained in a Field can't have action.
        List<PDAnnotationWidget> widgets = field.getWidgets();
        if (res && widgets != null) {
            for (PDAnnotationWidget widget : widgets) {
                ContextHelper.validateElement(ctx, widget.getCOSObject(), ANNOTATIONS_PROCESS);
                COSBase act = widget.getCOSObject().getDictionaryObject(COSName.A);
                if (act != null) {
                    addValidationError(ctx, new ValidationError(ERROR_ACTION_FORBIDDEN_WIDGET_ACTION_FIELD, "\"A\" must not be used in a widget annotation"));
                    return false;
                }
            }
        }
        return exploreWidgets(ctx, field.getWidgets());
    } else {
        return res && exploreFields(ctx, ((PDNonTerminalField) field).getChildren());
    }
}
Also used : PDFormFieldAdditionalActions(org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions) PDTerminalField(org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDNonTerminalField(org.apache.pdfbox.pdmodel.interactive.form.PDNonTerminalField)

Example 17 with ValidationError

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

the class BookmarkValidationProcess method validate.

@Override
public void validate(PreflightContext ctx) throws ValidationException {
    PDDocumentCatalog catalog = ctx.getDocument().getDocumentCatalog();
    if (catalog != null) {
        PDDocumentOutline outlineHierarchy = catalog.getDocumentOutline();
        if (outlineHierarchy != null) {
            COSDictionary dict = outlineHierarchy.getCOSObject();
            if (!checkIndirectObjects(ctx, dict)) {
                return;
            }
            COSObject firstObj = toCOSObject(dict.getItem(COSName.FIRST));
            COSObject lastObj = toCOSObject(dict.getItem(COSName.LAST));
            // Count entry is mandatory if there are childrens
            if (!isCountEntryPresent(dict) && (outlineHierarchy.getFirstChild() != null || outlineHierarchy.getLastChild() != null)) {
                addValidationError(ctx, new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID, "Outline Hierarchy doesn't have Count entry"));
            } else if (isCountEntryPositive(ctx, dict) && (outlineHierarchy.getFirstChild() == null || outlineHierarchy.getLastChild() == null)) {
                addValidationError(ctx, new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID, "Outline Hierarchy doesn't have First and/or Last entry(ies)"));
            } else {
                exploreOutlineLevel(ctx, outlineHierarchy.getFirstChild(), firstObj, lastObj);
            }
        }
    } else {
        ctx.addValidationError(new ValidationError(ERROR_SYNTAX_NOCATALOG, "There is no /Catalog entry in the Document"));
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDDocumentOutline(org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline) COSObject(org.apache.pdfbox.cos.COSObject) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Example 18 with ValidationError

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

the class BookmarkValidationProcess method checkIndirectObject.

// verify that if the named item exists, that it is is an indirect object
private boolean checkIndirectObject(PreflightContext ctx, COSDictionary dictionary, COSName name) {
    COSBase item = dictionary.getItem(name);
    if (item == null || item instanceof COSNull || item instanceof COSObject) {
        return true;
    }
    addValidationError(ctx, new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID, "/" + name.getName() + " entry must be an indirect object"));
    return false;
}
Also used : COSObject(org.apache.pdfbox.cos.COSObject) COSBase(org.apache.pdfbox.cos.COSBase) COSNull(org.apache.pdfbox.cos.COSNull) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 19 with ValidationError

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

the class CatalogValidationProcess method validateICCProfileNEntry.

private boolean validateICCProfileNEntry(COSStream stream, PreflightContext ctx, ICC_Profile iccp) {
    COSDictionary streamDict = (COSDictionary) stream.getCOSObject();
    if (!streamDict.containsKey(COSName.N)) {
        addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "/N entry of ICC profile is mandatory"));
        return false;
    }
    COSBase nValue = streamDict.getItem(COSName.N);
    if (!(nValue instanceof COSNumber)) {
        addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "/N entry of ICC profile must be a number, but is " + nValue));
        return false;
    }
    int nNumberValue = ((COSNumber) nValue).intValue();
    if (nNumberValue != 1 && nNumberValue != 3 && nNumberValue != 4) {
        addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "/N entry of ICC profile must be 1, 3 or 4, but is " + nNumberValue));
        return false;
    }
    if (iccp.getNumComponents() != nNumberValue) {
        addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "/N entry of ICC profile is " + nNumberValue + " but the ICC profile has " + iccp.getNumComponents() + " components"));
        return false;
    }
    return true;
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 20 with ValidationError

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

the class CatalogValidationProcess method validateICCProfileVersion.

private boolean validateICCProfileVersion(ICC_Profile iccp, PreflightContext ctx) {
    PreflightConfiguration config = ctx.getConfig();
    // check the ICC Profile version (6.2.2)
    if (iccp.getMajorVersion() == 2) {
        if (iccp.getMinorVersion() > 0x40) {
            // in PDF 1.4, max version is 02h.40h (meaning V 3.5)
            // see the ICCProfile specification (ICC.1:1998-09)page 13 - §6.1.3 :
            // The current profile version number is "2.4.0" (encoded as 02400000h")
            ValidationError error = new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_TOO_RECENT, "Invalid version of the ICCProfile");
            error.setWarning(config.isLazyValidation());
            addValidationError(ctx, error);
            return false;
        }
    // else OK
    } else if (iccp.getMajorVersion() > 2) {
        // in PDF 1.4, max version is 02h.40h (meaning V 3.5)
        // see the ICCProfile specification (ICC.1:1998-09)page 13 - §6.1.3 :
        // The current profile version number is "2.4.0" (encoded as 02400000h"
        ValidationError error = new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_TOO_RECENT, "Invalid version of the ICCProfile");
        error.setWarning(config.isLazyValidation());
        addValidationError(ctx, error);
        return false;
    }
    // else seems less than 2, so correct
    return true;
}
Also used : PreflightConfiguration(org.apache.pdfbox.preflight.PreflightConfiguration) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

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