Using the GroupDocs.Metadata search engine you can extract desired metadata properties from files of different types. You don’t need to worry about the exact file format and metadata standards it can deal with. The same code will work for all supported formats in the same way. Most commonly used metadata properties are marked with tags that allow searching them across all supported files in various metadata packages. All tags defined in GroupDocs.Metadata are divided into categories that make it easier to find a required tag. The code sample below demonstrates some advanced usage of tags, categories and other attributes of metadata properties.
Load a file to be searched for metadata properties
Make up a predicate to examine all extracted metadata properties
publicclassExtractingMetadata{publicstaticvoidrun(){Filefolder=newFile(Constants.InputPath);for(Filefile:folder.listFiles()){try(Metadatametadata=newMetadata(file.getAbsolutePath())){if(metadata.getFileFormat()!=FileFormat.Unknown&&!metadata.getDocumentInfo().isEncrypted()){System.out.println();System.out.println(file.getName());// Fetch all metadata properties that fall into a particular categoryIReadOnlyList<MetadataProperty>properties=metadata.findProperties(newFallsIntoCategorySpecification(Tags.getContent()));System.out.println("The metadata properties describing some characteristics of the file content: title, keywords, language, etc.");for(MetadataPropertyproperty:properties){System.out.println(String.format("Property name: %s, Property value: %s",property.getName(),property.getValue()));}// Fetch all properties having a specific type and valueintyear=Calendar.getInstance().get(Calendar.YEAR);properties=metadata.findProperties(newOfTypeSpecification(MetadataPropertyType.DateTime).and(newExtractingMetadata().newYearMatchSpecification(year)));System.out.println("All datetime properties with the year value equal to the current year");for(MetadataPropertyproperty:properties){System.out.println(String.format("Property name: %s, Property value: %s",property.getName(),property.getValue()));}// Fetch all properties whose names match the specified regexPatternpattern=Pattern.compile("^author|company|(.+date.*)$",Pattern.CASE_INSENSITIVE);properties=metadata.findProperties(newExtractingMetadata().newRegexSpecification(pattern));System.out.println(String.format("All properties whose names match the following regex: %s",pattern.pattern()));for(MetadataPropertyproperty:properties){System.out.println(String.format("Property name: %s, Property value: %s",property.getName(),property.getValue()));}}}}}// Define your own specifications to filter metadata propertiespublicclassYearMatchSpecificationextendsSpecification{publicYearMatchSpecification(intyear){setValue(year);}publicfinalintgetValue(){returnauto_Value;}privatevoidsetValue(intvalue){auto_Value=value;}privateintauto_Value;publicbooleanisSatisfiedBy(MetadataPropertycandidate){Datedate=candidate.getValue().toClass(Date.class);if(date!=null){Calendarcalendar=Calendar.getInstance();calendar.setTime(date);returngetValue()==calendar.get(Calendar.YEAR);}returnfalse;}}publicclassRegexSpecificationextendsSpecification{privatePatternpattern;publicRegexSpecification(Patternpattern){this.pattern=pattern;}@OverridepublicbooleanisSatisfiedBy(MetadataPropertymetadataProperty){Matchermatcher=pattern.matcher(metadataProperty.getName());returnmatcher.find();}}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: