class AddImageInsidePdf::TextReplaceProcessor

Public Class Methods

new(page, to_hide_arr) click to toggle source
Calls superclass method
# 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

Public Instance Methods

blackout_array(start_ind, end_ind) click to toggle source
# 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_text() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: show_text_with_positioning
show_text_with_positioning(str)
Alias for: show_text
sum_string(start_ind, end_ind) click to toggle source
# 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) click to toggle source
# 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