diff options
Diffstat (limited to 'examples/qpdf-job.cc')
-rw-r--r-- | examples/qpdf-job.cc | 93 |
1 files changed, 83 insertions, 10 deletions
diff --git a/examples/qpdf-job.cc b/examples/qpdf-job.cc index 41ee8603..d1760897 100644 --- a/examples/qpdf-job.cc +++ b/examples/qpdf-job.cc @@ -1,20 +1,27 @@ #include <qpdf/QPDFJob.hh> #include <qpdf/QUtil.hh> +#include <qpdf/QIntC.hh> #include <iostream> #include <cstring> -// QXXXQ describe +// This program is a simple demonstration of different ways to use the +// QPDFJob API. static char const* whoami = 0; -#if 0 // QXXXQ -static void usage(std::string const& msg) +static void usage() { - std::cerr << "Usage: " << whoami << " QXXXQ" << std::endl; + std::cerr + << "Usage: " << whoami << std::endl + << "This program linearizes the first page of in.pdf to out1.pdf," + << " out2.pdf, and" + << std::endl + << " out3.pdf, each demonstrating a different way to use the" + << " QPDFJob API" + << std::endl; exit(2); } -#endif int main(int argc, char* argv[]) { @@ -26,24 +33,90 @@ int main(int argc, char* argv[]) whoami += 3; } + if (argc != 1) + { + usage(); + } + + // The examples below all catch std::exception. Note that + // QPDFUsage can be caught separately to report on errors in using + // the API itself. For CLI, this is command-line usage. For JSON + // or the API, it would be errors from the equivalent invocation. + + // Note that staticId is used for testing only. + try { + // Use the config API QPDFJob j; j.config() - ->inputFile("/tmp/1.pdf") - ->outputFile("/tmp/2.pdf") + ->inputFile("in.pdf") + ->outputFile("out1.pdf") ->pages() - ->pageSpec(".", "1-z") + ->pageSpec(".", "1") ->endPages() - ->qdf() + ->linearize() + ->staticId() // for testing only ->checkConfiguration(); j.run(); + std::cout << "out1 status: " << j.getExitCode() << std::endl; + } + catch (std::exception& e) + { + std::cerr << "exception: " << e.what() << std::endl; + return 2; + } + + try + { + char const* new_argv[] = { + whoami, + "in.pdf", + "out2.pdf", + "--linearize", + "--pages", + ".", + "1", + "--", + "--static-id", + nullptr + }; + QPDFJob j; + j.initializeFromArgv(9, new_argv); + j.run(); + std::cout << "out2 status: " << j.getExitCode() << std::endl; } catch (std::exception& e) { - // QXXXQ catch usage, configerror, whatever we end up with separately std::cerr << "exception: " << e.what() << std::endl; return 2; } + + try + { + // Use the JSON API + QPDFJob j; + j.initializeFromJson(R"({ + "inputFile": "in.pdf", + "outputFile": "out3.pdf", + "staticId": "", + "linearize": "", + "pages": [ + { + "file": ".", + "range": "1" + } + ] +} +)"); + j.run(); + std::cout << "out3 status: " << j.getExitCode() << std::endl; + } + catch (std::exception& e) + { + std::cerr << "exception: " << e.what() << std::endl; + return 2; + } + return 0; } |