2010-08-05 21:51:19 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <qpdf/QPDF.hh>
|
2018-06-18 19:06:51 +00:00
|
|
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
2010-08-05 21:51:19 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/Buffer.hh>
|
|
|
|
#include <qpdf/QPDFWriter.hh>
|
|
|
|
|
|
|
|
static char const* whoami = 0;
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf [in-password]"
|
2022-02-08 14:18:08 +00:00
|
|
|
<< std::endl
|
|
|
|
<< "Double size of all pages in infile.pdf;"
|
|
|
|
<< " write output to outfile.pdf" << std::endl;
|
2010-08-05 21:51:19 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doubleBoxSize(QPDFObjectHandle& page, char const* box_name)
|
|
|
|
{
|
2010-08-06 00:38:30 +00:00
|
|
|
// If there is a box of this name, replace it with a new box whose
|
|
|
|
// elements are double the values of the original box.
|
2010-08-05 21:51:19 +00:00
|
|
|
QPDFObjectHandle box = page.getKey(box_name);
|
|
|
|
if (box.isNull())
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
if (! (box.isArray() && (box.getArrayNItems() == 4)))
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::runtime_error(std::string("box ") + box_name +
|
|
|
|
" is not an array of four elements");
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
std::vector<QPDFObjectHandle> doubled;
|
2019-06-21 03:35:23 +00:00
|
|
|
for (int i = 0; i < 4; ++i)
|
2010-08-05 21:51:19 +00:00
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
doubled.push_back(
|
|
|
|
QPDFObjectHandle::newReal(
|
2012-06-27 14:43:27 +00:00
|
|
|
box.getArrayItem(i).getNumericValue() * 2.0, 2));
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
page.replaceKey(box_name, QPDFObjectHandle::newArray(doubled));
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
|
|
|
|
// For libtool's sake....
|
|
|
|
if (strncmp(whoami, "lt-", 3) == 0)
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
whoami += 3;
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
|
2013-11-30 03:08:25 +00:00
|
|
|
// For test suite
|
|
|
|
bool static_id = false;
|
|
|
|
if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0))
|
|
|
|
{
|
|
|
|
static_id = true;
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
2010-08-05 21:51:19 +00:00
|
|
|
if (! ((argc == 3) || (argc == 4)))
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
usage();
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char const* infilename = argv[1];
|
|
|
|
char const* outfilename = argv[2];
|
|
|
|
char const* password = (argc == 4) ? argv[3] : "";
|
|
|
|
|
|
|
|
// Text to prepend to each page's contents
|
2012-07-26 02:32:14 +00:00
|
|
|
std::string content = "2 0 0 2 0 0 cm\n";
|
2010-08-05 21:51:19 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDF qpdf;
|
|
|
|
qpdf.processFile(infilename, password);
|
2010-08-05 21:51:19 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
std::vector<QPDFPageObjectHelper> pages =
|
2018-06-18 19:06:51 +00:00
|
|
|
QPDFPageDocumentHelper(qpdf).getAllPages();
|
2022-02-08 14:18:08 +00:00
|
|
|
for (std::vector<QPDFPageObjectHelper>::iterator iter =
|
2018-06-18 19:06:51 +00:00
|
|
|
pages.begin();
|
2022-02-08 14:18:08 +00:00
|
|
|
iter != pages.end(); ++iter)
|
|
|
|
{
|
2018-06-18 19:06:51 +00:00
|
|
|
QPDFPageObjectHelper& ph(*iter);
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle page = ph.getObjectHandle();
|
2010-08-05 21:51:19 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Prepend the buffer to the page's contents
|
|
|
|
ph.addPageContents(
|
2012-07-26 02:32:14 +00:00
|
|
|
QPDFObjectHandle::newStream(&qpdf, content), true);
|
2010-08-05 21:51:19 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Double the size of each of the content boxes
|
|
|
|
doubleBoxSize(page, "/MediaBox");
|
|
|
|
doubleBoxSize(page, "/CropBox");
|
|
|
|
doubleBoxSize(page, "/BleedBox");
|
|
|
|
doubleBoxSize(page, "/TrimBox");
|
|
|
|
doubleBoxSize(page, "/ArtBox");
|
|
|
|
}
|
2010-08-05 21:51:19 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Write out a new file
|
|
|
|
QPDFWriter w(qpdf, outfilename);
|
|
|
|
if (static_id)
|
|
|
|
{
|
|
|
|
// For the test suite, uncompress streams and use static
|
|
|
|
// IDs.
|
|
|
|
w.setStaticID(true); // for testing only
|
|
|
|
w.setStreamDataMode(qpdf_s_uncompress);
|
|
|
|
}
|
|
|
|
w.write();
|
|
|
|
std::cout << whoami << ": new file written to " << outfilename
|
|
|
|
<< std::endl;
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << whoami << " processing file " << infilename << ": "
|
|
|
|
<< e.what() << std::endl;
|
|
|
|
exit(2);
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|