Class: AddImageInsidePdf::TextReplaceProcessor

Inherits:
HexaPDF::Content::Processor
  • Object
show all
Defined in:
lib/add_image_inside_pdf/text_replace_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(page, to_hide_arr) ⇒ TextReplaceProcessor

Returns a new instance of TextReplaceProcessor.



4
5
6
7
8
9
# File 'lib/add_image_inside_pdf/text_replace_processor.rb', line 4

def initialize(page, to_hide_arr)
  super()
  @canvas = page.canvas(type: :overlay)
  @to_hide_arr = to_hide_arr
  @boxeslist = []
end

Instance Method Details

#blackout_array(start_ind, end_ind) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/add_image_inside_pdf/text_replace_processor.rb', line 29

def blackout_array(start_ind, end_ind)
  sum = ""
  i = start_ind
  while i < start_ind+end_ind  do
    box = @boxeslist[i]
    @canvas.fill_color(255, 255, 255)
    x, y = *box.lower_left
    tx, ty = *box.upper_right
    @canvas.rectangle(x, y, tx - x, ty - y).fill
    i +=1
  end
end

#blackout_textObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/add_image_inside_pdf/text_replace_processor.rb', line 18

def blackout_text()
  @to_hide_arr.each do |hide_item|
    @boxeslist.each_with_index do |box, index|
      #puts sum_string(index, hide_item.length)
      if hide_item == sum_string(index, hide_item.length)
        blackout_array(index, hide_item.length)
      end
    end
  end
end

#replace_text_to_image(writable_image, height = 20) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/add_image_inside_pdf/text_replace_processor.rb', line 42

def replace_text_to_image(writable_image, height=20)
  @to_hide_arr.each do |hide_item|
    @boxeslist.each_with_index do |box, index|
      #puts sum_string(index, hide_item.length)
      if hide_item == sum_string(index, hide_item.length)
        write_image_to_array(index, hide_item.length, writable_image, height)
      end
    end
  end
end

#show_text(str) ⇒ Object Also known as: show_text_with_positioning



11
12
13
14
15
16
# File 'lib/add_image_inside_pdf/text_replace_processor.rb', line 11

def show_text(str)
  boxes = decode_text_with_positioning(str)
  boxes.each do |box|
      @boxeslist << box
  end
end

#sum_string(start_ind, end_ind) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/add_image_inside_pdf/text_replace_processor.rb', line 75

def sum_string(start_ind, end_ind)
  sum = ""
  i = start_ind
  while i < start_ind+end_ind  do
    begin
      sum += @boxeslist[i].string
    rescue NoMethodError 
      print ""
    end
    i +=1
  end
  return sum
end

#write_image_to_array(start_ind, end_ind, writable_image, height) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/add_image_inside_pdf/text_replace_processor.rb', line 53

def write_image_to_array(start_ind, end_ind, writable_image, height)
  sum = ""
  i = start_ind
  x1, y1 = false, false
  while i < start_ind+end_ind  do
    box = @boxeslist[i]
    @canvas.fill_color(255, 255, 255)
    x, y = *box.lower_left
    tx, ty = *box.upper_right
    if i==start_ind
      x1, y1 = x, y
    end
    @canvas.rectangle(x, y, tx - x, ty - y).fill
    i +=1
  end
  if x1 && y1
    @canvas.translate(0, 0) do
      @canvas.image(writable_image, at: [x1, y1], height: height) 
    end
  end
end