title: Ruby/Poppler PDF Table of Contents/Index
body: <p>Have been trying to obtain the TOC/Index for PDF files using Ruby and Poppler. I would really appreciate any suggestions or hints in the right direction I am successful at getting the major Chapters, but not the sub headings under each chapter. ex</p>
  
  <pre><code>1.Chapter 1
    --Part of Chap1
    -- Part of Chap 1
  2.Chapter 2
     -- Part of Chap2 
     -- Part of Chap2
  </code></pre>
  
  <p>I am able to obtain <strong>Chapter 1</strong> and <strong>Chapter 2</strong> but not the <strong>part of Chap1/chap 2</strong> when using ruby and poppler. I might be thinking  about this the wrong way, but from what I understood , if a child exist for <code>Poppler::IndexIter</code> , I would assume the same methods used to obtain Chapter 1 and Chapter 2 would work for the child as well, however this does not appear to be the case. Tried to use action.title on the child class, however it appears to be nil. The Pdf i am currently using shows up in all PDF readers with sub chapters.</p>
  
  <p><a href="http://ruby-gnome2.sourceforge.jp/hiki.cgi?Poppler%3A%3AIndexIter#open%3F" rel="nofollow">http://ruby-gnome2.sourceforge.jp/hiki.cgi?Poppler%3A%3AIndexIter#open%3F</a></p>
  
  <pre><code>#!/usr/bin/env ruby
  
  require "rubygems"
  require "poppler"
  
  
  if ARGV.size &lt; 1
    puts "usage: #{$0} input.pdf"
    exit(-1)
  end
  
  input = ARGV.shift
  input_uri = "file://#{File.expand_path(input)}"
  
  doc = Poppler::Document.new(input_uri)
  indexer = Poppler::IndexIter.new(doc)
  author = doc.n_pages
  
  index = doc.index_iter
  pages = doc.n_pages
  #puts "  #{doc.metadata}\n"
  puts "This is the number of pages  #{pages}\n"
  
  
  #Prints out  Table of Contents each chater
  
  def walk_index(indexer)
      indexer.each do |i|
  
      chaps =i.action.title
      puts chaps
      child = i.child
  
      workk_index(child) if child.nil? == false
      end
  end
  
  def work_index(child)
      #puts child
      child.each do |h|
          puts h.action.title 
      end
  end
  
  walk_index(indexer)
  </code></pre>