Example usage for org.apache.pdfbox.cos COSName getPDFName

List of usage examples for org.apache.pdfbox.cos COSName getPDFName

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName getPDFName.

Prototype

public static COSName getPDFName(String aName) 

Source Link

Document

This will get a COSName object with that name.

Usage

From source file:net.padaf.preflight.actions.NamedAction.java

License:Apache License

@Override
protected boolean innerValid(List<ValidationError> error) {
    String n = this.actionDictionnary.getNameAsString(COSName.getPDFName(ACTION_DICTIONARY_KEY_N));

    // ---- N entry is mandatory
    if (n == null || "".equals(n)) {
        error.add(new ValidationError(ERROR_ACTION_MISING_KEY, "N entry is mandatory for the NamedActions"));
        return false;
    }/*from   w w w. j  av  a  2 s  .c om*/

    // ---- Only Predefine name actions are authorized
    if (!(ACTION_DICTIONARY_VALUE_ATYPE_NAMED_FIRST.equals(n)
            || ACTION_DICTIONARY_VALUE_ATYPE_NAMED_LAST.equals(n)
            || ACTION_DICTIONARY_VALUE_ATYPE_NAMED_NEXT.equals(n)
            || ACTION_DICTIONARY_VALUE_ATYPE_NAMED_PREV.equals(n))) {
        error.add(new ValidationError(ERROR_ACTION_FORBIDDEN_ACTIONS_NAMED,
                n + " isn't authorized as named action"));
        return false;
    }

    return true;
}

From source file:net.padaf.preflight.actions.SubmitAction.java

License:Apache License

@Override
protected boolean innerValid(List<ValidationError> error) {
    COSBase f = this.actionDictionnary.getItem(COSName.getPDFName(ACTION_DICTIONARY_KEY_F));
    if (f == null) {
        error.add(new ValidationError(ERROR_ACTION_MISING_KEY, "F entry is mandatory for the SubmitActions"));
        return false;
    }//from  w w w. j  a va2  s.co  m
    return true;
}

From source file:net.padaf.preflight.actions.ThreadAction.java

License:Apache License

@Override
protected boolean innerValid(List<ValidationError> error) {
    COSBase d = this.actionDictionnary.getItem(COSName.getPDFName(ACTION_DICTIONARY_KEY_D));

    // ---- D entry is mandatory
    if (d == null) {
        error.add(new ValidationError(ERROR_ACTION_MISING_KEY, "D entry is mandatory for the ThreadAction"));
        return false;
    }/*from  w  w  w. j  a v a 2  s. c o m*/

    if (!(COSUtils.isInteger(d, cDoc) || COSUtils.isString(d, cDoc) || COSUtils.isDictionary(d, cDoc))) {
        error.add(new ValidationError(ERROR_ACTION_INVALID_TYPE, "D entry type is invalid"));
        return false;
    }

    return true;
}

From source file:net.padaf.preflight.actions.UriAction.java

License:Apache License

@Override
protected boolean innerValid(List<ValidationError> error) {
    COSBase uri = this.actionDictionnary.getItem(COSName.getPDFName(ACTION_DICTIONARY_KEY_URI));
    if (uri == null) {
        error.add(new ValidationError(ERROR_ACTION_MISING_KEY, "URI entry is mandatory for the UriAction"));
        return false;
    }/* www .j  a v  a  2s .  c  o  m*/

    if (!COSUtils.isString(uri, cDoc)) {
        error.add(new ValidationError(ERROR_ACTION_INVALID_TYPE, "URI entry should be a string"));
        return false;
    }

    return true;
}

From source file:net.padaf.preflight.annotation.AnnotationValidator.java

License:Apache License

/**
 * Check if the CA value is 1.0. Return true if the CA element is missing or
 * if the value is 1.0. Return false otherwise and update the given list of
 * errors.//from w  ww  .  j  ava2s  .c  om
 * 
 * @param errors
 *          list of errors which is updated if validation fails
 * @return
 */
protected boolean checkCA(List<ValidationError> errors) {
    COSBase ca = this.pdAnnot.getDictionary().getItem(COSName.getPDFName(ANNOT_DICTIONARY_KEY_CA));
    if (ca != null) {
        float caf = this.pdAnnot.getDictionary().getFloat(COSName.getPDFName(ANNOT_DICTIONARY_KEY_CA));
        if (caf != 1.0f) { // ---- Only 1.0 is authorized as value
            errors.add(new ValidationResult.ValidationError(ValidationConstants.ERROR_ANNOT_INVALID_CA,
                    "CA entry is invalid. Expected 1.0 / Read " + caf));
            return false;
        }
    } //else  optional field,  ok
    return true;
}

From source file:net.padaf.preflight.annotation.AnnotationValidator.java

License:Apache License

/**
 * This method checks the AP entry of the Annotation Dictionary. If the AP key
 * is missing, this method returns true. If the AP key exists, only the N
 * entry is authorized and must be a Stream which define the appearance of the
 * annotation. (Currently, only the type of the N entry is checked because of
 * the Appearance stream is a Form XObject, so it will be checked by the
 * Graphics Helper)/*ww  w  .  j  a  v a2  s .c o  m*/
 * 
 * If the AP content isn't valid, this method return false and updates the
 * errors list.
 * 
 * @param errors
 *          list of errors which is updated if validation fails
 * @return
 */
protected boolean checkAP(List<ValidationError> errors) {
    PDAppearanceDictionary ap = this.pdAnnot.getAppearance();
    if (ap != null) {
        COSDictionary apDict = ap.getDictionary();
        // ---- Only N entry is authorized
        if (apDict.getItem(COSName.getPDFName(ANNOT_DICTIONARY_KEY_D)) != null
                || apDict.getItem(COSName.getPDFName(ANNOT_DICTIONARY_KEY_R)) != null) {
            errors.add(new ValidationResult.ValidationError(ValidationConstants.ERROR_ANNOT_INVALID_AP_CONTENT,
                    "Only the N Appearance is authorized"));
            return false;
        } else if (apDict.getItem(COSName.getPDFName(ANNOT_DICTIONARY_KEY_N)) == null) {
            // ---- N entry is required
            errors.add(new ValidationResult.ValidationError(
                    ValidationConstants.ERROR_ANNOT_MISSING_AP_N_CONTENT, "The N Appearance must be present"));
            return false;
        } else {
            // ---- the N entry must be a Stream (Dictionaries are forbidden)
            COSBase apn = apDict.getItem(COSName.getPDFName(ANNOT_DICTIONARY_KEY_N));
            if (!COSUtils.isStream(apn, this.cosDoc)) {
                errors.add(
                        new ValidationResult.ValidationError(ValidationConstants.ERROR_ANNOT_INVALID_AP_CONTENT,
                                "The N Appearance must be a Stream"));
                return false;
            }
        }
    } //else  ok, nothing to check,this field is optional
    return true;
}

From source file:net.padaf.preflight.annotation.AnnotationValidator.java

License:Apache License

/**
 * This method validates the Popup entry. This entry shall contain an other
 * Annotation. This annotation is validated with the right
 * AnnotationValidator./*from  w  w w. j  a v  a2s.  c o  m*/
 * 
 * @param errors
 * @return
 * @throws ValidationException
 */
protected boolean checkPopup(List<ValidationError> errors) throws ValidationException {
    COSBase cosPopup = this.annotDictionary.getItem(COSName.getPDFName(ANNOT_DICTIONARY_VALUE_SUBTYPE_POPUP));
    if (cosPopup != null) {
        COSDictionary popupDict = COSUtils.getAsDictionary(cosPopup, this.cosDoc);
        if (popupDict == null) {
            errors.add(new ValidationError(ERROR_SYNTAX_DICT_INVALID,
                    "An Annotation has a Popup entry, but the value is missing or isn't a dictionary"));
            return false;
        }
        AnnotationValidator popupVal = this.annotFact.getAnnotationValidator(popupDict, handler, errors);
        return popupVal.validate(errors);
    }
    return true;
}

