List of usage examples for org.apache.pdfbox.cos COSName getPDFName
public static COSName getPDFName(String aName)
From source file:net.padaf.preflight.helpers.GraphicsValidationHelper.java
License:Apache License
@Override public List<ValidationError> innerValidate(DocumentHandler handler) throws ValidationException { List<ValidationError> result = new ArrayList<ValidationError>(0); PDDocument pdfDoc = handler.getDocument(); // ---- Checks all XObjects COSDocument cDoc = pdfDoc.getDocument(); List<?> lCOSObj = cDoc.getObjects(); for (Object o : lCOSObj) { COSObject cObj = (COSObject) o;//from w w w . j a v a2 s. co m COSBase cBase = cObj.getObject(); if (cBase instanceof COSDictionary) { COSDictionary dic = (COSDictionary) cBase; String type = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE)); if (type != null && DICTIONARY_KEY_XOBJECT.equals(type)) { result.addAll(validateXObject(handler, cObj)); } else if (type != null && DICTIONARY_KEY_PATTERN.equals(type)) { result.addAll(validatePattern(handler, cObj)); } } } return result; }
From source file:net.padaf.preflight.helpers.GraphicsValidationHelper.java
License:Apache License
public List<ValidationError> validateXObject(DocumentHandler handler, COSObject cObj) throws ValidationException { XObjectValidator xObjVal = null;//from w ww . j av a2 s.co m // ---- According to the XObject subtype, the validation isn't processed by // the same Validator COSStream dic = (COSStream) cObj.getObject(); String subtype = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE)); if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG.equals(subtype)) { xObjVal = new XObjImageValidator(handler, dic); } else if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_FORM.equals(subtype)) { xObjVal = new XObjFormValidator(handler, dic); } else if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_POSTSCRIPT.equals(subtype)) { xObjVal = new XObjPostscriptValidator(handler, dic); } else { throw new ValidationException("Invalid XObject subtype"); } return xObjVal.validate(); }
From source file:net.padaf.preflight.helpers.StreamValidationHelper.java
License:Apache License
/** * This method checks if one of declared Filter is LZWdecode. If LZW is found, * the result list is updated with an error code. * // w ww. j a v a 2s . c o m * @param stream * @param handler * @param result */ protected void checkFilters(COSStream stream, DocumentHandler handler, List<ValidationError> result) { COSDocument cDoc = handler.getDocument().getDocument(); COSBase bFilter = stream.getItem(COSName.getPDFName(STREAM_DICTIONARY_KEY_FILTER)); if (bFilter != null) { if (COSUtils.isArray(bFilter, cDoc)) { COSArray afName = (COSArray) bFilter; for (int i = 0; i < afName.size(); ++i) { if (!FilterHelper.isAuthorizedFilter(afName.getString(i), result)) { return; } } } else if (bFilter instanceof COSName) { String fName = ((COSName) bFilter).getName(); if (!FilterHelper.isAuthorizedFilter(fName, result)) { return; } } else { // ---- The filter type is invalid result.add(new ValidationError(ERROR_SYNTAX_STREAM_INVALID_FILTER, "Filter should be a Name or an Array")); } } // else Filter entry is optional }
From source file:net.padaf.preflight.helpers.StreamValidationHelper.java
License:Apache License
protected void checkStreamLength(DocumentHandler handler, COSObject cObj, List<ValidationError> result) throws ValidationException { COSStream streamObj = (COSStream) cObj.getObject(); int length = streamObj.getInt(COSName.getPDFName(STREAM_DICTIONARY_KEY_LENGHT)); InputStream ra = null;//from w w w.j ava2 s . c om try { ra = handler.getSource().getInputStream(); Integer offset = (Integer) handler.getDocument().getDocument().getXrefTable() .get(new COSObjectKey(cObj)); // ---- go to the beginning of the object long skipped = 0; while (skipped != offset) { long curSkip = ra.skip(offset - skipped); if (curSkip < 0) { throw new ValidationException("Unable to skip bytes in the PDFFile to check stream length"); } skipped += curSkip; } // ---- go to the stream key word if (readUntilStream(ra)) { int c = ra.read(); if (c == '\r') { ra.read(); } // else c is '\n' no more character to read // ---- Here is the true beginning of the Stream Content. // ---- Read the given length of bytes and check the 10 next bytes // ---- to see if there are endstream. byte[] buffer = new byte[1024]; int nbBytesToRead = length; do { int cr = 0; if (nbBytesToRead > 1024) { cr = ra.read(buffer, 0, 1024); } else { cr = ra.read(buffer, 0, nbBytesToRead); } if (cr == -1) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); return; } else { nbBytesToRead = nbBytesToRead - cr; } } while (nbBytesToRead > 0); int len = "endstream".length() + 2; byte[] buffer2 = new byte[len]; for (int i = 0; i < len; ++i) { buffer2[i] = (byte) ra.read(); } // ---- check the content of 10 last characters String endStream = new String(buffer2); if (buffer2[0] == '\r' && buffer2[1] == '\n') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else if (buffer2[0] == '\r' && buffer2[1] == 'e') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else if (buffer2[0] == '\n' && buffer2[1] == 'e') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } catch (IOException e) { throw new ValidationException("Unable to read a stream to validate it due to : " + e.getMessage(), e); } finally { if (ra != null) { IOUtils.closeQuietly(ra); } } }
From source file:net.padaf.preflight.helpers.TrailerValidationHelper.java
License:Apache License
/** * Return true if the ID of the first dictionary is the same as the id of the * last dictionary Return false otherwise. * /*from w w w . j av a2 s .com*/ * @param first * @param last * @return */ protected boolean compareIds(COSDictionary first, COSDictionary last, COSDocument doc) { COSBase idFirst = first.getItem(COSName.getPDFName(TRAILER_DICTIONARY_KEY_ID)); COSBase idLast = last.getItem(COSName.getPDFName(TRAILER_DICTIONARY_KEY_ID)); if (idFirst == null || idLast == null) { return false; } // ---- cast two COSBase to COSArray. COSArray af = COSUtils.getAsArray(idFirst, doc); COSArray al = COSUtils.getAsArray(idLast, doc); // ---- if one COSArray is null, the PDF/A isn't valid if ((af == null) || (al == null)) { return false; } // ---- compare both arrays boolean isEqual = true; for (Object of : af.toList()) { boolean oneIsEquals = false; for (Object ol : al.toList()) { // ---- according to PDF Reference 1-4, ID is an array containing two // strings if (!oneIsEquals) oneIsEquals = ((COSString) ol).getString().equals(((COSString) of).getString()); } isEqual = isEqual && oneIsEquals; } return isEqual; }
From source file:net.padaf.preflight.helpers.TrailerValidationHelper.java
License:Apache License
/** * check if all keys are authorized in a trailer dictionary and if the type is * valid./* ww w. j a v a2s . co m*/ * * @param trailer * @param lErrors */ protected void checkMainTrailer(COSDocument doc, COSDictionary trailer, List<ValidationError> lErrors) { boolean id = false; boolean root = false; boolean size = false; boolean prev = false; boolean info = false; boolean encrypt = false; for (Object key : trailer.keySet()) { if (!(key instanceof COSName)) { lErrors.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_DICTIONARY_KEY_INVALID, "Invalid key in The trailer dictionary")); return; } String cosName = ((COSName) key).getName(); if (cosName.equals(TRAILER_DICTIONARY_KEY_ENCRYPT)) { encrypt = true; } if (cosName.equals(TRAILER_DICTIONARY_KEY_SIZE)) { size = true; } if (cosName.equals(TRAILER_DICTIONARY_KEY_PREV)) { prev = true; } if (cosName.equals(TRAILER_DICTIONARY_KEY_ROOT)) { root = true; } if (cosName.equals(TRAILER_DICTIONARY_KEY_INFO)) { info = true; } if (cosName.equals(TRAILER_DICTIONARY_KEY_ID)) { id = true; } } // ---- PDF/A Trailer dictionary must contain the ID key if (!id) { lErrors.add(new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_MISSING_ID, "The trailer dictionary doesn't contain ID")); } else { COSBase trailerId = trailer.getItem(COSName.getPDFName(TRAILER_DICTIONARY_KEY_ID)); if (!COSUtils.isArray(trailerId, doc)) { lErrors.add( new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains an id but it isn't an array")); } } // ---- PDF/A Trailer dictionary mustn't contain the Encrypt key if (encrypt) { lErrors.add(new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_ENCRYPT, "The trailer dictionary contains Encrypt")); } // ---- PDF Trailer dictionary must contain the Size key if (!size) { lErrors.add(new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_MISSING_SIZE, "The trailer dictionary doesn't contain Size")); } else { COSBase trailerSize = trailer.getItem(COSName.getPDFName(TRAILER_DICTIONARY_KEY_SIZE)); if (!COSUtils.isInteger(trailerSize, doc)) { lErrors.add( new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains a size but it isn't an integer")); } } // ---- PDF Trailer dictionary must contain the Root key if (!root) { lErrors.add(new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_MISSING_ROOT, "The trailer dictionary doesn't contain Root")); } else { COSBase trailerRoot = trailer.getItem(COSName.getPDFName(TRAILER_DICTIONARY_KEY_ROOT)); if (!COSUtils.isDictionary(trailerRoot, doc)) { lErrors.add( new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains a root but it isn't a dictionary")); } } // ---- PDF Trailer dictionary may contain the Prev key if (prev) { COSBase trailerPrev = trailer.getItem(COSName.getPDFName(TRAILER_DICTIONARY_KEY_PREV)); if (!COSUtils.isInteger(trailerPrev, doc)) { lErrors.add( new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains a prev but it isn't an integer")); } } // ---- PDF Trailer dictionary may contain the Info key if (info) { COSBase trailerInfo = trailer.getItem(COSName.getPDFName(TRAILER_DICTIONARY_KEY_INFO)); if (!COSUtils.isDictionary(trailerInfo, doc)) { lErrors.add( new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains an info but it isn't a dictionary")); } } }
From source file:net.padaf.preflight.helpers.TrailerValidationHelper.java
License:Apache License
/** * According to the PDF Reference, A linearized PDF contain a dictionary as * first object (linearized dictionary) and only this one in the first * section.//from w w w .j ava 2 s . co m * * @param document * @return */ protected COSDictionary isLinearizedPdf(PDDocument document) { // ---- Get Ref to obj COSDocument cDoc = document.getDocument(); List<?> lObj = cDoc.getObjects(); for (Object object : lObj) { COSBase curObj = ((COSObject) object).getObject(); if (curObj instanceof COSDictionary && ((COSDictionary) curObj).keySet().contains(COSName.getPDFName(DICTIONARY_KEY_LINEARIZED))) { return (COSDictionary) curObj; } } return null; }
From source file:net.padaf.preflight.utils.ContentStreamEngine.java
License:Apache License
/** * This method validates if the ColorSpace used by the InlinedImage is * consistent with the color space defined in OutputIntent dictionaries. * /*from www . j av a2s . c om*/ * @param operator * the InlinedImage object (BI to EI) * @throws ContentStreamException */ protected void validImageColorSpace(PDFOperator operator) throws ContentStreamException, IOException { COSDictionary dict = operator.getImageParameters().getDictionary(); COSDocument doc = this.documentHandler.getDocument().getDocument(); COSBase csInlinedBase = dict.getItem(COSName.getPDFName(STREAM_DICTIONARY_KEY_COLOR_SPACE)); ColorSpaceHelper csHelper = null; if (csInlinedBase != null) { if (COSUtils.isString(csInlinedBase, doc)) { // ---- In InlinedImage only DeviceGray/RGB/CMYK and restricted Indexed // color spaces // are allowed. String colorSpace = COSUtils.getAsString(csInlinedBase, doc); ColorSpaces cs = null; try { cs = ColorSpaces.valueOf(colorSpace); } catch (IllegalArgumentException e) { // ---- The color space is unknown. // ---- Try to access the resources dictionary, the color space can be // a reference. PDColorSpace pdCS = (PDColorSpace) this.getColorSpaces().get(colorSpace); if (pdCS != null) { cs = ColorSpaces.valueOf(pdCS.getName()); csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(pdCS, documentHandler, ColorSpaceRestriction.ONLY_DEVICE); } } if (cs == null) { throwContentStreamException("The ColorSpace is unknown", ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY); } } if (csHelper == null) { csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(csInlinedBase, documentHandler, ColorSpaceRestriction.ONLY_DEVICE); } List<ValidationError> errors = new ArrayList<ValidationError>(); try { if (!csHelper.validate(errors)) { ValidationError ve = errors.get(0); throwContentStreamException(ve.getDetails(), ve.getErrorCode()); } } catch (ValidationException e) { throw new IOException(e.getMessage()); } } }
From source file:net.padaf.preflight.utils.ContentStreamEngine.java
License:Apache License
/** * This method validates if the ColorSpace used as operand is consistent with * the color space defined in OutputIntent dictionaries. * //from www. j a va 2 s . c o m * @param operator * @param arguments * @throws IOException */ protected void checkSetColorSpaceOperators(PDFOperator operator, List<?> arguments) throws IOException { if (!("CS".equals(operator.getOperation()) || "cs".equals(operator.getOperation()))) { return; } String colorSpaceName = null; if (arguments.get(0) instanceof String) { colorSpaceName = (String) arguments.get(0); } else if (arguments.get(0) instanceof COSString) { colorSpaceName = ((COSString) arguments.get(0)).toString(); } else if (arguments.get(0) instanceof COSName) { colorSpaceName = ((COSName) arguments.get(0)).getName(); } else { throwContentStreamException("The operand doesn't have the expected type", ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY); } ColorSpaceHelper csHelper = null; ColorSpaces cs = null; try { cs = ColorSpaces.valueOf(colorSpaceName); } catch (IllegalArgumentException e) { // ---- The color space is unknown. // ---- Try to access the resources dictionary, the color space can be a // reference. PDColorSpace pdCS = (PDColorSpace) this.getColorSpaces().get(colorSpaceName); if (pdCS != null) { cs = ColorSpaces.valueOf(pdCS.getName()); csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(pdCS, documentHandler, ColorSpaceRestriction.NO_RESTRICTION); } } if (cs == null) { throwContentStreamException("The ColorSpace is unknown", ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY); } if (csHelper == null) { csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(COSName.getPDFName(colorSpaceName), documentHandler, ColorSpaceRestriction.NO_RESTRICTION); } List<ValidationError> errors = new ArrayList<ValidationError>(); try { if (!csHelper.validate(errors)) { ValidationError ve = errors.get(0); throwContentStreamException(ve.getDetails(), ve.getErrorCode()); } } catch (ValidationException e) { // throw new IOException(e.getMessage(), e); java 6 throw new IOException(e.getMessage()); } }
From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java
License:Apache License
private Set<COSObject> copyAnnotations(PDPage page) throws IOException { COSArray annots = (COSArray) page.getCOSObject().getDictionaryObject(COSName.ANNOTS); Set<COSObject> fields = Collections.emptySet(); if (annots != null) { fields = new TreeSet<COSObject>(new CompareFields()); for (Object annot1 : annots) { Collection<COSName> exclude = new ArrayList<COSName>(); exclude.add(COSName.P);// ww w.j av a 2s.com if (annot1 instanceof COSObject) { COSObject annot = (COSObject) annot1; COSObject fieldObject = annot; COSDictionary field = (COSDictionary) fieldObject.getObject(); COSObject parent; while ((parent = (COSObject) field.getItem(COSName.PARENT)) != null) { fieldObject = parent; field = (COSDictionary) fieldObject.getObject(); } fields.add(fieldObject); if (((COSDictionary) annot.getObject()).getItem(COSName.getPDFName("StructParent")) != null) { exclude.add(COSName.PARENT); } } PDFObject clonedAnnot = (PDFObject) cloneForNewDocument(annot1, annot1, exclude); if (clonedAnnot instanceof PDFDictionary) { clonedAnnot.setParent(targetPage); updateAnnotationLink((PDFDictionary) clonedAnnot); } targetPage.addAnnotation(clonedAnnot); } } return fields; }