The following code snippets retrieve metadata from a file using a regular expression.
Old API
Regexpattern=newRegex("author|company",RegexOptions.IgnoreCase);// This method works with document formats onlyMetadataPropertyCollectionproperties=SearchFacade.ScanDocument(@"D:\input.docx",pattern);for(inti=0;i<properties.Count;i++){Console.WriteLine(properties[i]);}
New API
Regexpattern=newRegex("author|company",RegexOptions.IgnoreCase);using(Metadatametadata=newMetadata(@"D:\input.docx")){// This method searches for properties across all metadata packages and works with all supported formatsvarproperties=metadata.FindProperties(p=>pattern.IsMatch(p.Name)||pattern.IsMatch(p.Value.ToString()));foreach(varpropertyinproperties){Console.WriteLine("{0} = {1}",property.Name,property.Value);}}
Replacing Metadata using Regular Expressions
The following code snippets show how to replace metadata properties using a regular expression.
Old API
Regexpattern=newRegex("^author|company$",RegexOptions.IgnoreCase);stringreplaceValue="Aspose";// This method works with document formats onlySearchFacade.ReplaceInDocument(@"D:\input.docx",pattern,replaceValue,@"D:\output.docx");
New API
varpattern=newRegex("^author|company$",RegexOptions.IgnoreCase);varreplaceValue=newPropertyValue("Aspose");using(Metadatametadata=newMetadata(@"D:\input.docx")){// This method updates writable properties across all metadata packages and works with all supported formatsmetadata.UpdateProperties(p=>pattern.IsMatch(p.Name),replaceValue);metadata.Save(@"D:\output.docx");}
Comparing Metadata
Get the Difference between Two Sets of Metadata Properties
The code samples below demonstrate how to get the difference of two collections of properties extracted from different files.
// The predicate to extract metadata properties we want to compare// In this code sample we retrieve built-in document propertiesCommon.Func<MetadataProperty,bool>predicate=p=>p.Tags.Contains(Tags.Document.BuiltIn);using(Metadatametadata1=newMetadata(@"D:\input1.docx"))using(Metadatametadata2=newMetadata(@"D:\input2.docx")){// You can implement your own equality comparer for metadata properties// In this code sample, we just use the old one that worked in previous versions of GroupDocs.Metadatavardifference=metadata1.FindProperties(predicate).Except(metadata2.FindProperties(predicate),newMetadataPropertyEqualityComparer());foreach(varpropertyindifference){Console.WriteLine("{0} = {1}",property.Name,property.Value);}}
Get the Intersection of Two Sets of Metadata Properties
The code samples below demonstrate how to get the intersection of two collections of properties extracted from different files.
// The predicate to extract metadata properties we want to compare// In this code sample we retrieve TIFF/EXIF tagsCommon.Func<MetadataProperty,bool>predicate=p=>pisTiffTag;using(Metadatametadata1=newMetadata(@"D:\input1.jpg"))using(Metadatametadata2=newMetadata(@"D:\input2.jpg")){// You can implement your own equality comparer for metadata properties// In this code sample, we just use the old one that worked in previous versions of GroupDocs.Metadatavarintersection=metadata1.FindProperties(predicate).Intersect(metadata2.FindProperties(predicate),newMetadataPropertyEqualityComparer());foreach(varpropertyinintersection){Console.WriteLine("{0} = {1}",property.Name,property.Value);}}
Exporting Metadata
Export Metadata Properties to CSV
The following code snippets show how to export metadata properties to a CSV file.
Old API
stringoutputPath=@"D:\metadata.csv";// export to csvbyte[]content=ExportFacade.ExportToCsv(@"D:\input.eml");// write data to the fileFile.WriteAllBytes(outputPath,content);
New API
using(Metadatametadata=newMetadata(@"D:\input.eml")){// We use a predicate that extracts all metadata propertiesvarproperties=metadata.FindProperties(p=>true);conststringdelimiter=";";StringBuilderbuilder=newStringBuilder();builder.AppendFormat("Name{0}Value",delimiter);builder.AppendLine();foreach(varpropertyinproperties){builder.AppendFormat(@"""{0}""{1}""{2}""",property.Name,delimiter,FormatValue(property.Value));builder.AppendLine();}File.WriteAllText(@"D:\metadata.csv",builder.ToString());}
Note
The implementation of the FormatValue method used in the code sample above can be different depending on your task so we just omitted it here. Please see the full code of the method in the sample project on GitHub.
Using the Replace API
Update Values with IReplaceHandler
The code samples below show how to update metadata properties using a custom filter.
Old API (Implementation of IReplaceHandler)
/// <summary>/// This class updates author to 'Jack London'/// </summary>publicclassAuthorReplaceHandler:IReplaceHandler<MetadataProperty>{publicAuthorReplaceHandler(stringoutputPath){this.OutputPath=outputPath;}publicstringOutputPath{get;privateset;}publicboolHandle(MetadataPropertyproperty){// if property name is 'author'if(property.Name.ToLower()=="author"){// update property valueproperty.Value=newPropertyValue("Jack London");// and mark property as updatedreturntrue;}// ignore all other propertiesreturnfalse;}}
In the new API there is no need to use the IReplaceHandler interface since you have full control over property filters using the GrooupDocs.Metadata search engine.