From source file:net.padaf.preflight.annotation.PDFAbAnnotationFactory.java

License:Apache License

/**
 * Return an instance of AnnotationValidator if the annotation subtype is
 * authorized for a PDF/A. Otherwise, returns null and the given list is
 * updated with the right error code./*  w  ww .  j a v  a  2 s.  c om*/
 * 
 * If the subtype isn't mentioned in the PDF/A specification and if it doesn't
 * exist in the PDF Reference 1.4, it will be considered as an invalid
 * annotation. Here is the list of Annotations which appear after the PDF 1.4
 * :
 * <UL>
 * <li>Polygon (1.5)
 * <li>Polyline (1.5)
 * <li>Caret (1.5)
 * <li>Screen (1.5)
 * <li>Watermark (1.6)
 * <li>3D (1.6)
 * </UL>
 * 
 * @param annotDic
 * @param handler
 * @param errors
 * @return
 */
@Override
public AnnotationValidator instantiateAnnotationValidator(COSDictionary annotDic, DocumentHandler handler,
        List<ValidationError> errors) {
    AnnotationValidator av = null;

    String subtype = annotDic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

    if (subtype == null || "".equals(subtype)) {
        errors.add(new ValidationError(ERROR_ANNOT_MISSING_SUBTYPE));
    } else {
        if (ANNOT_DICTIONARY_VALUE_SUBTYPE_TEXT.equals(subtype)) {
            av = new TextAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_LINK.equals(subtype)) {
            av = new LinkAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_FREETEXT.equals(subtype)) {
            av = new FreeTextAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_LINE.equals(subtype)) {
            av = new LineAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_SQUARE.equals(subtype)
                || ANNOT_DICTIONARY_VALUE_SUBTYPE_CIRCLE.equals(subtype)) {
            av = new SquareCircleAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_HIGHLIGHT.equals(subtype)
                || ANNOT_DICTIONARY_VALUE_SUBTYPE_UNDERLINE.equals(subtype)
                || ANNOT_DICTIONARY_VALUE_SUBTYPE_STRIKEOUT.equals(subtype)
                || ANNOT_DICTIONARY_VALUE_SUBTYPE_SQUILGGLY.equals(subtype)) {
            av = new MarkupAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_STAMP.equals(subtype)) {
            av = new RubberStampAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_INK.equals(subtype)) {
            av = new InkAnnotationValdiator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_POPUP.equals(subtype)) {
            av = new PopupAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_WIDGET.equals(subtype)) {
            av = new WidgetAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_PRINTERMARK.equals(subtype)) {
            av = new PrintMarkAnnotationValidator(handler, annotDic);
        } else if (ANNOT_DICTIONARY_VALUE_SUBTYPE_TRAPNET.equals(subtype)) {
            av = new TrapNetAnnotationValidator(handler, annotDic);
        } else {
            errors.add(new ValidationError(ERROR_ANNOT_FORBIDDEN_SUBTYPE,
                    "The subtype isn't authorized : " + subtype));
        }
    }

    if (av != null) {
        // initialize the factory if the Validator has been created
        av.setFactory(this);
    }

    return av;
}

From source file:net.padaf.preflight.font.CompositeFontValidator.java

License:Apache License

/**
 * This methods extracts from the Font dictionary all mandatory fields. If a
 * mandatory field is missing, the list of ValidationError in the
 * FontContainer is updated. On error, the method returns false.
 * /*from   ww w  .j  ava  2 s.  co  m*/
 * @return
 */
