Commit 78aa3b6c2bf6db9931a760a8a93ee2cf96b17123

Authored by m-holger
Committed by Jay Berkenbilt
1 parent c0fc776b

Tidy example pdf-double-page-size

Also fix typo in pdf-attach-file example.
examples/pdf-attach-file.cc
... ... @@ -98,7 +98,7 @@ static void process(char const* infilename, char const* password,
98 98 // apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
99 99 // apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
100 100 // apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]"));
101   - apdict.replaceKey("/Resources", "<< >>"_qpdf"");
  101 + apdict.replaceKey("/Resources", "<< >>"_qpdf);
102 102 apdict.replaceKey("/Type", "/XObject"_qpdf);
103 103 apdict.replaceKey("/Subtype", "/Form"_qpdf);
104 104 apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);
... ...
examples/pdf-double-page-size.cc
... ... @@ -19,28 +19,30 @@ void usage()
19 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 29 if (box.isNull())
28 30 {
29 31 return;
30 32 }
31   - if (! (box.isArray() && (box.getArrayNItems() == 4)))
  33 + if (! box.isRectangle())
32 34 {
33 35 throw std::runtime_error(std::string("box ") + box_name +
34 36 " is not an array of four elements");
35 37 }
36 38 std::vector<QPDFObjectHandle> doubled;
37   - for (int i = 0; i < 4; ++i)
  39 + for (auto& item : box.aitems())
38 40 {
39 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 48 int main(int argc, char* argv[])
... ... @@ -79,17 +81,10 @@ int main(int argc, char* argv[])
79 81 QPDF qpdf;
80 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 86 // Prepend the buffer to the page's contents
92   - ph.addPageContents(
  87 + page.addPageContents(
93 88 QPDFObjectHandle::newStream(&qpdf, content), true);
94 89  
95 90 // Double the size of each of the content boxes
... ... @@ -104,8 +99,7 @@ int main(int argc, char* argv[])
104 99 QPDFWriter w(qpdf, outfilename);
105 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 103 w.setStaticID(true); // for testing only
110 104 w.setStreamDataMode(qpdf_s_uncompress);
111 105 }
... ...