Class: RPDF::PDFDictionary

Inherits:
PDFObject show all
Defined in:
lib/rpdf/pdfdictionary.rb

Overview

Dictionary Object

PDFRef15 p35

Direct Known Subclasses

DocumentCatalog, ImageDictionary, Page, PageTree

Instance Attribute Summary collapse

Attributes inherited from PDFObject

#generation_number, #object_number

Instance Method Summary collapse

Methods inherited from PDFObject

#indirect?, #invalidate, #invalidated?, #to_pdf, #to_ref

Constructor Details

#initialize(dict, document = nil) ⇒ PDFDictionary

Returns a new instance of PDFDictionary.

Raises:

  • (Exception)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rpdf/pdfdictionary.rb', line 11

def initialize(dict, document=nil)
  raise Exception.new("PDFDictionary is not a hash") unless dict.kind_of?(Hash)
  @dict = Hash.new
  dict.each { |key, value|
    pdfkey = RPDF.topdfobject(key)
    pdfvalue = RPDF.topdfobject(value)
    raise Exception.new("PDFDictionary key must be a name") unless pdfkey.kind_of?(PDFName)
    @dict[pdfkey] = pdfvalue
  }
  super(document)
end

Instance Attribute Details

#dictObject (readonly)

Returns the value of attribute dict.



9
10
11
# File 'lib/rpdf/pdfdictionary.rb', line 9

def dict
  @dict
end

Instance Method Details

#[](key) ⇒ Object

Raises:

  • (Exception)


23
24
25
26
27
# File 'lib/rpdf/pdfdictionary.rb', line 23

def [](key)
  pdfkey = RPDF.topdfobject(key)
  raise Exception.new("PDFDictionary key must be a name") unless pdfkey.kind_of?(PDFName)
  @dict[pdfkey]
end

#[]=(key, value) ⇒ Object

Raises:

  • (Exception)


29
30
31
32
33
34
# File 'lib/rpdf/pdfdictionary.rb', line 29

def []=(key, value)
  pdfkey = RPDF.topdfobject(key)
  pdfvalue = RPDF.topdfobject(value)
  raise Exception.new("PDFDictionary key must be a name") unless pdfkey.kind_of?(PDFName)
  @dict[pdfkey] = pdfvalue
end

#to_sObject



41
42
43
44
45
46
47
# File 'lib/rpdf/pdfdictionary.rb', line 41

def to_s
  bytes = "<< "
  @dict.each { |key, value|
    bytes << "#{key.to_s} #{value.to_ref}\n" # to_ref -> reference to indirect object
  }
  bytes << ">>\n"
end

#update(other) ⇒ Object

Update dictionary with other dictionary



37
38
39
# File 'lib/rpdf/pdfdictionary.rb', line 37

def update(other)
  @dict.update(other.dict)
end