2014-06-07 17:04:30 +00:00
|
|
|
//
|
2023-05-27 17:19:52 +00:00
|
|
|
// This is a stand-alone example of splitting a PDF into individual pages. It does essentially the
|
|
|
|
// same thing that qpdf --split-pages does.
|
2014-06-07 17:04:30 +00:00
|
|
|
//
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2014-06-07 17:04:30 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
2018-06-18 19:06:51 +00:00
|
|
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
2014-06-07 17:04:30 +00:00
|
|
|
#include <qpdf/QPDFWriter.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2019-03-12 14:05:29 +00:00
|
|
|
|
2023-05-20 18:13:09 +00:00
|
|
|
#include <cstdlib>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <cstring>
|
2014-06-07 17:04:30 +00:00
|
|
|
#include <iostream>
|
2019-03-12 14:05:29 +00:00
|
|
|
#include <string>
|
2014-06-07 17:04:30 +00:00
|
|
|
|
|
|
|
static bool static_id = false;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
process(char const* whoami, char const* infile, std::string outprefix)
|
2014-06-07 17:04:30 +00:00
|
|
|
{
|
|
|
|
QPDF inpdf;
|
|
|
|
inpdf.processFile(infile);
|
2023-05-21 17:35:09 +00:00
|
|
|
std::vector<QPDFPageObjectHelper> pages = QPDFPageDocumentHelper(inpdf).getAllPages();
|
2022-09-21 16:49:21 +00:00
|
|
|
int pageno_len = QIntC::to_int(std::to_string(pages.size()).length());
|
2014-06-07 17:04:30 +00:00
|
|
|
int pageno = 0;
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& page: pages) {
|
2023-05-21 17:35:09 +00:00
|
|
|
std::string outfile = outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf";
|
2014-06-07 17:04:30 +00:00
|
|
|
QPDF outpdf;
|
|
|
|
outpdf.emptyPDF();
|
2018-06-18 19:06:51 +00:00
|
|
|
QPDFPageDocumentHelper(outpdf).addPage(page, false);
|
2014-06-07 17:04:30 +00:00
|
|
|
QPDFWriter outpdfw(outpdf, outfile.c_str());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (static_id) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// For the test suite, uncompress streams and use static IDs.
|
2022-02-08 14:18:08 +00:00
|
|
|
outpdfw.setStaticID(true); // for testing only
|
|
|
|
outpdfw.setStreamDataMode(qpdf_s_uncompress);
|
|
|
|
}
|
2014-06-07 17:04:30 +00:00
|
|
|
outpdfw.write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
usage(char const* whoami)
|
2017-07-30 12:44:45 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << whoami << " infile outprefix" << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2014-06-07 17:04:30 +00:00
|
|
|
{
|
2020-04-16 15:43:37 +00:00
|
|
|
char const* whoami = QUtil::getWhoami(argv[0]);
|
2014-06-07 17:04:30 +00:00
|
|
|
|
|
|
|
// For test suite
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) {
|
2014-06-07 17:04:30 +00:00
|
|
|
static_id = true;
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (argc != 3) {
|
2020-04-16 15:43:37 +00:00
|
|
|
usage(whoami);
|
2014-06-07 17:04:30 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2014-06-07 17:04:30 +00:00
|
|
|
process(whoami, argv[1], argv[2]);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception const& e) {
|
2014-06-07 17:04:30 +00:00
|
|
|
std::cerr << whoami << ": exception: " << e.what() << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|