Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 3rd, 2011  |  syntax: Ruby  |  size: 1.32 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env ruby
  2.  
  3. require 'poppler'
  4.  
  5. class PDFParser
  6.   def initialize(params = nil)
  7.     @loaded = false
  8.     if (params != nil) then
  9.       @file = params[:file] || nil
  10.       if (@file != nil) then
  11.         loadFile(@file)
  12.       end
  13.     else
  14.       @file = nil
  15.     end
  16.   end
  17.  
  18.   def loadFile(fileName)
  19.     @loaded = false
  20.     if (!File.exists?(fileName)) then
  21.       raise 'file not found ' + fileName
  22.     end
  23.     @file = fileName
  24.     @poppler = Poppler::Document.new(@file)
  25.     pageCount = 0;
  26.     @poppler.each do |page|
  27.       pageCount+=1
  28.     end
  29.     @pageCount = pageCount
  30.     @loaded = true
  31.   end
  32.  
  33.   def DumpLinks
  34.     if (!@loaded) then
  35.       loadFile(@file)
  36.     end
  37.     @poppler.each do |page|
  38.       links = page.link_mapping
  39.       links.each do |link|
  40.         action = link.action
  41.         if(action.is_a?(Poppler::ActionGotoDest))
  42.           puts "Destination: page_num: %d, top: %f, left: %f, right: %f, zoom: %f, dest: %s" %
  43.             [action.dest.page_num, action.dest.top, action.dest.left, action.dest.right, action.dest.zoom, action.dest.named_dest]
  44.           namedDest = action.dest.named_dest
  45.           puts namedDest
  46.           if (namedDest != nil) then
  47.             dest = @poppler.find_dest(namedDest)
  48.             puts dest
  49.           end
  50.         end
  51.       end
  52.     end
  53.   end
  54. end
  55.  
  56.  
clone this paste RAW Paste Data