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.graphics.XObjImageValidator.java

License:Apache License

protected void checkIntent(List<ValidationError> result) throws ValidationException {
    if (this.xobject.getItem(COSName.getPDFName("Intent")) != null) {
        String s = this.xobject.getNameAsString("Intent");
        if (!RenderingIntents.contains(s)) {
            result.add(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY,
                    "Unexpected value '" + s + "' for Intent key in image"));
        }/*w  w w.  jav  a 2s . c o  m*/
    }
}

From source file:net.padaf.preflight.graphics.XObjImageValidator.java

License:Apache License

protected void checkColorSpaceAndImageMask(List<ValidationError> result) throws ValidationException {

    COSBase csImg = this.xobject.getItem(COSName.getPDFName(XOBJECT_DICTIONARY_KEY_COLOR_SPACE));
    COSBase bitsPerComp = this.xobject.getItem(COSName.getPDFName("BitsPerComponent"));
    COSBase mask = this.xobject.getItem(COSName.getPDFName("Mask"));

    if (isImageMaskTrue()) {
        if (csImg != null || mask != null) {
            result.add(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_KEY,
                    "ImageMask entry is true, ColorSpace and Mask are forbidden."));
        }/* www .  j  a  va  2  s .  c  o  m*/

        Integer bitsPerCompValue = COSUtils.getAsInteger(bitsPerComp, cosDocument);
        if (bitsPerCompValue != 1) {
            result.add(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY,
                    "ImageMask entry is true, BitsPerComponent must be 1."));
        }

    } else {
        ColorSpaceHelper csh = ColorSpaceHelperFactory.getColorSpaceHelper(csImg, handler,
                ColorSpaceRestriction.NO_PATTERN);
        csh.validate(result);
    }
}

From source file:net.padaf.preflight.graphics.XObjImageValidator.java

License:Apache License

private boolean isImageMaskTrue() {
    COSBase imgMask = this.xobject.getItem(COSName.getPDFName("ImageMask"));
    if (imgMask != null && imgMask instanceof COSBoolean) {
        return ((COSBoolean) imgMask).getValue();
    } else {/*  w  w w.  j  a va2 s  .co m*/
        return false;
    }
}

From source file:net.padaf.preflight.helpers.BookmarkValidationHelper.java

License:Apache License

/**
 * Return true if the Count entry is present in the given dictionary.
 * /* w ww .  j  a va 2s. co  m*/
 * @param outline
 * @return
 */
private boolean isCountEntryPresent(COSDictionary outline) {
    return outline.getItem(COSName.getPDFName("Count")) != null;
}

From source file:net.padaf.preflight.helpers.BookmarkValidationHelper.java

License:Apache License

/**
 * This method checks the inputItem dictionary and call the
 * exploreOutlineLevel method on the first child if it is not null.
 * /*from   www.  jav a  2  s.  c  o m*/
 * @param inputItem
 *          outline item to validate
 * @param handler
 *          The document handler which provides useful data for the level
 *          exploration (ex : access to the PDDocument)
 * @param result
 * @return
 * @throws ValidationException
 */
protected boolean validateItem(PDOutlineItem inputItem, DocumentHandler handler, List<ValidationError> result)
        throws ValidationException {
    boolean isValid = true;
    // ---- Dest entry isn't permitted if the A entry is present
    // A entry isn't permitted if the Dest entry is present
    // If the A enntry is present, the referenced actions is validated
    COSDictionary dictionary = inputItem.getCOSDictionary();
    COSBase dest = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_DESTINATION));
    COSBase action = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_ACTION));

    if (action != null && dest != null) {
        result.add(new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID,
                "Dest entry isn't permitted if the A entry is present"));
        return false;
    } else if (action != null) {
        List<AbstractActionManager> actions = this.actionFact.getActions(dictionary,
                handler.getDocument().getDocument());
        for (AbstractActionManager act : actions) {
            isValid = isValid && act.valid(result);
        }
    } // else no specific validation

    // ---- check children
    PDOutlineItem fChild = inputItem.getFirstChild();
    if (fChild != null) {
        if (!isCountEntryPresent(inputItem.getCOSDictionary())) {
            result.add(new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID,
                    "Outline item doesn't have Count entry but has at least one descendant."));
            isValid = false;
        } else {
            // ---- there are some descendants, so dictionary must have a Count
            // entry
            isValid = isValid && exploreOutlineLevel(fChild, handler, result);
        }
    }

    return isValid;
}

