Working with XMP metadata

What is XMP?

The Extensible Metadata Platform (XMP) is an XML-based ISO metadata standard, originally created by Adobe Systems Inc. It defines a data structure, serialization model and basic metadata properties intended to form a unified metadata package that can be embedded into different media formats. The defined XMP data model can be used to store any set of metadata properties. These can be simple name/value pairs, structured values or lists of values. The data can be nested as well.

Reading XMP properties

To access XMP metadata in a file of any supported format, GroupDocs.Metadata provides the IXmp.XmpPackage property. The following are the steps to read XMP metadata:

  1. Load a file that contains XMP metadata
  2. Extract the XMP metadata package using the IXmp.XmpPackage property

The following code snippet gets XMP properties of a PNG image and displays them on the screen. 

AdvancedUsage.WorkingWithMetadataStandards.Xmp.ReadXmpProperties

using (Metadata metadata = new Metadata(Constants.PngWithXmp))
{
	IXmp root = metadata.GetRootPackage() as IXmp;
	if (root != null && root.XmpPackage != null)
	{
		if (root.XmpPackage.Schemes.XmpBasic != null)
		{
			Console.WriteLine(root.XmpPackage.Schemes.XmpBasic.CreatorTool);
			Console.WriteLine(root.XmpPackage.Schemes.XmpBasic.CreateDate);
			Console.WriteLine(root.XmpPackage.Schemes.XmpBasic.ModifyDate);
			Console.WriteLine(root.XmpPackage.Schemes.XmpBasic.Label);
			Console.WriteLine(root.XmpPackage.Schemes.XmpBasic.Nickname);

			// ...
		}

		if (root.XmpPackage.Schemes.DublinCore != null)
		{
			Console.WriteLine(root.XmpPackage.Schemes.DublinCore.Format);
			Console.WriteLine(root.XmpPackage.Schemes.DublinCore.Coverage);
			Console.WriteLine(root.XmpPackage.Schemes.DublinCore.Identifier);
			Console.WriteLine(root.XmpPackage.Schemes.DublinCore.Source);

			// ...
		}

		if (root.XmpPackage.Schemes.Photoshop != null)
		{
			Console.WriteLine(root.XmpPackage.Schemes.Photoshop.ColorMode);
			Console.WriteLine(root.XmpPackage.Schemes.Photoshop.IccProfile);
			Console.WriteLine(root.XmpPackage.Schemes.Photoshop.Country);
			Console.WriteLine(root.XmpPackage.Schemes.Photoshop.City);
			Console.WriteLine(root.XmpPackage.Schemes.Photoshop.DateCreated);

			// ... 
		}

		// ...
	}
}

Here is a full list of supported XMP schemes:

Updating XMP properties

The GroupDocs.Metadata API facilitates the user to update XMP metadata in a convenient way - using the XmpPacketWrapper class properties. Follow the below steps to update XMP metadata in a file of any supported format.

  1. Load a file that contains XMP metadata
  2. Extract the XMP metadata package using the IXmp.XmpPackage property
  3. Assign values to desired XMP properties
  4. Save the changes

AdvancedUsage.WorkingWithMetadataStandards.Xmp.UpdateXmpProperties

using (Metadata metadata = new Metadata(Constants.GifWithXmp))
{
	IXmp root = metadata.GetRootPackage() as IXmp;
	if (root != null && root.XmpPackage != null)
	{
		// if there is no such scheme in the XMP package we should create it
		if (root.XmpPackage.Schemes.DublinCore == null)
		{
			root.XmpPackage.Schemes.DublinCore = new XmpDublinCorePackage();
		}
		root.XmpPackage.Schemes.DublinCore.Format = "image/gif";
		root.XmpPackage.Schemes.DublinCore.SetRights("Copyright (C) 2011-2019 GroupDocs. All Rights Reserved");
		root.XmpPackage.Schemes.DublinCore.SetSubject("test");

		if (root.XmpPackage.Schemes.CameraRaw == null)
		{
			root.XmpPackage.Schemes.CameraRaw = new XmpCameraRawPackage();
		}
		root.XmpPackage.Schemes.CameraRaw.Shadows = 50;
		root.XmpPackage.Schemes.CameraRaw.AutoBrightness = true;
		root.XmpPackage.Schemes.CameraRaw.AutoExposure = true;
		root.XmpPackage.Schemes.CameraRaw.CameraProfile = "test";
		root.XmpPackage.Schemes.CameraRaw.Exposure = 0.0001;

		// If you don't want to keep the old values just replace the whole scheme
		root.XmpPackage.Schemes.XmpBasic = new XmpBasicPackage();
		root.XmpPackage.Schemes.XmpBasic.CreateDate = DateTime.Today;
		root.XmpPackage.Schemes.XmpBasic.BaseUrl = "https://groupdocs.com";
		root.XmpPackage.Schemes.XmpBasic.Rating = 5;
		root.XmpPackage.Schemes.BasicJobTicket = new XmpBasicJobTicketPackage();

		// Set a complex type property
		root.XmpPackage.Schemes.BasicJobTicket.Jobs = new[]
		{
			new XmpJob
			{
				ID = "1",
				Name = "test job",
				Url = "https://groupdocs.com"
			},
		};

		// ... 

		metadata.Save(Constants.OutputGif);
	}
}

Adding a custom XMP package

The GroupDocs.Metadata API provides access to a number of commonly used XMP schemes. But it also allows you to create fully custom XMP packages containing user-defined properties. The example below demonstrates how to create and add a custom XMP package to a file.

AdvancedUsage.WorkingWithMetadataStandards.Xmp.AddCustomXmpPackage

using (Metadata metadata = new Metadata(Constants.InputJpeg))
{
	IXmp root = metadata.GetRootPackage() as IXmp;
	if (root != null)
	{
		var packet = new XmpPacketWrapper();
		
		var custom = new XmpPackage("gd", "https://groupdocs.com");
		custom.Set("gd:Copyright", "Copyright (C) 2011-2019 GroupDocs. All Rights Reserved.");
		custom.Set("gd:CreationDate", DateTime.Today);
		custom.Set("gd:Company", XmpArray.From(new [] { "Aspose", "GroupDocs" }, XmpArrayType.Ordered));
		packet.AddPackage(custom);

		root.XmpPackage = packet;

		metadata.Save(Constants.OutputJpeg);
	}
}

Removing XMP metadata

To remove the XMP package from a file just assign null to the IXmp.XmpPackage property. The code sample below shows how to remove XMP metadata from a file.

AdvancedUsage.WorkingWithMetadataStandards.Xmp.AddCustomXmpPackage

using (Metadata metadata = new Metadata(Constants.JpegWithXmp))
{
	IXmp root = metadata.GetRootPackage() as IXmp;
	if (root != null)
	{
		root.XmpPackage = null;

		metadata.Save(Constants.OutputJpeg);
	}
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document metadata management App

Along with full featured .NET library we provide simple, but powerful free Apps. You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.