diff options
author | Jay Berkenbilt <ejb@ql.org> | 2014-06-07 19:04:30 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2014-06-07 22:40:38 +0200 |
commit | 0b2e9cb16886d422e6645a5d65c74a340ae6deff (patch) | |
tree | 6942cf401c7c4a3b2746c5c463a6291976d51217 /examples/pdf-split-pages.cc | |
parent | 9f8aba1db7f101e424cbc2c742abe92868cc4bff (diff) | |
download | qpdf-0b2e9cb16886d422e6645a5d65c74a340ae6deff.tar.zst |
Example: fast split into single pages
This is faster than using qpdf --pages to do it.
Diffstat (limited to 'examples/pdf-split-pages.cc')
-rw-r--r-- | examples/pdf-split-pages.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/examples/pdf-split-pages.cc b/examples/pdf-split-pages.cc new file mode 100644 index 00000000..0a70ed8b --- /dev/null +++ b/examples/pdf-split-pages.cc @@ -0,0 +1,77 @@ +// +// This is a stand-alone example of splitting a PDF into individual +// pages. It is much faster than using the qpdf command-line tool to +// split into separate files per page. +// + +#include <qpdf/QPDF.hh> +#include <qpdf/QPDFWriter.hh> +#include <qpdf/QUtil.hh> +#include <string> +#include <iostream> +#include <cstdlib> + +static bool static_id = false; + +static void process(char const* whoami, + char const* infile, + std::string outprefix) +{ + QPDF inpdf; + inpdf.processFile(infile); + std::vector<QPDFObjectHandle> const& pages = inpdf.getAllPages(); + int pageno_len = QUtil::int_to_string(pages.size()).length(); + int pageno = 0; + for (std::vector<QPDFObjectHandle>::const_iterator iter = pages.begin(); + iter != pages.end(); ++iter) + { + QPDFObjectHandle page = *iter; + std::string outfile = + outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf"; + QPDF outpdf; + outpdf.emptyPDF(); + outpdf.addPage(page, false); + QPDFWriter outpdfw(outpdf, outfile.c_str()); + if (static_id) + { + // For the test suite, uncompress streams and use static + // IDs. + outpdfw.setStaticID(true); + outpdfw.setStreamDataMode(qpdf_s_uncompress); + } + outpdfw.write(); + } +} + +int main(int argc, char* argv[]) +{ + char* whoami = QUtil::getWhoami(argv[0]); + + // For libtool's sake.... + if (strncmp(whoami, "lt-", 3) == 0) + { + whoami += 3; + } + // For test suite + if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) + { + static_id = true; + --argc; + ++argv; + } + + if (argc != 3) + { + std::cerr << "Usage: " << whoami << " infile outprefix" << std::endl; + } + try + { + process(whoami, argv[1], argv[2]); + } + catch (std::exception e) + { + std::cerr << whoami << ": exception: " << e.what() << std::endl; + return 2; + } + return 0; +} |