From source file:net.padaf.preflight.helpers.CatalogValidationHelper.java

License:Apache License

/**
 * A Catalog shall not contain the OCPProperties (Optional Content Properties)
 * entry./* ww w  .  ja v  a 2 s.co m*/
 * 
 * @param handler
 * @param catalog
 * @param result
 * @throws ValidationException
 */
protected void validateOCProperties(DocumentHandler handler, PDDocumentCatalog catalog,
        List<ValidationError> result) throws ValidationException {
    COSBase ocp = catalog.getCOSDictionary()
            .getItem(COSName.getPDFName(DOCUMENT_DICTIONARY_KEY_OPTIONAL_CONTENTS));
    if (ocp != null) {
        result.add(new ValidationError(ERROR_SYNTAX_TRAILER_CATALOG_OCPROPERTIES,
                "A Catalog shall not contain the OCPProperties entry."));
    }
}

From source file:net.padaf.preflight.helpers.CatalogValidationHelper.java

License:Apache License

/**
 * This method checks the content of each OutputIntent. The S entry must
 * contain GTS_PDFA1. The DestOuputProfile must contain a valid ICC Profile
 * Stream./*from   ww w  . j  a  v a 2  s  .  c  om*/
 * 
 * If there are more than one OutputIntent, they have to use the same ICC
 * Profile.
 * 
 * This method returns a list of ValidationError. It is empty if no errors
 * have been found.
 * 
 * @param handler
 * @return
 * @throws ValidationException
 */
public List<ValidationError> validateOutputIntent(DocumentHandler handler) throws ValidationException {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    PDDocument pdDocument = handler.getDocument();
    PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
    COSDocument cDoc = pdDocument.getDocument();

    COSBase cBase = catalog.getCOSDictionary()
            .getItem(COSName.getPDFName(DOCUMENT_DICTIONARY_KEY_OUTPUT_INTENTS));
    COSArray outputIntents = COSUtils.getAsArray(cBase, cDoc);

    Map<COSObjectKey, Boolean> tmpDestOutputProfile = new HashMap<COSObjectKey, Boolean>();

    for (int i = 0; outputIntents != null && i < outputIntents.size(); ++i) {
        COSDictionary dictionary = COSUtils.getAsDictionary(outputIntents.get(i), cDoc);

        if (dictionary == null) {

            result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
                    "OutputIntent object is null or isn't a dictionary"));

        } else {
            // ---- S entry is mandatory and must be equals to GTS_PDFA1
            String sValue = dictionary.getNameAsString(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_S));
            if (!OUTPUT_INTENT_DICTIONARY_VALUE_GTS_PDFA1.equals(sValue)) {
                result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_S_VALUE_INVALID,
                        "The S entry of the OutputIntent isn't GTS_PDFA1"));
                continue;
            }

            // ---- OutputConditionIdentifier is a mandatory field
            String outputConditionIdentifier = dictionary
                    .getString(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_OUTPUT_CONDITION_IDENTIFIER));
            if (outputConditionIdentifier == null || "".equals(outputConditionIdentifier)) {
                result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
                        "The OutputIntentCondition is missing"));
                continue;
            }

            // ---- If OutputConditionIdentifier is "Custom" :
            // ---- DestOutputProfile and Info are mandatory
            // ---- DestOutputProfile must be a ICC Profile

            // ---- Because of PDF/A conforming file needs to specify the color
            // characteristics, the DestOutputProfile
            // is checked even if the OutputConditionIdentifier isn't "Custom"
            COSBase dop = dictionary
                    .getItem(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_DEST_OUTPUT_PROFILE));
            ValidationError valer = validateICCProfile(dop, cDoc, tmpDestOutputProfile, handler);
            if (valer != null) {
                result.add(valer);
                continue;
            }

            if (OUTPUT_INTENT_DICTIONARY_VALUE_OUTPUT_CONDITION_IDENTIFIER_CUSTOM
                    .equals(outputConditionIdentifier)) {
                String info = dictionary.getString(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_INFO));
                if (info == null || "".equals(info)) {
                    result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
                            "The Info entry of a OutputIntent dictionary is missing"));
                    continue;
                }
            }
        }
    }
    return result;
}

