boazsegev / combine_pdf

A Pure ruby library to merge PDF files, number pages and maybe more...
MIT License
735 stars 157 forks source link

Skipping numberibng pages #41

Closed sashman closed 3 years ago

sashman commented 8 years ago

Is there currently an easy way to skip pages for numbering? For example specifying pages or skipping odd/even pages?

boazsegev commented 8 years ago

the number_pages and stamp_pages helper methods can be used on adjacent groups of pages (using the :page_range option).

For customized groups (i.e. odd numbers etc') I would recommend using pdf.pages.select and then stamping each page (or numbering it). i.e. (untested):

pdf = CombinePDF.load "mypdf.pdf"
stamp = CombinePDF.load("stamp.pdf").pages[0]

odd_pages = pdf.pages.select.with_index {|p, i|  i.even?} # odd pages are actually even indexed
odd_pages.count.times do |i|
   # whatever you need to do...

   # stamp
   odd_pages[i] << stamp
   # add page numbers
   odd_pages[i].textbox "- odd #{i} -", text_valign: :bottom, max_font_size:12

end

It's possible that the pdf#number_pages api could be extended to accept a block, much like select... but then what about page numbers? do we force the select block to take page numbers (0 indexed?) or page objects?

... I think that it's important to keep the helper methods as simple and intuitive as possible (also as far as performance go) and allow any customization to be performed through the actual core API.

But... if you have a better idea, I'm always open for suggestions.

sashman commented 8 years ago

I was thinking just a straight forward pdf#number_pages option like skip_odd: :true and then do something along the lines of next if options[:skip_odd] and page_number.even?. That's with out performance considerations....

boazsegev commented 8 years ago

Sounds great. Do you want to put it in?

The method is defined in the pdf_public.rb file. I think line 373 is where it should go...

I'm a bit busy these days, but I could probably get to it by February if you'd not write the code (school is starting in a few days)... :-)