aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pdf-npages.cc
blob: c62d33debcd97a7c05d2c7a32ac065ad72109cdf (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
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string.h>
#include <stdlib.h>

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

static char const* whoami = 0;

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]);

    // For libtool's sake....
    if (strncmp(whoami, "lt-", 3) == 0)
    {
	whoami += 3;
    }

    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;
}