Get Media Management Metadata
The xmpMM: namespace is how XMP tracks document identity across saves,
conversions and renditions. Document.GetMediaManagement returns it as an
xmputil.MediaManagement. Read it when you need to know whether two files are the
same document, or what a file was derived from.
Three identifiers do the work, and they change at different rates:
| Field | Changes when |
|---|---|
OriginalDocumentID | Never, for the life of the document as a concept. Assigned once, when the content is first created. |
DocumentID | The document is copied to a new path or converted to another format. Identifies a version or branch. |
InstanceID | Every save after any change. Identifies one exact version of the bytes. |
Doing it
xmpDoc, err := xmputil.LoadDocument(stream.Stream)
if err != nil {
return err
}
mm, ok := xmpDoc.GetMediaManagement()
if !ok {
fmt.Println("no xmpMM namespace in this document")
return nil
}
fmt.Println(mm.OriginalDocumentID, mm.DocumentID, mm.InstanceID, mm.VersionID)The stream comes from reader.GetCatalogMetadata and core.GetStream, as in
get XMP metadata.
DerivedFrom is a *MediaManagementDerivedFrom and is nil when the document is
not recorded as derived from anything. It is a partial reference by design: the
components it does carry, usually an instance ID and a version, point at the source,
and anything missing is assumed unchanged.
Versions is the edit history, each entry holding VersionID, ModifyDate,
Comments and Modifier. It is empty in most files.
Limitations
Every identifier is a xmputil.GUID, which is a string type. Compare them as
strings, and treat an empty value as absent rather than as a zero GUID.
SetMediaManagement does not write version history, so a non-empty Versions
means the packet came from a tool that records it. Reading the field back after
writing with UniPDF will give you nothing.
The identifiers only mean what the producing application made them mean. A tool that
rewrites a file without updating InstanceID leaves you unable to tell two versions
apart, and a tool that regenerates DocumentID on every save destroys the branch
relationship. Treat the values as evidence, not proof.
Run the example
The example prints all of the xmpMM: fields it finds, including the derivation
reference and any version history.
git clone https://github.com/unidoc/unipdf-examples.git
cd unipdf-examples/metadata
go run pdf_get_xmp_media_management_metadata.go input.pdfIf this is your first time using UniPDF, follow the getting started guide to create an API key and set up your development environment.