Search in sources :

Example 41 with ValidationError

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

the class ICCProfileWrapper method searchFirstICCProfile.

/**
 * This method read all outputIntent dictionary until on of them have a destOutputProfile stream. This stream is
 * parsed and is used to create a IccProfileWrapper.
 *
 * @param context
 * @return an instance of ICCProfileWrapper or null if there are no DestOutputProfile
 * @throws ValidationException
 *             if an IOException occurs during the DestOutputProfile parsing
 */
private static ICCProfileWrapper searchFirstICCProfile(PreflightContext context) throws ValidationException {
    PreflightDocument document = context.getDocument();
    PDDocumentCatalog catalog = document.getDocumentCatalog();
    COSBase cBase = catalog.getCOSObject().getItem(COSName.getPDFName(DOCUMENT_DICTIONARY_KEY_OUTPUT_INTENTS));
    COSArray outputIntents = COSUtils.getAsArray(cBase, document.getDocument());
    for (int i = 0; outputIntents != null && i < outputIntents.size(); ++i) {
        COSDictionary outputIntentDict = COSUtils.getAsDictionary(outputIntents.get(i), document.getDocument());
        COSBase destOutputProfile = outputIntentDict.getItem(OUTPUT_INTENT_DICTIONARY_KEY_DEST_OUTPUT_PROFILE);
        if (destOutputProfile != null) {
            try {
                COSStream stream = COSUtils.getAsStream(destOutputProfile, document.getDocument());
                if (stream != null) {
                    InputStream is = stream.createInputStream();
                    try {
                        return new ICCProfileWrapper(ICC_Profile.getInstance(is));
                    } finally {
                        is.close();
                    }
                }
            } catch (IllegalArgumentException e) {
                context.addValidationError(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID, "DestOutputProfile isn't a valid ICCProfile. Caused by : " + e.getMessage(), e));
            } catch (IOException e) {
                context.addValidationError(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID, "Unable to parse the ICCProfile. Caused by : " + e.getMessage(), e));
            }
        }
    }
    return null;
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) COSArray(org.apache.pdfbox.cos.COSArray) COSDictionary(org.apache.pdfbox.cos.COSDictionary) InputStream(java.io.InputStream) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Example 42 with ValidationError

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

the class StandardColorSpaceHelper method processDefaultColorSpace.

/**
 * Look up in the closest PDResources objects if there are a default ColorSpace. If there are, check that is a
 * authorized ColorSpace.
 *
 * @param colorSpace
 * @return true if the default colorspace is a right one, false otherwise.
 */
protected boolean processDefaultColorSpace(PDColorSpace colorSpace) {
    boolean result = false;
    // get default color space
    PreflightPath vPath = context.getValidationPath();
    PDResources resources = vPath.getClosestPathElement(PDResources.class);
    if (resources != null) {
        PDColorSpace defaultCS = null;
        try {
            if (colorSpace.getName().equals(ColorSpaces.DeviceCMYK.getLabel()) && resources.hasColorSpace(COSName.DEFAULT_CMYK)) {
                defaultCS = resources.getColorSpace(COSName.DEFAULT_CMYK);
            } else if (colorSpace.getName().equals(ColorSpaces.DeviceRGB.getLabel()) && resources.hasColorSpace(COSName.DEFAULT_RGB)) {
                defaultCS = resources.getColorSpace(COSName.DEFAULT_RGB);
            } else if (colorSpace.getName().equals(ColorSpaces.DeviceGray.getLabel()) && resources.hasColorSpace(COSName.DEFAULT_GRAY)) {
                defaultCS = resources.getColorSpace(COSName.DEFAULT_GRAY);
            }
        } catch (IOException e) {
            context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read default color space : " + e.getMessage(), e));
        }
        if (defaultCS != null) {
            // defaultCS is valid if the number of errors hasn't changed
            int nbOfErrors = context.getDocument().getResult().getErrorsList().size();
            processAllColorSpace(defaultCS);
            int newNbOfErrors = context.getDocument().getResult().getErrorsList().size();
            result = (nbOfErrors == newNbOfErrors);
        }
    }
    return result;
}
Also used : PDResources(org.apache.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PreflightPath(org.apache.pdfbox.preflight.PreflightPath) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 43 with ValidationError

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

the class MetadataValidationProcess method checkStreamFilterUsage.

/**
 * Check if metadata dictionary has no stream filter
 *
 * @param doc the document to check.
 * @return the list of validation errors.
 */
protected List<ValidationError> checkStreamFilterUsage(PDDocument doc) {
    List<ValidationError> ve = new ArrayList<>();
    List<?> filters = doc.getDocumentCatalog().getMetadata().getFilters();
    if (filters != null && !filters.isEmpty()) {
        ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_MAIN, "Using stream filter on metadata dictionary is forbidden"));
    }
    return ve;
}
Also used : ArrayList(java.util.ArrayList) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 44 with ValidationError

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

the class MetadataValidationProcess method checkThumbnail.

private void checkThumbnail(ThumbnailType tb, PreflightContext ctx) {
    byte[] binImage;
    try {
        binImage = DatatypeConverter.parseBase64Binary(tb.getImage());
    } catch (IllegalArgumentException e) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, "xapGImg:image is not correct base64 encoding"));
        return;
    }
    if (!hasJpegMagicNumber(binImage)) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, "xapGImg:image decoded base64 content is not in JPEG format"));
        return;
    }
    BufferedImage bim;
    try {
        bim = ImageIO.read(new ByteArrayInputStream(binImage));
    } catch (IOException e) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, e.getMessage(), e));
        return;
    }
    if (!"JPEG".equals(tb.getFormat())) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, "xapGImg:format must be 'JPEG'"));
    }
    if (bim.getHeight() != tb.getHeight()) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, "xapGImg:height does not match the actual base64-encoded thumbnail image data"));
    }
    if (bim.getWidth() != tb.getWidth()) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, "xapGImg:witdh does not match the actual base64-encoded thumbnail image data"));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 45 with ValidationError

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

the class PageTreeValidationProcess method validate.

@Override
public void validate(PreflightContext context) throws ValidationException {
    PDDocumentCatalog catalog = context.getDocument().getDocumentCatalog();
    if (catalog != null) {
        COSDictionary catalogDict = catalog.getCOSObject();
        if (!(catalogDict.getDictionaryObject(COSName.PAGES) instanceof COSDictionary)) {
            addValidationError(context, new ValidationError(ERROR_PDF_PROCESSING_MISSING, "/Pages dictionary entry is missing in document catalog"));
            return;
        }
        int numPages = context.getDocument().getNumberOfPages();
        for (int i = 0; i < numPages; i++) {
            context.setCurrentPageNumber(i);
            validatePage(context, context.getDocument().getPage(i));
            context.setCurrentPageNumber(null);
        }
    } else {
        context.addValidationError(new ValidationError(ERROR_SYNTAX_NOCATALOG, "There are no Catalog entry in the Document"));
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

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