protected boolean checkMandatoryFields() {
    String type = fDictionary.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
    String subtype = fDictionary.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

    // ---- just check if they are present because of the Helper has already
    // checked them.
    if ((type == null || "".equals(type)) || (subtype == null || "".equals(subtype))) {
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "Type and/or Subtype keys are missing"));
        return false;
    }

    // ---- Check presence of baseFont, CMap and CIDFont
    this.basefont = fDictionary.getNameAsString(COSName.getPDFName(FONT_DICTIONARY_KEY_BASEFONT));
    this.descendantFonts = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_DESCENDANT_FONTS));
    this.encoding = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_ENCODING));

    if ((basefont == null || "".equals(basefont)) || descendantFonts == null || encoding == null) {
        // ---- baseFont syntax isn't checked because of it is a convention not a
        // rule
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                "BaseFont, Encoding or DescendantFonts keys are missing"));
        return false;
    }

    // ---- toUnicode is optional, but keep the value if present.
    this.toUnicode = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_TOUNICODE));
    return true;
}

From source file:net.padaf.preflight.font.CompositeFontValidator.java

License:Apache License

/**
 * This method validates the CIDFont dictionary.
 * // w  w w . j av  a  2s  .  c  o  m
 * This method returns false and updates the list of errors in the
 * FontContainer if some mandatory fields are missing.
 * 
 * This method calls the processCIDFontTypeX method to check if the font is
 * damaged or not. If the font is damaged, the errors list is updated and the
 * method return false.
 * 
 * @return
 * @throws ValidationException
 */
protected boolean checkCIDFont() throws ValidationException {

    // ---- a CIDFont is contained in the DescendantFonts array
    COSDocument cDoc = this.handler.getDocument().getDocument();
    COSArray array = COSUtils.getAsArray(descendantFonts, cDoc);

    if (array == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_INVALID,
                "CIDFont is missing from the DescendantFonts array"));
        return false;
    }
    // ---- in PDF 1.4, this array must contain only one element,
    // because of a PDF/A should be a PDF 1.4, this method returns an error if
    // the array
    // has more than one element.
    if (array.size() != 1) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_INVALID,
                "The DescendantFonts array should have one element."));
        return false;
    }

    this.cidFont = COSUtils.getAsDictionary(array.get(0), cDoc);
    if (this.cidFont == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_INVALID,
                "The DescendantFonts array should have one element with is a dictionary."));
        return false;
    }

    String type = cidFont.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
    String subtype = cidFont.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

    if ((type == null || "".equals(type)) || (subtype == null || "".equals(subtype))) {
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "Type and/or Subtype keys are missing"));
        return false;
    }

    boolean isT0 = FONT_DICTIONARY_VALUE_TYPE0.equals(subtype);
    boolean isT2 = FONT_DICTIONARY_VALUE_TYPE2.equals(subtype);
    // ---- Even if these entries are present, values must be checked.
    if (!FONT_DICTIONARY_VALUE_FONT.equals(type) || !(isT0 || isT2)) {
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "Type and/or Subtype keys are missing"));
        return false;
    }

    // ---- BaseFont is mandatory
    String bf = cidFont.getNameAsString(COSName.getPDFName(FONT_DICTIONARY_KEY_BASEFONT));
    if (bf == null || "".equals(bf)) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "BaseFont is missing"));
        return false;
    }

    // ---- checks others mandatory fields
    COSBase sysinfo = cidFont.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CID_SYSINFO));
    COSBase fontDesc = cidFont.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_FONT_DESC));
    COSBase cidToGid = cidFont.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CID_GIDMAP));

    boolean result = checkCIDSystemInfo(sysinfo, cDoc);
    if (isT0) {
        result = result && checkCIDToGIDMap(cidToGid, cDoc, false);
        result = result && processCIDFontType0(fontDesc);
    } else {
        result = result && checkCIDToGIDMap(cidToGid, cDoc, true);
        result = result && processCIDFontType2(fontDesc);
    }
    return result;
}