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 to all the PDF metadata, PSPDFKit comes with PSPDFDocumentPDFMetadata and PSPDFDocumentXMPMetadata, allowing you to retrieve or modify the documents metadata.

PDF Metadata

Use PSPDFDocumentPDFMetadata to work with the dictionary based metadata in a PDF. All values specified in the Info dictionary are represented by the following types:

  • NSString
  • NSNumber
  • NSDate
  • NSArray<id>: can include any of the types mentioned.
  • NSDictionary<NSString*, id>: value can be any of the types mentioned.

These types can be combined in any way you see fit and it will be converted into the proper PDF types.

The dictionary metadata may contain the following info keys by default:

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

You can of course add any supported key-value dictionary to the metadata.

Retrieving

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

Copy
1
2
3
let document = ...
let pdfMetadata = PSPDFDocumentPDFMetadata(document: document)
let author = pdfMetadata.object(forInfoDictionaryKey: "Author")
Copy
1
2
3
PSPDFDocument *document = ...
PSPDFDocumentPDFMetadata *pdfMetadata = [[PSPDFDocumentPDFMetadata alloc] initWithDocument:document];
NSString *author = [pdfMetadata objectForInfoDictionaryKey:@"Author"];

Saving

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

Copy
1
2
3
4
5
6
7
let pdfMetadata = PSPDFDocumentPDFMetadata(document: document)

let metadataKey = "MyKey"
let metadataValue = ["MyValue"]
pdfMetadata[metadataKey] = metadataValue

document.save()
Copy
1
2
3
4
5
6
7
PSPDFDocumentPDFMetadata *pdfMetadata = [[PSPDFDocumentPDFMetadata alloc] initWithDocument:document];

NSString *metadataKey = @"MyKey";
NSArray *metadataValue = @[@"MyValue"];
pdfMetadata[metadataKey] = metadataValue;

[document save:nil];

XMP Metadata

Use PSPDFDocumentXMPMetadata 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 automatically generated.

Retrieving

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

Copy
1
2
let metadata = PSPDFDocumentXMPMetadata(document: document)
let documentFormat = xmpMetadata.string(forXMPKey: "format", namespace: PSPDFXMPDCNamespace)
Copy
1
2
PSPDFDocumentXMPMetadata *xmpMetadata = [[PSPDFDocumentXMPMetadata alloc] initWithDocument:document];
NSString *documentFormat = [xmpMetadata stringForXMPKey:@"format" namespace:PSPDFXMPDCNamespace];

Saving

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

Copy
1
2
3
4
5
6
let xmpMetadata = PSPDFDocumentXMPMetadata(document: document)

let metadataKey = "MyKey"
let metadataValue = "MyValue"

xmpMetadata.setString(metadataValue, forXMPKey: metadataKey, namespace: PSPDFXMPPDFNamespace, suggestedNamespacePrefix: PSPDFXMPPDFNamespacePrefix)
Copy
1
2
3
4
5
6
7
PSPDFDocumentXMPMetadata *xmpMetadata = [[PSPDFDocumentXMPMetadata alloc] initWithDocument:document];

NSString *metadataKey = @"MyKey";
NSString *metadataValue = @"MyValue";

[xmpMetadata setString:metadataValue forXMPKey:metadataKey namespace:PSPDFXMPPDFNamespace suggestedNamespacePrefix:PSPDFXMPPDFNamespacePrefix];
[document save:nil];
Was this page helpful? We're happy to answer any questions.