# frozen_string_literal: true require "pdf-reader" require_relative "find_text/version" module PDF::Reader::FindText def find_text(value) runs(merge: false).each_cons(value.tr(' ', '').size).map do |chars| string = merge_runs_with_max_length(chars, value.size) string if string.text[0, value.size] == value end.compact end private def merge_runs_with_max_length(chars, length) chars.inject do |string, char| if string.mergable?(char) && string.text.size < length string + char else string end end end end PDF::Reader::Page.include PDF::Reader::FindText