class HexaPDF::Dictionary

Included Modules

Implementation of the PDF dictionary type.

Subclasses should use the available class method ::define_field to create fields according to the PDF specification. This allows, among other things, automatic type checking and basic validation.

Fields defined in superclasses are inherited by their subclasses. This avoids duplicating basic field information. If fields differ from their superclass definition, they can be defined again in the subclass.

See: PDF2.0 s7.3.7

Public Class Methods

define_field(name, type:, required: false, default: nil, indirect: nil, allowed_values: nil, version: '1.0')

Defines an entry for the field name and returns the initalized HexaPDF::DictionaryFields::Field object. A suitable converter module (see HexaPDF::DictionaryFields::Field#converter) is selected based on the type argument.



The class (or an array of classes) that a value of this field must have. Here is a mapping from PDF object types to classes:


[TrueClass, FalseClass] (or use the Boolean constant)






String (for text strings), PDFByteString (for binary strings)






PDFArray or Array


Dictionary (or any subclass) or Hash


Stream (or any subclass)



If an array of classes is provided, the value can be an instance of any of these classes.

If a Symbol object instead of a class is provided, the class is looked up using the ‘object.type_map’ global configuration option when necessary to support lazy loading.

Note that if multiple types are allowed and one of the allowed types is Dictionary (or a Symbol), it has to be the first in the list. Otherwise automatic type conversion functions won’t work correctly.


Specifies whether this field is required, either true or false.


Specifies the default value for the field, if any.


Specifies whether the value (or the values in the array value) of this field has to be an indirect object (true), a direct object (false) or if it doesn’t matter (unspecified or nil).


An array of allowed values for this field.


Specifies the minimum version of the PDF specification needed for this value.


Defines the static PDF type of the class in cases where this is possible, i.e. when the class implements one specific PDF type (e.g. the HexaPDF::Type::Catalog class).

each_field {|name, data| block } → class
each_field → Enumerator

Calls the block once for each field defined either in this class or in one of the ancestor classes.


Returns the field entry for the given field name.

The ancestor classes are also searched for such a field entry if none is found for the current class.


Returns the statically defined PDF type of the class.

See ::define_type

Public Instance Methods


Returns the value for the given dictionary entry.

This method should be used instead of direct access to the value because it provides numerous advantages:

  • References are automatically resolved.

  • Returns the native Ruby object for values with class HexaPDF::Object. However, all subclasses of HexaPDF::Object are returned as is (it makes no sense, for example, to return the hash that describes the Catalog instead of the Catalog object).

  • Automatically wraps hash values in specific subclasses of this class if field information is available (see ::define_field).

  • Returns the default value if one is specified and no value is available.

Note: If field information is available for the entry, a Hash or Array value will always be wrapped by Dictionary or PDFArray. Otherwise, the value will be returned as-is.

Note: This method may throw a “can’t add a new key into hash during iteration” error in certain cases because it potentially modifies the underlying hash!

[]=(name, data)

Stores the data under name in the dictionary. Name has to be a Symbol.

If the current value for this name has the class HexaPDF::Object (and only this, no subclasses) and the given value has not (including subclasses), the value is stored inside the HexaPDF::Object.


Deletes the name-value pair from the dictionary and returns the value. If such a pair does not exist, nil is returned.

each {|name, value| block} → dict
each → Enumerator

Calls the given block once for every name-value entry that is stored in the dictionary.

Note that the yielded value is already preprocessed like in [].


Returns true if the dictionary contains no entries.


Returns true if the given key is present in the dictionary and not nil.


Returns a hash containing the preprocessed values (like in []).


Returns, in order or availability, the value of ::type, the /Type field or the result of Object#type.

Calls superclass method HexaPDF::Object#type