Data Primitives and XMP Metadata
Data Primitives and XMP Metadata
Aspose.PDF FOSS for Python builds its higher-level API — Document, Page,
and the annotation and form classes — on top of a small set of primitive
data types defined in aspose_pdf.engine.data. You will rarely construct
most of these directly, but they surface constantly as property types and
return values: a page’s resources dictionary is keyed by PdfName values,
colors are Color instances, and Document.xmp_metadata returns an
XmpPacket. This guide introduces the object-identity primitives, the
primitive value wrappers, Color, the text-encoding enums, and the XMP
metadata model.
Object identity and the object registry
PdfObjectID identifies an indirect object in a PDF file by its
object_number and generation_number. PdfObjectRegistry assigns and
tracks these identifiers as objects are serialized, and PdfTrailerable is
the shared contract for objects that can produce the dictionary written into
a PDF trailer.
import aspose_pdf
from aspose_pdf.engine.data import PdfObjectRegistry
registry = PdfObjectRegistry()
# register() assigns a PdfObjectID to a serializable object
object_id = registry.register(some_pdf_object)
print(object_id.object_number, object_id.generation_number)
# get() resolves an object back from its PdfObjectID
resolved = registry.get(object_id)Primitive value wrappers
PdfNumber and PdfName wrap raw PDF values in typed Python objects.
PdfNumber holds a numeric value and converts it with to_double() or
to_int(); PdfName holds a name string and is the type used for
dictionary keys throughout the low-level object model — for example, the
Font and Resources keys on a page’s resource dictionary. PdfNull
represents the PDF null object.
import aspose_pdf
from aspose_pdf.engine.data.number import PdfNumber
from aspose_pdf.engine.data.types import PdfName
number = PdfNumber(3.14)
as_float = number.to_double()
as_int = number.to_int()
resources_key = PdfName("Resources")
font_key = PdfName("Font")Color values
Color represents an RGBA color with r, g, b, and a float
properties. Besides the two-argument-plus-alpha constructor, it exposes a
full set of named color factories — both lowercase (aqua(), blue(),
azure(), red(), green(), yellow(), black(), white(), gray())
and PascalCase aliases (Aqua(), Blue(), Azure(), Red(), Green(),
Yellow(), Black(), White(), Gray()).
import aspose_pdf
from aspose_pdf.engine.data.types import Color
custom = Color(r=0.2, g=0.4, b=0.8, a=1.0)
# Named factories return ready-made Color instances
highlight = Color.yellow()
border = Color.Black()Text encodings and stream filters
EncodingType (WIN_ANSI, MAC_ROMAN, MAC_EXPERT, PDF_DOC, UNICODE)
identifies a text encoding, and Encoding exposes the matching encoding
constants (UTF8, UTF16, LATIN1, WIN_ANSI) alongside an
encode(text, encoding_type) method that returns the encoded bytes.
FilterType (NONE, FLATE_DECODE, LZW_DECODE, DCT_DECODE,
JPX_DECODE, CCITT_FAX_DECODE, JBIG2_DECODE) identifies the stream
filter applied to compressed PDF content.
import aspose_pdf
from aspose_pdf.engine.data import Encoding, EncodingType, FilterType
encoded = Encoding.encode("Hello, world", EncodingType.WIN_ANSI)
# FilterType values identify how a content or image stream is compressed
current_filter = FilterType.FLATE_DECODEXMP metadata
XmpPacket is the in-memory model for a document’s XMP metadata packet —
it is the type returned by Document.xmp_metadata. It holds an ordered
collection of fields (each a XmpField, XmpArray, or XmpProperty) and
resolves namespace prefixes through an XmpNamespaceProvider. Typed
getters and setters (get_value-style accessors such as get_bool,
get_int, get_real, get_date, get_localized_text, get_array, and
their set_* counterparts) read and write individual properties by
namespace prefix or URI and property name.
import aspose_pdf
doc = aspose_pdf.Document()
doc.load_from("input.pdf")
xmp = doc.xmp_metadata # XmpPacket
title = xmp.get_localized_text("dc", "title", lang="x-default")
xmp.set_value("dc", "creator", "Jane Doe")
serialized = xmp.serialize()XmpStruct models a structured (rdf:parseType="Resource") value with its
own get(name) lookup, XmpArray models an ordered array with add(item)
and remove(item), and XmpProperty is a property that carries its own
add_qualifier/remove_qualifier qualifiers. All four share the same
XmpField building block for individual values.
Tips and Best Practices
- Read
Document.xmp_metadatato get the document’sXmpPacketrather than building one from scratch — it is already populated from the loaded file. - Use
PdfNumber.to_double()orto_int()rather than assuming the underlyingvalueis already the Python numeric type you expect. - Prefer the named
Colorfactories (Color.red(),Color.Black(), and so on) over hand-rolledr/g/b/atuples for common colors — they read more clearly at call sites. - Match the
EncodingTypeyou pass toEncoding.encode()to the encoding the destination (font, stream, or viewer) actually expects; PDF text encoding mismatches are a common source of garbled output. - Treat
PdfObjectRegistryandPdfObjectIDas low-level, engine-facing types — most application code reads document content throughDocumentandPagerather than registering objects directly.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
PdfNumber comparisons behave unexpectedly | Comparing the wrapper instead of its numeric value | Call to_double() or to_int() before comparing or doing arithmetic |
XMP property lookup returns None | Wrong prefix or uri passed to get_value-style accessors | Confirm the namespace prefix via XmpNamespaceProvider.get_uri() / get_prefix() before reading |
Encoded text looks garbled after Encoding.encode() | EncodingType doesn’t match what the destination expects | Use the EncodingType value (WIN_ANSI, MAC_ROMAN, MAC_EXPERT, PDF_DOC, UNICODE) that matches the target font or viewer |
Custom PdfObjectRegistry objects don’t resolve later | get() called with a PdfObjectID that was never register()-ed on that registry instance | Register every object on the same PdfObjectRegistry instance before resolving it |
FAQ
Do I need to construct PdfObjectID or PdfObjectRegistry myself?
Rarely. They back the engine’s serialization process; most application code never instantiates them directly unless it is working with the low-level object model.
What is the difference between XmpField, XmpArray, XmpStruct, and XmpProperty?
XmpField is a single scalar value. XmpArray is an ordered list of
values, XmpStruct is a structured (rdf:parseType="Resource") grouping of
fields, and XmpProperty is a value that also carries its own qualifiers.
How do I get a document’s XMP metadata?
Read the xmp_metadata property on a loaded Document — it returns an
XmpPacket you can query and update in place.
Are the lowercase and PascalCase Color factories different colors?
No — Color.red() and Color.Red() are aliases that return the same
color; both spellings are provided for convenience.
API Reference Summary
| Class / Method | Description |
|---|---|
PdfObjectID | Identifies an indirect PDF object by object and generation number |
PdfObjectRegistry.register | Assigns a PdfObjectID to an object being serialized |
PdfObjectRegistry.get | Resolves an object from a previously registered PdfObjectID |
PdfTrailerable.get_dictionary | Produces the dictionary written into a PDF trailer |
PdfNumber.to_double / to_int | Converts a wrapped PDF number to a Python float or int |
PdfName | Wraps a PDF name value, used as dictionary keys in the low-level object model |
PdfNull | Represents the PDF null object |
Color | RGBA color value with named factories (red, blue, Black, and so on) |
Encoding.encode | Encodes text to bytes using an EncodingType |
EncodingType | Text encoding identifier: WIN_ANSI, MAC_ROMAN, MAC_EXPERT, PDF_DOC, UNICODE |
FilterType | Stream compression filter identifier used by content and image streams |
XmpPacket | In-memory XMP metadata packet, returned by Document.xmp_metadata |
XmpField | A single XMP property value |
XmpArray | An ordered XMP array value |
XmpStruct | A structured (rdf:parseType="Resource") XMP value |
XmpProperty | An XMP property that carries its own qualifiers |
XmpNamespaceProvider | Resolves XMP namespace prefixes to and from URIs |