Search in sources :

Example 6 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.

the class PDDefaultAppearanceString method processSetFontColor.

/**
 * Process the font color operator.
 *
 * This is assumed to be an RGB color.
 *
 * @param operands the color components
 * @throws IOException in case of the color components not matching
 */
private void processSetFontColor(List<COSBase> operands) throws IOException {
    PDColorSpace colorSpace;
    switch(operands.size()) {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            throw new IOException("Missing operands for set non stroking color operator " + Arrays.toString(operands.toArray()));
    }
    COSArray array = new COSArray();
    array.addAll(operands);
    setFontColor(new PDColor(array, colorSpace));
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) IOException(java.io.IOException) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 7 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.

the class ShadingPatternValidationProcess method checkColorSpace.

/**
 * Checks if the ColorSapce entry is consistent which rules of the PDF Reference and the ISO 190005-1:2005
 * Specification.
 *
 * This method is called by the validate method.
 *
 * @param context the preflight context.
 * @param page the page to check.
 * @param shadingRes the Shading pattern to check.
 * @throws ValidationException
 */
protected void checkColorSpace(PreflightContext context, PDPage page, PDShading shadingRes) throws ValidationException {
    try {
        PDColorSpace pColorSpace = shadingRes.getColorSpace();
        PreflightConfiguration config = context.getConfig();
        ColorSpaceHelperFactory csFact = config.getColorSpaceHelperFact();
        ColorSpaceHelper csh = csFact.getColorSpaceHelper(context, pColorSpace, ColorSpaceRestriction.NO_PATTERN);
        csh.validate();
    } catch (IOException e) {
        LOGGER.debug("Unable to get the color space", e);
        context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_UNKNOWN_COLOR_SPACE, e.getMessage()));
    }
}
Also used : PreflightConfiguration(org.apache.pdfbox.preflight.PreflightConfiguration) ColorSpaceHelperFactory(org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory) ColorSpaceHelper(org.apache.pdfbox.preflight.graphic.ColorSpaceHelper) IOException(java.io.IOException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 8 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.

the class SinglePageValidationProcess method validateColorSpaces.

/**
 * Check that all ColorSpace present in the Resource dictionary are conforming to the ISO 19005:2005-1
 * specification.
 *
 * @param context the preflight context.
 * @param page the page to check.
 */
protected void validateColorSpaces(PreflightContext context, PDPage page) {
    PDResources resources = page.getResources();
    if (resources != null) {
        PreflightConfiguration config = context.getConfig();
        ColorSpaceHelperFactory colorSpaceFactory = config.getColorSpaceHelperFact();
        for (COSName name : resources.getColorSpaceNames()) {
            try {
                PDColorSpace pdCS = resources.getColorSpace(name);
                ColorSpaceHelper csHelper = colorSpaceFactory.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.NO_RESTRICTION);
                csHelper.validate();
            } catch (IOException e) {
                // fixme: this code was previously in PDResources
                LOGGER.debug("Unable to create colorspace", e);
            }
        }
    }
}
Also used : PreflightConfiguration(org.apache.pdfbox.preflight.PreflightConfiguration) ColorSpaceHelperFactory(org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory) COSName(org.apache.pdfbox.cos.COSName) PDResources(org.apache.pdfbox.pdmodel.PDResources) ColorSpaceHelper(org.apache.pdfbox.preflight.graphic.ColorSpaceHelper) IOException(java.io.IOException) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 9 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.

the class DeviceColorSpaceHelper method processIndexedColorSpace.

/**
 * Indexed color space is authorized only if the BaseColorSpace is a DeviceXXX color space. In all other cases the
 * given list is updated with a ValidationError (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns
 * false.
 */
@Override
protected void processIndexedColorSpace(PDColorSpace colorSpace) {
    PDIndexed indexed = (PDIndexed) colorSpace;
    PDColorSpace baseColorSpace = indexed.getBaseColorSpace();
    ColorSpaces colorSpaces = ColorSpaces.valueOf(baseColorSpace.getName());
    switch(colorSpaces) {
        case Indexed:
        case I:
        case Pattern:
            context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, colorSpaces.getLabel() + " ColorSpace is forbidden"));
            break;
        default:
            processAllColorSpace(baseColorSpace);
    }
}
Also used : PDIndexed(org.apache.pdfbox.pdmodel.graphics.color.PDIndexed) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 10 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace 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)

Aggregations

PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)35 IOException (java.io.IOException)13 PDPattern (org.apache.pdfbox.pdmodel.graphics.color.PDPattern)9 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)8 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)8 COSArray (org.apache.pdfbox.cos.COSArray)7 COSBase (org.apache.pdfbox.cos.COSBase)6 COSName (org.apache.pdfbox.cos.COSName)6 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)5 ColorSpaceHelper (org.apache.pdfbox.preflight.graphic.ColorSpaceHelper)5 BufferedImage (java.awt.image.BufferedImage)4 WritableRaster (java.awt.image.WritableRaster)4 PDPageContentStream (org.apache.pdfbox.pdmodel.PDPageContentStream)4 PDIndexed (org.apache.pdfbox.pdmodel.graphics.color.PDIndexed)4 PDTilingPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern)4 PreflightConfiguration (org.apache.pdfbox.preflight.PreflightConfiguration)4 ColorSpaceHelperFactory (org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory)4 Paint (java.awt.Paint)3 Point (java.awt.Point)3 Rectangle2D (java.awt.geom.Rectangle2D)3