2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-08 17:24:06 +00:00

Rewrite bookmark example to use outline helpers

Now uses QPDFOutlineDocumentHelper and QPDFOutlineObjectHelper.
This commit is contained in:
Jay Berkenbilt 2018-12-19 13:11:59 -05:00
parent d5d179f441
commit 4fbffdf8ed

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <qpdf/QPDF.hh> #include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh> #include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFOutlineDocumentHelper.hh>
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
#include <qpdf/QTC.hh> #include <qpdf/QTC.hh>
@ -56,9 +57,8 @@ void generate_page_map(QPDF& qpdf)
} }
} }
void extract_bookmarks(QPDFObjectHandle outlines, std::vector<int>& numbers) void show_bookmark_details(QPDFOutlineObjectHelper outline,
{ std::vector<int> numbers)
if (outlines.hasKey("/Title"))
{ {
// No default so gcc will warn on missing tag // No default so gcc will warn on missing tag
switch (style) switch (style)
@ -88,10 +88,10 @@ void extract_bookmarks(QPDFObjectHandle outlines, std::vector<int>& numbers)
if (show_open) if (show_open)
{ {
if (outlines.hasKey("/Count")) int count = outline.getCount();
if (count)
{ {
QTC::TC("examples", "pdf-bookmarks has count"); QTC::TC("examples", "pdf-bookmarks has count");
int count = outlines.getKey("/Count").getIntValue();
if (count > 0) if (count > 0)
{ {
// hierarchy is open at this point // hierarchy is open at this point
@ -115,53 +115,42 @@ void extract_bookmarks(QPDFObjectHandle outlines, std::vector<int>& numbers)
{ {
QTC::TC("examples", "pdf-bookmarks targets"); QTC::TC("examples", "pdf-bookmarks targets");
std::string target = "unknown"; std::string target = "unknown";
// Only explicit destinations supported for now QPDFObjectHandle dest_page = outline.getDestPage();
if (outlines.hasKey("/Dest")) if (! dest_page.isNull())
{ {
QTC::TC("examples", "pdf-bookmarks dest"); QTC::TC("examples", "pdf-bookmarks dest");
QPDFObjectHandle dest = outlines.getKey("/Dest"); QPDFObjGen og = dest_page.getObjGen();
if ((dest.isArray()) && (dest.getArrayNItems() > 0))
{
QPDFObjectHandle first = dest.getArrayItem(0);
QPDFObjGen og = first.getObjGen();
if (page_map.count(og)) if (page_map.count(og))
{ {
target = QUtil::int_to_string(page_map[og]); target = QUtil::int_to_string(page_map[og]);
} }
} }
std::cout << "[ -> " << target << " ] "; std::cout << "[ -> " << target << " ] ";
} }
std::cout << outline.getTitle() << std::endl;
} }
std::cout << outlines.getKey("/Title").getUTF8Value() << std::endl; void extract_bookmarks(std::list<QPDFOutlineObjectHelper> outlines,
} std::vector<int>& numbers)
if (outlines.hasKey("/First"))
{ {
numbers.push_back(0); numbers.push_back(0);
QPDFObjectHandle child = outlines.getKey("/First"); for (std::list<QPDFOutlineObjectHelper>::iterator iter = outlines.begin();
while (1) iter != outlines.end(); ++iter)
{ {
++(numbers.back()); ++(numbers.back());
bool has_next = child.hasKey("/Next"); show_bookmark_details(*iter, numbers);
std::list<QPDFOutlineObjectHelper>::iterator next = iter;
++next;
bool has_next = (next != outlines.end());
if ((style == st_lines) && (! has_next)) if ((style == st_lines) && (! has_next))
{ {
numbers.back() = 0; numbers.back() = 0;
} }
extract_bookmarks(child, numbers); extract_bookmarks((*iter).getKids(), numbers);
if (has_next)
{
child = child.getKey("/Next");
}
else
{
break;
}
} }
numbers.pop_back(); numbers.pop_back();
} }
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -233,15 +222,15 @@ int main(int argc, char* argv[])
QPDF qpdf; QPDF qpdf;
qpdf.processFile(filename, password); qpdf.processFile(filename, password);
QPDFObjectHandle root = qpdf.getRoot(); QPDFOutlineDocumentHelper odh(qpdf);
if (root.hasKey("/Outlines")) if (odh.hasOutlines())
{ {
std::vector<int> numbers; std::vector<int> numbers;
if (show_targets) if (show_targets)
{ {
generate_page_map(qpdf); generate_page_map(qpdf);
} }
extract_bookmarks(root.getKey("/Outlines"), numbers); extract_bookmarks(odh.getTopLevelOutlines(), numbers);
} }
else else
{ {