I see that pdf-viewers like okular and evince are able to display the index of a pdf document (book) very well, with link to every paragraph. How can they do so? They use poppler library, how could I do extract that index with poppler, or in general?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
it just stops at first level (recursion needed to go more deeply)
toc=document->toc();
QDomElement docElem = toc->documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
qDebug("elem %s\n",qPrintable(e.tagName())); // the node really is an element.
}
n = n.nextSibling();
}
回答2:
Here is a demo how to do this with poppler in Python:
import poppler
def walk_index(iterp, doc):
while iterp.next():
link=iterp.get_action()
s = doc.find_dest(link.dest.named_dest)
print link.title,' ', doc.get_page(s.page_num).get_label()
child = iterp.get_child()
if child:
walk_index(child, doc)
def main():
uri = ("file:///"+path_to_pdf)
doc = poppler.document_new_from_file(uri, None)
iterp = poppler.IndexIter(doc)
link = iterp.get_action()
s = doc.find_dest(link.dest.named_dest)
print link.title,' ', doc.get_page(s.page_num).get_label()
walk_index(iterp, doc)
return 0
if __name__ == '__main__':
main()
python poppler
library is obsolete, here is how to do it with Gobject:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# walk to table of contents and print titles and pages
import sys
from gi.repository import Poppler
def walk_index(iterp, doc):
while iterp.next():
link=iterp.get_action()
dest=doc.find_dest(link.goto_dest.dest.named_dest)
s = doc.get_page(dest.page_num-1)
print link.goto_dest.title, dest.page_num, s.get_label()
child = iterp.get_child()
if child:
walk_index(child, doc)
def main():
uri = ("file:///"+sys.argv[1])
doc = Poppler.Document.new_from_file(uri, None)
iterp = Poppler.IndexIter.new(doc)
link = iterp.get_action()
dest=doc.find_dest(link.goto_dest.dest.named_dest)
s = doc.get_page(dest.page_num-1)
print link.goto_dest.title, dest.page_num, s.get_label()
walk_index(iterp, doc)
return 0
if __name__ == '__main__':
main()