class PDF::Reader::FormXObject

High level representation of a single PDF form xobject. Form xobjects are contained pieces of content that can be inserted onto multiple pages. They're generally used as a space efficient way to store repetative content (like logos, header, footers, etc).

This behaves and looks much like a limited PDF::Reader::Page class.

Attributes

xobject[R]

Public Class Methods

new(page, xobject, options = {}) click to toggle source
# File lib/pdf/reader/form_xobject.rb, line 19
def initialize(page, xobject, options = {})
  @page    = page
  @objects = page.objects
  @cache   = options[:cache] || {}
  @xobject = @objects.deref(xobject)
end

Public Instance Methods

font_objects() click to toggle source

return a hash of fonts used on this form.

The keys are the font labels used within the form content stream.

The values are a PDF::Reader::Font instances that provide access to most available metrics for each font.

# File lib/pdf/reader/form_xobject.rb, line 33
def font_objects
  raw_fonts = @objects.deref(resources[:Font] || {})
  ::Hash[raw_fonts.map { |label, font|
    [label, PDF::Reader::Font.new(@objects, @objects.deref(font))]
  }]
end
raw_content() click to toggle source

returns the raw content stream for this page. This is plumbing, nothing to see here unless you're a PDF nerd like me.

# File lib/pdf/reader/form_xobject.rb, line 52
def raw_content
  @xobject.unfiltered_data
end
walk(*receivers) click to toggle source

processes the raw content stream for this form in sequential order and passes callbacks to the receiver objects.

See the comments on PDF::Reader::Page#walk for more detail.

# File lib/pdf/reader/form_xobject.rb, line 45
def walk(*receivers)
  content_stream(receivers, raw_content)
end

Private Instance Methods

cached_tokens_key() click to toggle source
# File lib/pdf/reader/form_xobject.rb, line 74
def cached_tokens_key
  @cached_tokens_key ||= "tokens-#{content_stream_md5}"
end
callback(receivers, name, params=[]) click to toggle source
# File lib/pdf/reader/form_xobject.rb, line 64
def callback(receivers, name, params=[])
  receivers.each do |receiver|
    receiver.send(name, *params) if receiver.respond_to?(name)
  end
end
content_stream(receivers, instructions) click to toggle source
# File lib/pdf/reader/form_xobject.rb, line 90
def content_stream(receivers, instructions)
  params       = []

  tokens.each do |token|
    if token.kind_of?(Token) and PagesStrategy::OPERATORS.has_key?(token)
      callback(receivers, PagesStrategy::OPERATORS[token], params)
      params.clear
    else
      params << token
    end
  end
rescue EOFError
  raise MalformedPDFError, "End Of File while processing a content stream"
end
content_stream_md5() click to toggle source
# File lib/pdf/reader/form_xobject.rb, line 70
def content_stream_md5
  @content_stream_md5 ||= Digest::MD5.hexdigest(raw_content)
end
resources() click to toggle source

Returns the resources that accompany this form.

# File lib/pdf/reader/form_xobject.rb, line 60
def resources
  @resources ||= @objects.deref(@xobject.hash[:Resources]) || {}
end
tokens() click to toggle source
# File lib/pdf/reader/form_xobject.rb, line 78
def tokens
  @cache[cached_tokens_key] ||= begin
                buffer = Buffer.new(StringIO.new(raw_content), :content_stream => true)
                parser = Parser.new(buffer, @objects)
                result = []
                while (token = parser.parse_token(PagesStrategy::OPERATORS))
                  result << token
                end
                result
              end
end