一直在尝试使用 Ruby 和 Poppler 获取 PDF 文件的目录/索引。我非常感谢任何正确方向的建议或提示,我成功地获得了主要章节,但不是每章下的子标题。前任
1.Chapter 1
--Part of Chap1
-- Part of Chap 1
2.Chapter 2
-- Part of Chap2
-- Part of Chap2
使用 ruby 和 poppler 时,我可以获得第 1章和第 2 章,但不能获得第 1 章/第 2 章的部分。我可能会以错误的方式思考这个问题,但据我了解,如果孩子存在Poppler::IndexIter
,我会假设用于获得第 1 章和第 2 章的相同方法也适用于孩子,但这似乎并不适用就是这样。尝试在子类上使用 action.title,但它似乎为零。我目前使用的 Pdf 出现在所有带有子章节的 PDF 阅读器中。
http://ruby-gnome2.sourceforge.jp/hiki.cgi?Poppler%3A%3AIndexIter#open%3F
#!/usr/bin/env ruby
require "rubygems"
require "poppler"
if ARGV.size < 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)