Method: CombinePDF::PDF#insert
- Defined in:
- lib/combine_pdf/pdf_public.rb
#insert(location, data) ⇒ Object
add PDF pages (or PDF files) into a specific location.
returns the new pages Array! (unlike ‘#<<`, doesn’t return self!)
- location
-
the location for the added page(s). Could be any number. negative numbers represent a count backwards (-1 being the end of the page array and 0 being the begining). if the location is beyond bounds, the pages will be added to the end of the PDF object (or at the begining, if the out of bounds was a negative number).
- data
-
a PDF page, a PDF file (CombinePDF.new “filname.pdf”) or an array of pages (CombinePDF.new(“filname.pdf”).pages).
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/combine_pdf/pdf_public.rb', line 298 def insert(location, data) pages_to_add = nil if data.is_a? PDF @version = [@version, data.version].max pages_to_add = data.pages actual_value(@names ||= {}.dup).update actual_value(data.names_object), &self.class.method(:hash_merge_new_no_page) merge_outlines((@outlines ||= {}.dup), data.outlines_object, location) unless actual_value(data.outlines_object).empty? if actual_value(@forms_data) actual_value(@forms_data).update actual_value(data.forms_data), &self.class.method(:hash_merge_new_no_page) if data.forms_data else @forms_data = data.forms_data end warn 'Form data might be lost when combining PDF forms (possible conflicts).' unless data.forms_data.nil? || data.forms_data.empty? elsif data.is_a?(Array) && (data.select { |o| !(o.is_a?(Hash) && o[:Type] == :Page) }).empty? pages_to_add = data elsif data.is_a?(Hash) && data[:Type] == :Page pages_to_add = [data] else warn "Shouldn't add objects to the file unless they are PDF objects or PDF pages (an Array or a single PDF page)." return false # return false, which will also stop any chaining. end # pages_to_add.map! {|page| page.copy } catalog = rebuild_catalog pages_array = catalog[:Pages][:referenced_object][:Kids] page_count = pages_array.length if location < 0 && (page_count + location < 0) location = 0 elsif location > 0 && (location > page_count) location = page_count end pages_array.insert location, pages_to_add pages_array.flatten! self end |