class HexaPDF::CLI::Merge

Merges pages from multiple PDF files.

Private Instance Methods

import_page(page_tree, source_index, page, rotation) click to toggle source

Import the page with the given rotation into the page tree.

# File lib/hexapdf/cli/merge.rb, line 176
def import_page(page_tree, source_index, page, rotation)
  if page_tree.document == page.document
    page.value.update(page.copy_inherited_values)
    page = page.deep_copy unless source_index == 0
  else
    page = page_tree.document.import(page).deep_copy
  end
  if rotation == :none
    page.delete(:Rotate)
  elsif rotation.kind_of?(Integer)
    page.rotate(rotation)
  end
  page_tree.document.add(page)
  page_tree.add_page(page)
end
import_pages(page_tree) click to toggle source

Imports the pages of the document as specified with the –pages option to the given page tree.

# File lib/hexapdf/cli/merge.rb, line 145
def import_pages(page_tree)
  @files.each do |s|
    page_list = s.file.pages.to_a
    s.pages = parse_pages_specification(s.pages, s.file.pages.count)
    s.pages.each {|arr| arr[0] = page_list[arr[0]] }
  end

  if @interleave
    max_pages_per_file = 0
    all = @files.each_with_index.map do |spec, findex|
      list = []
      spec.pages.each {|index, rotation| list << [spec.file, findex, index, rotation] }
      max_pages_per_file = list.size if list.size > max_pages_per_file
      list
    end
    first, *rest = *all
    first[max_pages_per_file - 1] ||= nil
    first.zip(*rest) do |slice|
      slice.each do |source, findex, page, rotation|
        next unless source
        import_page(page_tree, findex, page, rotation)
      end
    end
  else
    @files.each_with_index do |s, findex|
      s.pages.each {|page, rotation| import_page(page_tree, findex, page, rotation) }
    end
  end
end