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).



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/combine_pdf/pdf_public.rb', line 292

def insert(location, data)
	pages_to_add = nil
	if data.is_a? PDF
 		@version = [@version, data.version].max
		pages_to_add = data.pages
		@names.update data.names_object, &self.class.method(:hash_merge_new_no_page)
	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