An internal PDF::Reader class that represents the XRef table in a PDF file as a hash-like object.
An Xref table is a map of object identifiers and byte offsets. Any time a particular object needs to be found, the Xref table is used to find where it is stored in the file.
Hash keys are object ids, values are either:
a byte offset where the object starts (regular PDF objects)
a PDF::Reader::Reference instance that points to a stream that contains the desired object (PDF objects embedded in an object stream)
The class behaves much like a standard Ruby hash, including the use of the Enumerable mixin. The key difference is no []= method - the hash is read only.
create a new Xref table based on the contents of the supplied io object
io - must be an IO object, generally either a file or a StringIO
# File lib/pdf/reader/xref.rb, line 54 def initialize (io) @io = io @xref = {} @trailer = load_offsets end
returns the byte offset for the specified PDF object.
ref - a PDF::Reader::Reference object containing an object ID and revision number
# File lib/pdf/reader/xref.rb, line 69 def [](ref) @xref[ref.id][ref.gen] rescue raise InvalidObjectError, "Object #{ref.id}, Generation #{ref.gen} is invalid" end
iterate over each object in the xref table
# File lib/pdf/reader/xref.rb, line 76 def each(&block) ids = @xref.keys.sort ids.each do |id| gen = @xref[id].keys.sort[-1] yield PDF::Reader::Reference.new(id, gen) end end
return the number of objects in this file. Objects with multiple generations are only counter once.
# File lib/pdf/reader/xref.rb, line 62 def size @xref.size end