Read and edit PDF XMP metadata using C#

XMP

XMP (Extensible Metadata Platform) is an ISO standard used for creating and processing standardized and custom metadata for digital documents. It uses the RDF/XML syntax, which is embedded into a file. It consists of name/value pairs and allows using structured values and lists of values, as well as nesting them in other values.

Supported image formats

  • JPEG (read and write)
  • PNG (read and write)
  • TIFF (read and write)
  • WEBP (read and write)

Getting XMP metadata from a PDF document

To get the XMP metadata of a PDF document, use the GetMetadata method. This method doesn’t require any parameters and returns a string value of the document metadata in XMP format.

To get a PDF’s XMP metadata, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Get the XMP metadata.
string metadata = gdpicturePDF.GetMetadata();
Console.WriteLine(metadata);

Related topics

Getting XMP metadata from a PDF page

To get the XMP metadata from a PDF page, use the GetPageMetadata method. Before using this method in your code, use the SelectPage method to specify from which page you want to get the XMP metadata.

The GetPageMetadata method doesn’t require any parameters and returns a string value of the document metadata in XMP format.

To get the XMP metadata of the first page of a PDF, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Select the first page of the PDF document.
gdpicturePDF.SelectPage(1);
// Get the XMP metadata.
string metadata = gdpicturePDF.GetPageMetadata();
Console.WriteLine(metadata);

Setting XMP metadata for a PDF document

To set XMP metadata for a PDF document, use the SetMetadata method. It requires XMP metadata in the form of a string as its parameter.

To set XMP metadata for a PDF document from an XMP file, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Save the metadata to a string from a file.
String data = File.ReadAllText(@"C:\temp\metadata.xmp");
// Set the metadata from the string.
gdpicturePDF.SetMetadata(data);
gdpicturePDF.SaveToFile(@"C:\temp\source.pdf");

Setting XMP metadata for a PDF page

To set the XMP metadata for a PDF page, use the SetPageMetadata method. Before using this method in your code, use the SelectPage method to specify from which page you want to get the XMP metadata.

The GetPageMetadata method requires the XMP metadata as its parameter in the form of either a string or a byte array.

To set the XMP metadata of the first page of a PDF from a byte array, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Select the first page of the PDF document.
gdpicturePDF.SelectPage(1);
// Save the metadata to a byte array from a file.
byte[] data = File.ReadAllBytes(@"C:\temp\metadata.xmp");
// Set the metadata from the byte array.
gdpicturePDF.SetPageMetadata(data);
gdpicturePDF.SaveToFile(@"C:\temp\source.pdf");

Adding a custom XMP metadata entry to a PDF document

To add a custom metadata entry to a PDF document in XMP format, use the SetCustomPDFInformation method. It requires the key and the value of the custom XMP metadata in the form of a string.

To add a custom XMP metadata entry to a PDF document describing from which department the file originates, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Add custom XMP metadata.
gdpicturePDF.SetCustomPDFInformation("Department", "Human Resources");
gdpicturePDF.SaveToFile(@"C:\temp\source.pdf");