2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Buffer.hh>
|
2010-08-05 21:51:19 +00:00
|
|
|
#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/QPDFWriter.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-08-05 21:51:19 +00:00
|
|
|
|
|
|
|
static char const* whoami = 0;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
usage()
|
2010-08-05 21:51:19 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:37:51 +00:00
|
|
|
// If there is a box of name box_name, replace it with a new box whose
|
|
|
|
// elements are double the values of the original box.
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
doubleBoxSize(QPDFPageObjectHelper& page, char const* box_name)
|
2010-08-05 21:51:19 +00:00
|
|
|
{
|
2022-03-02 18:37:51 +00:00
|
|
|
// We need to use getAttribute rather than getKey as some boxes could
|
|
|
|
// be inherited.
|
|
|
|
auto box = page.getAttribute(box_name, true);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (box.isNull()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!box.isRectangle()) {
|
|
|
|
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;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (auto& item : box.aitems()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
doubled.push_back(
|
2022-03-02 18:37:51 +00:00
|
|
|
QPDFObjectHandle::newReal(item.getNumericValue() * 2.0, 2));
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
page.getObjectHandle().replaceKey(
|
|
|
|
box_name, QPDFObjectHandle::newArray(doubled));
|
2010-08-05 21:51:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2010-08-05 21:51:19 +00:00
|
|
|
{
|
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
|
2013-11-30 03:08:25 +00:00
|
|
|
// For test suite
|
|
|
|
bool static_id = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) {
|
2013-11-30 03:08:25 +00:00
|
|
|
static_id = true;
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +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
|
|
|
|
2022-04-02 21:14:10 +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-04-02 21:14:10 +00:00
|
|
|
for (auto& page : QPDFPageDocumentHelper(qpdf).getAllPages()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Prepend the buffer to the page's contents
|
2022-03-02 18:37:51 +00:00
|
|
|
page.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);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (static_id) {
|
2022-03-02 18:37:51 +00:00
|
|
|
// For the test suite, uncompress streams and use static IDs.
|
2022-02-08 14:18:08 +00:00
|
|
|
w.setStaticID(true); // for testing only
|
|
|
|
w.setStreamDataMode(qpdf_s_uncompress);
|
|
|
|
}
|
|
|
|
w.write();
|
|
|
|
std::cout << whoami << ": new file written to " << outfilename
|
|
|
|
<< std::endl;
|
2022-04-02 21:14:10 +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;
|
|
|
|
}
|