Customizing Document Metadata

Metadata may be stored in a PDF document in two ways: in a document information dictionary associated with the document, or in a metadata stream containing XMP data. To give you full access to all the PDF metadata, PSPDFKit comes with DocumentPdfMetadata and DocumentXmpMetadata, which allow you to retrieve or modify a document’s metadata.

PDF Metadata

Use DocumentPdfMetadata to work with the dictionary-based metadata in a PDF.

All values specified in the PdfValue are represented by the following types:

  • boolean
  • long
  • double
  • String
  • List<PdfValue>
  • Map<String, PdfValue>

By default, the dictionary metadata may contain the following info keys:

  • Author
  • CreationDate
  • Creator
  • Keywords
  • ModDate
  • Producer
  • Title

You can, of course, add any supported key-value dictionary to the metadata. When dealing with these pre-defined keys, it’s recommended to use the DocumentPdfMetadata getters and setters so that you get out-of-the-box conversions from objects such as Date.

Retrieving

To get an entry of the metadata dictionary (e.g. the Author), you can use the following code snippet:

1
2
3
val document = ...
val pdfMetadata = document.getPdfMetadata()
val author = pdfMetada.getAuthor()
1
2
3
PdfDocument document = ...
DocumentPdfMetadata pdfMetadata = document.getPdfMetadata();
String author = pdfMetadata.getAuthor();

For any custom values, use this:

1
2
3
val document = ...
val pdfMetadata = document.pdfMetadata
val value = pdfMetada.get("Custom key")
1
2
3
PdfDocument document = ...
DocumentPdfMetadata pdfMetadata = document.getPdfMetadata();
PdfValue value = pdfMetadata.get("Custom key");

Saving

You can customize the document metadata and then save the document, which also saves the modified metadata into the PDF:

1
2
3
4
val pdfMetadata = document.pdfMetadata
val metadataKey = "Key"
pdfMetadata.set(metadataKey, PdfValue(3))
document.saveIfModified()
Copy
1
2
3
4
DocumentPdfMetadata pdfMetadata = document.getPdfMetadata();
String metadataKey = "Key";
pdfMetadata.set(metadataKey, new PdfValue(3));
document.saveIfModified();

XMP Metadata

Use DocumentXmpMetadata to work with the metadata stream containing XMP data.

Each key in the XMP metadata stream has to have a namespace set. You can define your own namespace or use one of the already existing ones. PSPDFKit exposes two constants for common namespaces:

When setting a value, you also have to pass along a suggested namespace prefix, as this can’t be generated automatically.

Retrieving

Use the following code snippet to get an object from the XMP metadata:

1
2
val xmpMetadata = document.xmpMetadata
val pdfValue = xmpMetadata.get("Key", NAMESPACE)
1
2
DocumentXmpMetadata xmpMetadata = document.getXmpMetadata();
PdfValue pdfValue = xmpMetadata.get("Key", NAMESPACE);

Saving

You can also set new metadata and save it to the document:

Copy
1
2
3
4
5
6
val xmpMetadata = document.xmpMetadata

val metadataKey = "MyKey"
val metadataValue = "MyValue"

xmpMetadata.set(metadataKey, metadataValue, NAMESPACE, NAMESPACE_PREFIX)
Copy
1
2
3
4
5
6
DocumentXmpMetadata xmpMetadata = document.getXmpMetadata();

String metadataKey = "MyKey";
String metadataValue = "MyValue";

xmpMetadata.set(metadataKey, metadataValue, NAMESPACE, NAMESPACE_PREFIX);