Class: RPDF::PDFObject

Inherits:
Object
  • Object
show all
Defined in:
lib/rpdf/pdfobject.rb

Overview

An object which is a part of a PDF document. [PDFRef15 p27]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document = nil) ⇒ PDFObject

Make an indirect object if a document is passed, otherwise a direct object.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rpdf/pdfobject.rb', line 9

def initialize(document=nil)
  if document
    @object_number = document.new_id(self)
    @document = document
  else
    @object_number = 0 # The object number of an indirect object should be positive
  end
  @generation_number = 0
  @invalidated = false
end

Instance Attribute Details

#generation_numberObject (readonly)

Returns the value of attribute generation_number.



6
7
8
# File 'lib/rpdf/pdfobject.rb', line 6

def generation_number
  @generation_number
end

#object_numberObject (readonly)

Returns the value of attribute object_number.



6
7
8
# File 'lib/rpdf/pdfobject.rb', line 6

def object_number
  @object_number
end

Instance Method Details

#indirect?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rpdf/pdfobject.rb', line 20

def indirect?
  @object_number > 0
end

#invalidateObject

Subclasses has to call this in their invalidate method if they override it



25
26
27
28
# File 'lib/rpdf/pdfobject.rb', line 25

def invalidate
  @document.write(self)
         @invalidated = true
end

#invalidated?Boolean

true if the object has already been written to the PDF stream

Returns:

  • (Boolean)


31
32
33
# File 'lib/rpdf/pdfobject.rb', line 31

def invalidated?
  @invalidated
end

#to_pdfObject

Return a representation suitable to write to a PDF file

PDFRef15 p39


37
38
39
40
41
42
43
44
45
# File 'lib/rpdf/pdfobject.rb', line 37

def to_pdf
  if indirect?
    bytes = "#{@object_number} #{@generation_number} obj\n"
    bytes << to_s << "\n"
    bytes << "endobj\n\n"
  else
    to_s
  end
end

#to_refObject

Return an indirect reference to this object if it’s indirect, return the bytestream otherwise.

PDFRef15 p39


50
51
52
53
54
55
56
# File 'lib/rpdf/pdfobject.rb', line 50

def to_ref
  if indirect?
    "#{@object_number} #{@generation_number} R"
  else
    to_s
  end
end