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;
}
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;
}
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;
}
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"));
}
}
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"));
}
}
Aggregations