Search code examples
c#photoshop-scriptxmpmetadata-extractor

Getting custom XMP metadata using metadata-extractor in c#


I am writing some XMP metadata using a photoshop .jsx script using :

var xmp = new XMPMeta( activeDocument.xmpMetadata.rawData); XMPMeta.registerNamespace(nameSpace, nsPrefix);

and then adding some data to this new namespace. I am able to view it in Photoshop by checking under File -> FileInfo. My question is how can I access this data using the metadata extractor library in my c# project ? When I use the following code I do not see the new metadata I added inside any of the directories:

FileStream OriginalFile = new FileStream("C:\\Users\\av\\Desktop\\test.tif", FileMode.Open, FileAccess.Read, FileShare.Read); IEnumerable<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(OriginalFile);

Edit: I am able to loop through all the properties but when I try to do

var xmpDirectory = ImageMetadataReader.ReadMetadata("path/test.tif").OfType<XmpDirectory>().FirstOrDefault(); xmpDirectory.XmpMeta.GetProperty("http://ns.adobe.com/xap/1.0/mm/xmpMM:DerivedForm/", "stRef:documentID")

I get an exception. The property is present when I look at it through Photoshop.

xmp data in photoshop


Solution

  • XMP data is stored in the XmpDirectory. Access it via:

    var xmpDirectory = ImageMetadataReader.ReadMetadata("path\test.tif")
        .OfType<XmpDirectory>().FirstOrDefault();
    

    Note however that XMP data in metadata-extractor does not follow the standard tag/value format of other directories. Instead, you should access the XmpMeta property of the directory in order to inspect that data.

    You can then write code resembling:

    foreach (var property in xmpDirectory.XmpMeta.Properties)
        Console.WriteLine($"Path={property.Path} Namespace={property.Namespace} " +
                           "Value={property.Value}");
    

    Some more discussion here and more information about the XMP library here.