aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pdf-npages.cc
blob: 59598cfdcfd6025037d30deed6a5a46fa733d065 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdlib>
#include <cstring>
#include <iostream>

#include <qpdf/QPDF.hh>
#include <qpdf/QUtil.hh>

static char const* whoami = nullptr;

void
usage()
{
    std::cerr << "Usage: " << whoami << " filename" << std::endl
              << "Prints the number of pages in filename" << std::endl;
    exit(2);
}

int
main(int argc, char* argv[])
{
    whoami = QUtil::getWhoami(argv[0]);

    if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) {
        std::cout << whoami << " version 1.3" << std::endl;
        exit(0);
    }

    if (argc != 2) {
        usage();
    }
    char const* filename = argv[1];

    try {
        QPDF pdf;
        pdf.processFile(filename);
        QPDFObjectHandle root = pdf.getRoot();
        QPDFObjectHandle pages = root.getKey("/Pages");
        QPDFObjectHandle count = pages.getKey("/Count");
        std::cout << count.getIntValue() << std::endl;
    } catch (std::exception& e) {
        std::cerr << whoami << ": " << e.what() << std::endl;
        exit(2);
    }

    return 0;
}