Class: PDF::Reader::PageTextReceiver

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pdf/reader/page_text_receiver.rb

Overview

Builds a UTF-8 string of all the text on a single page by processing all the operaters in a content stream.

Constant Summary collapse

SPACE =

Signature:

  • String

" "

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Signature:

  • untyped



23
24
25
# File 'lib/pdf/reader/page_text_receiver.rb', line 23

def options
  @options
end

#stateObject (readonly)

Signature:

  • untyped



20
21
22
# File 'lib/pdf/reader/page_text_receiver.rb', line 20

def state
  @state
end

Instance Method Details

#begin_marked_content_with_pl(tag, properties) ⇒ Object

Marked Content



129
130
131
132
133
134
# File 'lib/pdf/reader/page_text_receiver.rb', line 129

def begin_marked_content_with_pl(tag, properties)
  if properties.is_a?(Hash) && properties[:ActualText]
    @actual_text = properties[:ActualText]
    @actual_text_consumed = false
  end
end

#contentObject

deprecated

Signature:

  • () -> String



90
91
92
93
# File 'lib/pdf/reader/page_text_receiver.rb', line 90

def content
  mediabox = @page.rectangles[:MediaBox]
  PageLayout.new(runs, mediabox).to_s
end

#end_marked_contentObject



136
137
138
139
# File 'lib/pdf/reader/page_text_receiver.rb', line 136

def end_marked_content
  @actual_text = nil
  @actual_text_consumed = false
end

#invoke_xobject(label) ⇒ Object

XObjects



144
145
146
147
148
149
150
151
# File 'lib/pdf/reader/page_text_receiver.rb', line 144

def invoke_xobject(label)
  @state.invoke_xobject(label) do |xobj|
    case xobj
    when PDF::Reader::FormXObject then
      xobj.walk(self)
    end
  end
end

#move_to_next_line_and_show_text(str) ⇒ Object

'



115
116
117
118
# File 'lib/pdf/reader/page_text_receiver.rb', line 115

def move_to_next_line_and_show_text(str) # '
  @state.move_to_start_of_next_line
  show_text(str)
end

#page=(page) ⇒ Object

starting a new page



47
48
49
50
51
52
53
54
# File 'lib/pdf/reader/page_text_receiver.rb', line 47

def page=(page)
  @state = PageState.new(page)
  @page = page
  @content = []
  @characters = []
  @actual_text = nil
  @actual_text_consumed = false
end

#runs(opts = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pdf/reader/page_text_receiver.rb', line 56

def runs(opts = {})
  runs = @characters

  if rect = opts.fetch(:rect, @page.rectangles[:CropBox])
    runs = BoundingRectangleRunsFilter.runs_within_rect(runs, rect)
  end

  if opts.fetch(:skip_zero_width, true)
    runs = ZeroWidthRunsFilter.exclude_zero_width_runs(runs)
  end

  if opts.fetch(:skip_overlapping, true)
    runs = OverlappingRunsFilter.exclude_redundant_runs(runs)
  end

  runs = NoTextFilter.exclude_empty_strings(runs)

  if opts.fetch(:merge, true)
    runs = merge_runs(runs)
  end

  if (only_filter = opts.fetch(:only, nil))
    runs = AdvancedTextRunFilter.only(runs, only_filter)
  end

  if (exclude_filter = opts.fetch(:exclude, nil))
    runs = AdvancedTextRunFilter.exclude(runs, exclude_filter)
  end

  runs
end

#set_spacing_next_line_show_text(aw, ac, string) ⇒ Object

"



120
121
122
123
124
# File 'lib/pdf/reader/page_text_receiver.rb', line 120

def set_spacing_next_line_show_text(aw, ac, string) # "
  @state.set_word_spacing(aw)
  @state.set_character_spacing(ac)
  move_to_next_line_and_show_text(string)
end

#show_text(string) ⇒ Object

Text Showing Operators

record text that is drawn on the page



99
100
101
# File 'lib/pdf/reader/page_text_receiver.rb', line 99

def show_text(string) # Tj (AWAY)
  internal_show_text(string)
end

#show_text_with_positioning(params) ⇒ Object

TJ [(A) 120 (WA) 20 (Y)]



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pdf/reader/page_text_receiver.rb', line 103

def show_text_with_positioning(params) # TJ [(A) 120 (WA) 20 (Y)]
  params.each do |arg|
    if arg.is_a?(String)
      internal_show_text(arg)
    elsif arg.is_a?(Numeric)
      @state.process_glyph_displacement(0, arg, false)
    else
      # skip it
    end
  end
end