List of usage examples for org.apache.pdfbox.cos COSDictionary keySet
public Set<COSName> keySet()
From source file:at.gv.egiz.pdfas.lib.impl.pdfbox.placeholder.SignaturePlaceholderExtractor.java
License:EUPL
@Override public Map<String, PDFont> getFonts() { if (fonts == null) { // at least an empty map will be returned // TODO we should return null instead of an empty map fonts = new HashMap<String, PDFont>(); if (this.getResources() != null && this.getResources().getCOSDictionary() != null) { COSDictionary fontsDictionary = (COSDictionary) this.getResources().getCOSDictionary() .getDictionaryObject(COSName.FONT); if (fontsDictionary == null) { // ignore we do not want to set anything, never when creating a signature!!!!! //fontsDictionary = new COSDictionary(); //this.getResources().getCOSDictionary().setItem(COSName.FONT, fontsDictionary); } else { for (COSName fontName : fontsDictionary.keySet()) { COSBase font = fontsDictionary.getDictionaryObject(fontName); // data-000174.pdf contains a font that is a COSArray, looks to be an error in the // PDF, we will just ignore entries that are not dictionaries. if (font instanceof COSDictionary) { PDFont newFont = null; try { newFont = PDFontFactory.createFont((COSDictionary) font); } catch (IOException exception) { logger.error("error while creating a font", exception); }/*from ww w .j av a2 s. co m*/ if (newFont != null) { fonts.put(fontName.getName(), newFont); } } } } } } return fonts; }
From source file:at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignaturePlaceholderExtractor.java
License:EUPL
public Map<String, PDFont> getFonts() { if (fonts == null) { // at least an empty map will be returned // TODO we should return null instead of an empty map fonts = new HashMap<String, PDFont>(); if (this.getResources() != null && this.getResources().getCOSObject() != null) { COSDictionary fontsDictionary = (COSDictionary) this.getResources().getCOSObject() .getDictionaryObject(COSName.FONT); if (fontsDictionary == null) { // ignore we do not want to set anything, never when creating a signature!!!!! //fontsDictionary = new COSDictionary(); //this.getResources().getCOSDictionary().setItem(COSName.FONT, fontsDictionary); } else { for (COSName fontName : fontsDictionary.keySet()) { COSBase font = fontsDictionary.getDictionaryObject(fontName); // data-000174.pdf contains a font that is a COSArray, looks to be an error in the // PDF, we will just ignore entries that are not dictionaries. if (font instanceof COSDictionary) { PDFont newFont = null; try { newFont = PDFontFactory.createFont((COSDictionary) font); } catch (IOException exception) { logger.error("error while creating a font", exception); }/*w ww . j av a2 s .c o m*/ if (newFont != null) { fonts.put(fontName.getName(), newFont); } } } } } } return fonts; }
From source file:net.padaf.preflight.actions.ActionManagerFactory.java
License:Apache License
/** * This method extract actions from the given dictionary. An action is * identified by the following entries : * <UL>// ww w . jav a 2s.c o m * <li>A (Action) : Available in Annotations, Outline items * <li>OpenAction (OpenAction) : Available in the Catalog dictionary * <li>AA (Additional Action) : Available in the Catalog dictionary, * Annotations, Pages * </UL> * * If there are no action, an empty list is returned. * * @param dictionary * @param cDoc * @return * @throws ValidationException */ public final List<AbstractActionManager> getActions(COSDictionary dictionary, COSDocument cDoc) throws ValidationException { List<AbstractActionManager> result = new ArrayList<AbstractActionManager>(0); Map<COSObjectKey, Boolean> alreadyCreated = new HashMap<COSObjectKey, Boolean>(); COSBase aDict = dictionary.getDictionaryObject(DICTIONARY_KEY_ACTION); if (aDict != null) { callCreateAction(aDict, cDoc, result, alreadyCreated); } COSBase oaDict = dictionary.getDictionaryObject(DICTIONARY_KEY_OPEN_ACTION); if (oaDict != null) { if (!COSUtils.isArray(oaDict, cDoc)) { callCreateAction(oaDict, cDoc, result, alreadyCreated); } // else Nothing to do because of an array contains a Destination not an // action. } COSBase aa = dictionary.getDictionaryObject(DICTIONARY_KEY_ADDITIONAL_ACTION); if (aa != null) { COSDictionary aaDict = COSUtils.getAsDictionary(aa, cDoc); if (aaDict != null) { for (Object key : aaDict.keySet()) { COSName name = (COSName) key; callCreateAction(aaDict.getItem(name), cDoc, result, name.getName(), alreadyCreated); } } } return result; }
From source file:net.padaf.preflight.font.CompositeFontValidator.java
License:Apache License
/** * Check if all required fields are present in the PDF file to describe the * Font Descriptor./*ww w . j ava2s . c o m*/ * * @param handler * @param fontDescriptor * @param result */ protected boolean checkFontDescriptorMandatoryFields(PDFontDescriptorDictionary pdFontDesc) { boolean fname = false, flags = false, itangle = false, cheight = false; boolean fbbox = false, asc = false, desc = false, stemv = false; COSDictionary fontDescDictionary = pdFontDesc.getCOSDictionary(); for (Object key : fontDescDictionary.keySet()) { if (!(key instanceof COSName)) { this.fontContainer.addError(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_DICTIONARY_KEY_INVALID, "Invalid key in The font descriptor")); return false; } String cosName = ((COSName) key).getName(); if (cosName.equals(FONT_DICTIONARY_KEY_FONTNAME)) { fname = true; } if (cosName.equals(FONT_DICTIONARY_KEY_FLAGS)) { flags = true; } if (cosName.equals(FONT_DICTIONARY_KEY_ITALICANGLE)) { itangle = true; } if (cosName.equals(FONT_DICTIONARY_KEY_CAPHEIGHT)) { cheight = true; } if (cosName.equals(FONT_DICTIONARY_KEY_FONTBBOX)) { fbbox = true; } if (cosName.equals(FONT_DICTIONARY_KEY_ASCENT)) { asc = true; } if (cosName.equals(FONT_DICTIONARY_KEY_DESCENT)) { desc = true; } if (cosName.equals(FONT_DICTIONARY_KEY_STEMV)) { stemv = true; } } if (!(fname && flags && itangle && cheight && fbbox && asc && desc && stemv)) { this.fontContainer.addError( new ValidationError(ERROR_FONTS_DESCRIPTOR_INVALID, "Some mandatory fields are missing")); return false; } return true; }
From source file:net.padaf.preflight.font.Type3FontValidator.java
License:Apache License
/** * If the Resources entry is present, this method check its content. Only * fonts and Images are checked because this resource describes glyphs. REMARK * : The font and the image aren't validated because they will be validated by * an other ValidationHelper.// ww w . j a v a2 s. c o m * * @return */ private boolean checkResources() throws ValidationException { if (this.resources == null) { // ---- No resources dictionary. return true; } COSDocument cDoc = this.handler.getDocument().getDocument(); COSDictionary dictionary = COSUtils.getAsDictionary(this.resources, cDoc); if (dictionary == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources element isn't a dictionary")); return false; } COSBase cbImg = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_XOBJECT)); COSBase cbFont = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_FONT)); if (cbImg == null && cbFont == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED, "The Resources element doesn't have Glyph information")); return false; } if (cbImg != null) { // ---- the referenced objects must be present in the PDF file COSDictionary dicImgs = COSUtils.getAsDictionary(cbImg, cDoc); Set<COSName> keyList = dicImgs.keySet(); for (Object key : keyList) { COSBase item = dictionary.getItem((COSName) key); COSDictionary xObjImg = COSUtils.getAsDictionary(item, cDoc); if (xObjImg == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } if (!XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG .equals(xObjImg.getString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE)))) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } } } if (cbFont != null) { // ---- the referenced object must be present in the PDF file COSDictionary dicFonts = COSUtils.getAsDictionary(cbFont, cDoc); Set<COSName> keyList = dicFonts.keySet(); for (Object key : keyList) { COSBase item = dictionary.getItem((COSName) key); COSDictionary xObjFont = COSUtils.getAsDictionary(item, cDoc); if (xObjFont == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } if (!FONT_DICTIONARY_VALUE_FONT .equals(xObjFont.getString(COSName.getPDFName(DICTIONARY_KEY_TYPE)))) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } try { PDFont aFont = PDFontFactory.createFont(xObjFont); // FontContainer aContainer = this.handler.retrieveFontContainer(aFont); AbstractFontContainer aContainer = this.handler.getFont(aFont.getCOSObject()); // ---- another font is used in the Type3, check if the font is valid. if (aContainer.isValid() != State.VALID) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED, "The Resources dictionary of type 3 font contains invalid font")); return false; } } catch (IOException e) { throw new ValidationException("Unable to valid the Type3 : " + e.getMessage()); } } } List<ValidationError> errors = new ArrayList<ValidationError>(); ExtGStateContainer extGStates = new ExtGStateContainer(dictionary, cDoc); boolean res = extGStates.validateTransparencyRules(errors); for (ValidationError err : errors) { this.fontContainer.addError(err); } return res && validateShadingPattern(dictionary, errors); }
From source file:net.padaf.preflight.font.Type3FontValidator.java
License:Apache License
/** * This method check the Shading entry of the resource dictionary if exists. * //from www . j a v a 2 s. c o m * @param result * @return * @throws ValidationException */ protected boolean validateShadingPattern(COSDictionary dictionary, List<ValidationError> result) throws ValidationException { boolean res = true; if (dictionary != null) { COSDictionary shadings = (COSDictionary) dictionary.getDictionaryObject(PATTERN_KEY_SHADING); if (shadings != null) { for (COSName key : shadings.keySet()) { COSDictionary aShading = (COSDictionary) shadings.getDictionaryObject(key); ShadingPattern sp = new ShadingPattern(handler, aShading); List<ValidationError> lErrors = sp.validate(); if (lErrors != null && !lErrors.isEmpty()) { result.addAll(lErrors); res = false; } } } } return res; }
From source file:net.padaf.preflight.graphics.ExtGStateContainer.java
License:Apache License
/** * Create an instance of ExtGStateContainer using a Resource dictionary and a * COSDocument. This constructor initializes the listOfExtGState attribute * using the given Resource dictionary and the COSDocument. * //w w w .j av a 2 s. com * @param resources * a resource COSDictionary * @param cDoc * the COSDocument which contains the Resource dictionary * @throws ValidationException * thrown if a the Extended Graphic State isn't valid */ public ExtGStateContainer(COSDictionary resources, COSDocument cDoc) throws ValidationException { this.cDoc = cDoc; COSBase egsEntry = resources.getItem(COSName.getPDFName(TRANPARENCY_DICTIONARY_KEY_EXTGSTATE)); COSDictionary extGStates = COSUtils.getAsDictionary(egsEntry, cDoc); if (extGStates != null) { for (Object object : extGStates.keySet()) { COSName key = (COSName) object; if (key.getName().matches(TRANPARENCY_DICTIONARY_KEY_EXTGSTATE_ENTRY_REGEX)) { COSBase gsBase = extGStates.getItem(key); COSDictionary gsDict = COSUtils.getAsDictionary(gsBase, cDoc); if (gsDict == null) { throw new ValidationException("The Extended Graphics State dictionary is invalid"); } this.listOfExtGState.add(gsDict); } } } //else if there are no ExtGState, the list will be empty. }
From source file:net.padaf.preflight.graphics.XObjFormValidator.java
License:Apache License
/** * This method check the Shading entry of the resource dictionary if exists. * To process this validation, an instance of ShadinPattern is used. * //from w ww. j a v a 2 s .c om * @param result * the list of error to update if the validation fails * @return true if the validation succeed, false otherwise. * @throws ValidationException */ protected boolean validateShadingPattern(List<ValidationError> result) throws ValidationException { PDResources resources = this.pdXObj.getResources(); boolean res = true; if (resources != null) { COSDictionary shadings = (COSDictionary) resources.getCOSDictionary() .getDictionaryObject(PATTERN_KEY_SHADING); if (shadings != null) { for (Object key : shadings.keySet()) { COSDictionary aShading = (COSDictionary) shadings.getDictionaryObject((COSName) key); ShadingPattern sp = new ShadingPattern(handler, aShading); List<ValidationError> lErrors = sp.validate(); if (lErrors != null && !lErrors.isEmpty()) { result.addAll(lErrors); res = false; } } } } return res; }
From source file:net.padaf.preflight.helpers.PagesValidationHelper.java
License:Apache License
/** * This method check the Shading entry of the resource dictionary if exists. * /* w ww. j a v a 2 s . c om*/ * @param page * @param handler * @param result * @return * @throws ValidationException */ protected boolean validateShadingPattern(PDPage page, DocumentHandler handler, List<ValidationError> result) throws ValidationException { PDResources resources = page.getResources(); COSDictionary shadings = (COSDictionary) resources.getCOSDictionary() .getDictionaryObject(PATTERN_KEY_SHADING); boolean res = true; if (shadings != null) { for (Object key : shadings.keySet()) { COSDictionary aShading = (COSDictionary) shadings.getDictionaryObject((COSName) key); ShadingPattern sp = new ShadingPattern(handler, aShading); List<ValidationError> lErrors = sp.validate(); if (lErrors != null && !lErrors.isEmpty()) { result.addAll(lErrors); res = false; } } } return res; }
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./*from w w w .j ava 2 s . c o 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")); } } }