2014-06-07 17:04:30 +00:00
|
|
|
//
|
|
|
|
// This is a stand-alone example of splitting a PDF into individual
|
2017-08-05 14:01:34 +00:00
|
|
|
// pages. It does essentially the same thing that qpdf --split-pages
|
|
|
|
// does.
|
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-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2019-03-12 14:05:29 +00:00
|
|
|
|
2014-06-07 17:04:30 +00:00
|
|
|
#include <iostream>
|
2019-03-12 14:05:29 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
2019-06-22 01:49:12 +00:00
|
|
|
#include <cstring>
|
2014-06-07 17:04:30 +00:00
|
|
|
|
2017-07-30 12:44:45 +00:00
|
|
|
static char const* whoami = 0;
|
2014-06-07 17:04:30 +00:00
|
|
|
static bool static_id = false;
|
|
|
|
|
|
|
|
static void process(char const* whoami,
|
|
|
|
char const* infile,
|
|
|
|
std::string outprefix)
|
|
|
|
{
|
|
|
|
QPDF inpdf;
|
|
|
|
inpdf.processFile(infile);
|
2018-06-18 19:06:51 +00:00
|
|
|
std::vector<QPDFPageObjectHelper> pages =
|
|
|
|
QPDFPageDocumentHelper(inpdf).getAllPages();
|
2019-06-21 03:35:23 +00:00
|
|
|
int pageno_len =
|
|
|
|
QIntC::to_int(QUtil::uint_to_string(pages.size()).length());
|
2014-06-07 17:04:30 +00:00
|
|
|
int pageno = 0;
|
2018-06-18 19:06:51 +00:00
|
|
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
2014-06-07 17:04:30 +00:00
|
|
|
iter != pages.end(); ++iter)
|
|
|
|
{
|
2018-06-18 19:06:51 +00:00
|
|
|
QPDFPageObjectHelper& page(*iter);
|
2014-06-07 17:04:30 +00:00
|
|
|
std::string outfile =
|
|
|
|
outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf";
|
|
|
|
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());
|
|
|
|
if (static_id)
|
|
|
|
{
|
|
|
|
// For the test suite, uncompress streams and use static
|
|
|
|
// IDs.
|
2015-11-01 21:39:15 +00:00
|
|
|
outpdfw.setStaticID(true); // for testing only
|
2014-06-07 17:04:30 +00:00
|
|
|
outpdfw.setStreamDataMode(qpdf_s_uncompress);
|
|
|
|
}
|
|
|
|
outpdfw.write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 12:44:45 +00:00
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << whoami << " infile outprefix" << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2014-06-07 17:04:30 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2017-07-30 12:44:45 +00:00
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
2014-06-07 17:04:30 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2017-07-30 12:44:45 +00:00
|
|
|
usage();
|
2014-06-07 17:04:30 +00:00
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
process(whoami, argv[1], argv[2]);
|
|
|
|
}
|
2018-08-13 01:57:52 +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;
|
|
|
|
}
|