2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Tidy example pdf-double-page-size

Also fix typo in pdf-attach-file example.
This commit is contained in:
m-holger 2022-03-02 18:37:51 +00:00 committed by Jay Berkenbilt
parent c0fc776ba4
commit 78aa3b6c2b
2 changed files with 15 additions and 21 deletions

View File

@ -98,7 +98,7 @@ static void process(char const* infilename, char const* password,
// apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject")); // apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
// apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form")); // apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
// apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]")); // apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]"));
apdict.replaceKey("/Resources", "<< >>"_qpdf""); apdict.replaceKey("/Resources", "<< >>"_qpdf);
apdict.replaceKey("/Type", "/XObject"_qpdf); apdict.replaceKey("/Type", "/XObject"_qpdf);
apdict.replaceKey("/Subtype", "/Form"_qpdf); apdict.replaceKey("/Subtype", "/Form"_qpdf);
apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf); apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);

View File

@ -19,28 +19,30 @@ void usage()
exit(2); exit(2);
} }
static void doubleBoxSize(QPDFObjectHandle& page, char const* box_name) // 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.
static void doubleBoxSize(QPDFPageObjectHelper& page, char const* box_name)
{ {
// If there is a box of this name, replace it with a new box whose // We need to use getAttribute rather than getKey as some boxes could
// elements are double the values of the original box. // be inherited.
QPDFObjectHandle box = page.getKey(box_name); auto box = page.getAttribute(box_name, true);
if (box.isNull()) if (box.isNull())
{ {
return; return;
} }
if (! (box.isArray() && (box.getArrayNItems() == 4))) if (! box.isRectangle())
{ {
throw std::runtime_error(std::string("box ") + box_name + throw std::runtime_error(std::string("box ") + box_name +
" is not an array of four elements"); " is not an array of four elements");
} }
std::vector<QPDFObjectHandle> doubled; std::vector<QPDFObjectHandle> doubled;
for (int i = 0; i < 4; ++i) for (auto& item : box.aitems())
{ {
doubled.push_back( doubled.push_back(
QPDFObjectHandle::newReal( QPDFObjectHandle::newReal(item.getNumericValue() * 2.0, 2));
box.getArrayItem(i).getNumericValue() * 2.0, 2));
} }
page.replaceKey(box_name, QPDFObjectHandle::newArray(doubled)); page.getObjectHandle()
.replaceKey(box_name, QPDFObjectHandle::newArray(doubled));
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@ -79,17 +81,10 @@ int main(int argc, char* argv[])
QPDF qpdf; QPDF qpdf;
qpdf.processFile(infilename, password); qpdf.processFile(infilename, password);
std::vector<QPDFPageObjectHelper> pages = for (auto& page : QPDFPageDocumentHelper(qpdf).getAllPages())
QPDFPageDocumentHelper(qpdf).getAllPages();
for (std::vector<QPDFPageObjectHelper>::iterator iter =
pages.begin();
iter != pages.end(); ++iter)
{ {
QPDFPageObjectHelper& ph(*iter);
QPDFObjectHandle page = ph.getObjectHandle();
// Prepend the buffer to the page's contents // Prepend the buffer to the page's contents
ph.addPageContents( page.addPageContents(
QPDFObjectHandle::newStream(&qpdf, content), true); QPDFObjectHandle::newStream(&qpdf, content), true);
// Double the size of each of the content boxes // Double the size of each of the content boxes
@ -104,8 +99,7 @@ int main(int argc, char* argv[])
QPDFWriter w(qpdf, outfilename); QPDFWriter w(qpdf, outfilename);
if (static_id) if (static_id)
{ {
// For the test suite, uncompress streams and use static // For the test suite, uncompress streams and use static IDs.
// IDs.
w.setStaticID(true); // for testing only w.setStaticID(true); // for testing only
w.setStreamDataMode(qpdf_s_uncompress); w.setStreamDataMode(qpdf_s_uncompress);
} }