The easiest way to remove metadata properties from a file is to use corresponding tags that allow you to locate the desired properties across all metadata packages. But sometimes it’s necessary to remove metadata entries having a particular value. Using the GroupDocs.Metadata search engine you can find and remove properties satisfying a specification that can be as complex as you need.
The following example demonstrates how to remove specific metadata properties using a combination of criteria.
publicclassRemoveMetadataProperties{publicstaticvoidrun(){// Constants.InputDocx is an absolute or relative path to your document. Ex: @"C:\Docs\source.docx"try(Metadatametadata=newMetadata(Constants.InputDocx)){// Remove all the properties satisfying the predicate:// property contains the name of the document author OR// it refers to the last editor OR// the property value is a string that is equal to the given string "John" (to remove any mentions of John from the detected metadata)intaffected=metadata.removeProperties(newContainsTagSpecification(Tags.getPerson().getCreator()).or(newContainsTagSpecification(Tags.getPerson().getEditor())).or(newOfTypeSpecification(MetadataPropertyType.String).and(newRemoveMetadataProperties().newWithValueSpecification("John"))));System.out.println(String.format("Properties removed: %s",affected));metadata.save(Constants.OutputDocx);}}// Define your own specifications to filter metadata propertiespublicclassWithValueSpecificationextendsSpecification{publicWithValueSpecification(Objectvalue){setValue(value);}publicfinalObjectgetValue(){returnauto_Value;}privatevoidsetValue(Objectvalue){auto_Value=value;}privateObjectauto_Value;publicbooleanisSatisfiedBy(MetadataPropertycandidate){returncandidate.getValue().getRawValue().equals(getValue());}}}
As a result of execution of the code snippet above, we remove all mentions of the document author/editor and all other string metadata properties containing the name John.
For more information on supported features of the GroupDocs.Metadata search engine please refer to the following articles: