Method: HexaPDF::Document#add

Defined in:
lib/hexapdf/document.rb

#add(obj, revision: :current, **wrap_opts) ⇒ Object

:call-seq:

doc.add(obj, revision: :current, **wrap_opts)     -> indirect_object

Adds the object to the specified revision of the document and returns the wrapped indirect object.

The object can either be a native Ruby object (Hash, Array, Integer, …) or a HexaPDF::Object. If it is not the latter, #wrap is called with the object and the additional keyword arguments.

If the revision option is :current, the current revision is used. Otherwise revision should be a revision index.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/hexapdf/document.rb', line 196

def add(obj, revision: :current, **wrap_opts)
  obj = wrap(obj, wrap_opts) unless obj.kind_of?(HexaPDF::Object)

  revision = (revision == :current ? @revisions.current : @revisions.revision(revision))
  if revision.nil?
    raise ArgumentError, "Invalid revision index specified"
  end

  if obj.document? && obj.document != self
    raise HexaPDF::Error, "Can't add object that is already attached to another document"
  end
  obj.document = self

  if obj.indirect? && (rev_obj = revision.object(obj.oid))
    if rev_obj.equal?(obj)
      return obj
    else
      raise HexaPDF::Error, "Can't add object because the specified revision already has " \
        "an object with object number #{obj.oid}"
    end
  end

  obj.oid = @revisions.map(&:next_free_oid).max unless obj.indirect?

  revision.add(obj)
end