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).
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/combine_pdf/pdf_public.rb', line 316 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 data.names, &HASH_MERGE_NEW_NO_PAGE merge_outlines((@outlines ||= {}.dup), actual_value(data.outlines), location) unless actual_value(data.outlines).empty? if actual_value(@forms_data) actual_value(@forms_data).update actual_value(data.forms_data), &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 |