Commit 78aa3b6c2bf6db9931a760a8a93ee2cf96b17123
Committed by
Jay Berkenbilt
1 parent
c0fc776b
Tidy example pdf-double-page-size
Also fix typo in pdf-attach-file example.
Showing
2 changed files
with
15 additions
and
21 deletions
examples/pdf-attach-file.cc
| @@ -98,7 +98,7 @@ static void process(char const* infilename, char const* password, | @@ -98,7 +98,7 @@ static void process(char const* infilename, char const* password, | ||
| 98 | // apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject")); | 98 | // apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject")); |
| 99 | // apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form")); | 99 | // apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form")); |
| 100 | // apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]")); | 100 | // apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]")); |
| 101 | - apdict.replaceKey("/Resources", "<< >>"_qpdf""); | 101 | + apdict.replaceKey("/Resources", "<< >>"_qpdf); |
| 102 | apdict.replaceKey("/Type", "/XObject"_qpdf); | 102 | apdict.replaceKey("/Type", "/XObject"_qpdf); |
| 103 | apdict.replaceKey("/Subtype", "/Form"_qpdf); | 103 | apdict.replaceKey("/Subtype", "/Form"_qpdf); |
| 104 | apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf); | 104 | apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf); |
examples/pdf-double-page-size.cc
| @@ -19,28 +19,30 @@ void usage() | @@ -19,28 +19,30 @@ void usage() | ||
| 19 | exit(2); | 19 | exit(2); |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | -static void doubleBoxSize(QPDFObjectHandle& page, char const* box_name) | 22 | +// If there is a box of name box_name, replace it with a new box whose |
| 23 | +// elements are double the values of the original box. | ||
| 24 | +static void doubleBoxSize(QPDFPageObjectHelper& page, char const* box_name) | ||
| 23 | { | 25 | { |
| 24 | - // If there is a box of this name, replace it with a new box whose | ||
| 25 | - // elements are double the values of the original box. | ||
| 26 | - QPDFObjectHandle box = page.getKey(box_name); | 26 | + // We need to use getAttribute rather than getKey as some boxes could |
| 27 | + // be inherited. | ||
| 28 | + auto box = page.getAttribute(box_name, true); | ||
| 27 | if (box.isNull()) | 29 | if (box.isNull()) |
| 28 | { | 30 | { |
| 29 | return; | 31 | return; |
| 30 | } | 32 | } |
| 31 | - if (! (box.isArray() && (box.getArrayNItems() == 4))) | 33 | + if (! box.isRectangle()) |
| 32 | { | 34 | { |
| 33 | throw std::runtime_error(std::string("box ") + box_name + | 35 | throw std::runtime_error(std::string("box ") + box_name + |
| 34 | " is not an array of four elements"); | 36 | " is not an array of four elements"); |
| 35 | } | 37 | } |
| 36 | std::vector<QPDFObjectHandle> doubled; | 38 | std::vector<QPDFObjectHandle> doubled; |
| 37 | - for (int i = 0; i < 4; ++i) | 39 | + for (auto& item : box.aitems()) |
| 38 | { | 40 | { |
| 39 | doubled.push_back( | 41 | doubled.push_back( |
| 40 | - QPDFObjectHandle::newReal( | ||
| 41 | - box.getArrayItem(i).getNumericValue() * 2.0, 2)); | 42 | + QPDFObjectHandle::newReal(item.getNumericValue() * 2.0, 2)); |
| 42 | } | 43 | } |
| 43 | - page.replaceKey(box_name, QPDFObjectHandle::newArray(doubled)); | 44 | + page.getObjectHandle() |
| 45 | + .replaceKey(box_name, QPDFObjectHandle::newArray(doubled)); | ||
| 44 | } | 46 | } |
| 45 | 47 | ||
| 46 | int main(int argc, char* argv[]) | 48 | int main(int argc, char* argv[]) |
| @@ -79,17 +81,10 @@ int main(int argc, char* argv[]) | @@ -79,17 +81,10 @@ int main(int argc, char* argv[]) | ||
| 79 | QPDF qpdf; | 81 | QPDF qpdf; |
| 80 | qpdf.processFile(infilename, password); | 82 | qpdf.processFile(infilename, password); |
| 81 | 83 | ||
| 82 | - std::vector<QPDFPageObjectHelper> pages = | ||
| 83 | - QPDFPageDocumentHelper(qpdf).getAllPages(); | ||
| 84 | - for (std::vector<QPDFPageObjectHelper>::iterator iter = | ||
| 85 | - pages.begin(); | ||
| 86 | - iter != pages.end(); ++iter) | 84 | + for (auto& page : QPDFPageDocumentHelper(qpdf).getAllPages()) |
| 87 | { | 85 | { |
| 88 | - QPDFPageObjectHelper& ph(*iter); | ||
| 89 | - QPDFObjectHandle page = ph.getObjectHandle(); | ||
| 90 | - | ||
| 91 | // Prepend the buffer to the page's contents | 86 | // Prepend the buffer to the page's contents |
| 92 | - ph.addPageContents( | 87 | + page.addPageContents( |
| 93 | QPDFObjectHandle::newStream(&qpdf, content), true); | 88 | QPDFObjectHandle::newStream(&qpdf, content), true); |
| 94 | 89 | ||
| 95 | // Double the size of each of the content boxes | 90 | // Double the size of each of the content boxes |
| @@ -104,8 +99,7 @@ int main(int argc, char* argv[]) | @@ -104,8 +99,7 @@ int main(int argc, char* argv[]) | ||
| 104 | QPDFWriter w(qpdf, outfilename); | 99 | QPDFWriter w(qpdf, outfilename); |
| 105 | if (static_id) | 100 | if (static_id) |
| 106 | { | 101 | { |
| 107 | - // For the test suite, uncompress streams and use static | ||
| 108 | - // IDs. | 102 | + // For the test suite, uncompress streams and use static IDs. |
| 109 | w.setStaticID(true); // for testing only | 103 | w.setStaticID(true); // for testing only |
| 110 | w.setStreamDataMode(qpdf_s_uncompress); | 104 | w.setStreamDataMode(qpdf_s_uncompress); |
| 111 | } | 105 | } |