How do I embed and extract XMP data associated with a PDF document?

Q:

How do I embed and extract an XMP stream associated with a PDF
document?
I would like to store some custom entries in the XMP stream.
------
A:

You can extract this XMP stream as follows :

Obj xmp_stm = pdfdoc.GetRoot().FindObj("Metadata");
if (xmp_stm != null) {
  // extract embedded XMP ...
  Obj stm = xmp_stm.GetDecodedStream();
  FilterReader reader = new FilterReader(stm);

  StdFile out_file = new StdFile("myxmp.xml",
StdFile.OpenMode.e_write_mode);
  FilterWriter writer = new FilteWriter(out_file);
  writer.WriteFilter(reader);
  writer.Flush();
  out_file.Close();
}

// Similarly you can embed or replace an existing XMP stream as
follows:
// Using C# - Add XMP metadata to document catalog.

StdFile xmp_file = new StdFile("myxmp.xml",
StdFile.OpenMode.e_read_mode); FilterReader reader = new
FilterReader(xmp_file); Obj xmp_stm =
pdfdoc.CreateIndirectStream(reader);
xmp_stm.Put("Subtype", Obj.CreateName("XML")); xmp_stm.Put("Type",
Obj.CreateName("Metadata")); pdfdoc.GetRoot().Put("Metadata",
xmp_stm);

It is also possible to embed a XMP data from a memory buffer.

Having said this, it is probably a better idea (or at least is much
easier) to store custom key/value pairs using SDF/Cos API (instead as
part of the embedded XMP stream). This is illustrated in SDF sample
project: http://www.pdftron.com/net/samplecode/SDFTest.cs.

You can find more information about SDF/Cos api here:
  www.pdftron.com/net/usermanual.html#low_level_api