aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qpdfjob-remove-annotations.cc
blob: bc043d334f975914fb54f7edf6f23f1977bdbac7 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <qpdf/QPDFJob.hh>
#include <qpdf/QPDFUsage.hh>
#include <qpdf/QUtil.hh>

#include <cstdio>
#include <cstdlib>
#include <iostream>

// This example demonstrates how we can use the QPDFJob createQPDF and writeQPDF methods to add
// custom transformations to the output produced by QPDFJob runs. The example is a full copy of the
// qpdf program modified to allways remove all annotations from the final output.

static char const* whoami = nullptr;

static void
usageExit(std::string const& msg)
{
    std::cerr << std::endl
              << whoami << ": " << msg << std::endl
              << std::endl
              << "For help:" << std::endl
              << "  " << whoami << " --help=usage       usage information" << std::endl
              << "  " << whoami << " --help=topic       help on a topic" << std::endl
              << "  " << whoami << " --help=--option    help on an option" << std::endl
              << "  " << whoami << " --help             general help and a topic list" << std::endl
              << std::endl;
    exit(QPDFJob::EXIT_ERROR);
}

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

    QPDFJob j;
    try {
        // See "HOW TO ADD A COMMAND-LINE ARGUMENT" in README-maintainer.
        j.initializeFromArgv(argv);
        auto qpdf_sp = j.createQPDF();
        auto& pdf = *qpdf_sp;
        for (auto page: pdf.getAllPages()) {
            page.replaceKey("/Annots", "null"_qpdf);
        }
        j.writeQPDF(pdf);
    } catch (QPDFUsage& e) {
        usageExit(e.what());
    } catch (std::exception& e) {
        std::cerr << whoami << ": " << e.what() << std::endl;
        return QPDFJob::EXIT_ERROR;
    }
    return j.getExitCode();
}

#ifdef WINDOWS_WMAIN

extern "C" int
wmain(int argc, wchar_t* argv[])
{
    return QUtil::call_main_from_wmain(argc, argv, realmain);
}

#else

int
main(int argc, char* argv[])
{
    return realmain(argc, argv);
}

#endif