From source file:net.padaf.preflight.helpers.FileSpecificationValidationHelper.java

License:Apache License

@Override
public List<ValidationError> innerValidate(DocumentHandler handler) throws ValidationException {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    PDDocument pdfDoc = handler.getDocument();
    COSDocument cDoc = pdfDoc.getDocument();

    List<?> lCOSObj = cDoc.getObjects();
    for (Object o : lCOSObj) {
        COSObject cObj = (COSObject) o;/*from w ww . j  a  v a 2 s  . c om*/

        // ---- If this object represents a Stream
        // The Dictionary must contain the Length key
        COSBase cBase = cObj.getObject();
        if (cBase instanceof COSDictionary) {
            COSDictionary dic = (COSDictionary) cBase;
            String type = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
            if (FILE_SPECIFICATION_VALUE_TYPE.equals(type)) {
                // ---- It is a file specification
                result.addAll(validateFileSpecification(handler, cObj));
            }
        }
    }
    return result;
}

From source file:net.padaf.preflight.helpers.FileSpecificationValidationHelper.java

License:Apache License

/**
 * Validate a FileSpec dictionary, a FileSpec dictionary mustn't have the EF
 * (EmbeddedFile) entry./*from  w  w w  .  j a  va  2  s .  c o  m*/
 * 
 * @param handler
 *          The document handler
 * @param cObj
 *          the FileSpec Dictionary
 * @return
 */
public List<ValidationError> validateFileSpecification(DocumentHandler handler, COSObject cObj) {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    COSDictionary fileSpec = (COSDictionary) cObj.getObject();

    // ---- Check dictionary entries
    // ---- Only the EF entry is forbidden
    if (fileSpec.getItem(COSName.getPDFName(FILE_SPECIFICATION_KEY_EMBEDDED_FILE)) != null) {
        result.add(new ValidationError(ERROR_SYNTAX_EMBEDDED_FILES,
                "EmbeddedFile entry is present in a FileSpecification dictionary"));
    }

    return result;
}

From source file:net.padaf.preflight.helpers.FontValidationHelper.java

License:Apache License

@Override
public List<ValidationError> innerValidate(DocumentHandler handler) throws ValidationException {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    PDDocument pdfDoc = handler.getDocument();
    COSDocument cDoc = pdfDoc.getDocument();

    List<?> lCOSObj = cDoc.getObjects();

    List<FontValidator> lType3 = new ArrayList<FontValidator>();

    for (Object o : lCOSObj) {
        COSObject cObj = (COSObject) o;/*ww  w . j a v a2 s. c o  m*/

        // If this object represents a Stream, the Dictionary must contain the
        // Length key
        COSBase cBase = cObj.getObject();
        if (cBase instanceof COSDictionary) {
            COSDictionary dic = (COSDictionary) cBase;
            String type = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
            if (type != null && FONT_DICTIONARY_VALUE_FONT.equals(type)) {
                FontValidator fontVal = fontValidationFactory.getFontValidator(cObj, handler);
                if (fontVal instanceof Type3FontValidator) {
                    lType3.add(fontVal);
                } else {
                    validateFont(handler, fontVal, result);
                }
            }
        }
    }

    // ---- Type 3 can contain other font, so type 3 are validated at the end.
    for (FontValidator t3FontVal : lType3) {
        validateFont(handler, t3FontVal, result);
    }

    return result;
}