Commit 2e6e1204a51b4aba4657712a34e9447d928b5fc6
1 parent
2e7ee23b
Convert examples to use new page helper classes
Showing
9 changed files
with
60 additions
and
31 deletions
ChangeLog
| ... | ... | @@ -7,7 +7,9 @@ |
| 7 | 7 | of structural features of PDF while still staying true to qpdf's |
| 8 | 8 | philosophy of not isolating the user from the underlying |
| 9 | 9 | structure. Please see the chapter in the documentation entitled |
| 10 | - "Design and Library Notes" for additional discussion. | |
| 10 | + "Design and Library Notes" for additional discussion. The examples | |
| 11 | + have also been updated to use QPDFPageDocumentHelper and | |
| 12 | + QPDFPageObjctHelper when performing page-level operations. | |
| 11 | 13 | |
| 12 | 14 | 2018-06-19 Jay Berkenbilt <ejb@ql.org> |
| 13 | 15 | ... | ... |
examples/pdf-bookmarks.cc
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | #include <string.h> |
| 3 | 3 | #include <stdlib.h> |
| 4 | 4 | #include <qpdf/QPDF.hh> |
| 5 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 5 | 6 | #include <qpdf/QUtil.hh> |
| 6 | 7 | #include <qpdf/QTC.hh> |
| 7 | 8 | |
| ... | ... | @@ -44,12 +45,13 @@ void print_lines(std::vector<int>& numbers) |
| 44 | 45 | |
| 45 | 46 | void generate_page_map(QPDF& qpdf) |
| 46 | 47 | { |
| 47 | - std::vector<QPDFObjectHandle> pages = qpdf.getAllPages(); | |
| 48 | + QPDFPageDocumentHelper dh(qpdf); | |
| 49 | + std::vector<QPDFPageObjectHelper> pages = dh.getAllPages(); | |
| 48 | 50 | int n = 0; |
| 49 | - for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin(); | |
| 51 | + for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); | |
| 50 | 52 | iter != pages.end(); ++iter) |
| 51 | 53 | { |
| 52 | - QPDFObjectHandle& oh = *iter; | |
| 54 | + QPDFObjectHandle oh = (*iter).getObjectHandle(); | |
| 53 | 55 | page_map[oh.getObjGen()] = ++n; |
| 54 | 56 | } |
| 55 | 57 | } | ... | ... |
examples/pdf-count-strings.cc
| ... | ... | @@ -10,6 +10,8 @@ |
| 10 | 10 | #include <stdlib.h> |
| 11 | 11 | |
| 12 | 12 | #include <qpdf/QPDF.hh> |
| 13 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 14 | +#include <qpdf/QPDFPageObjectHelper.hh> | |
| 13 | 15 | #include <qpdf/QUtil.hh> |
| 14 | 16 | #include <qpdf/QPDFObjectHandle.hh> |
| 15 | 17 | #include <qpdf/Pl_StdioFile.hh> |
| ... | ... | @@ -90,12 +92,13 @@ int main(int argc, char* argv[]) |
| 90 | 92 | { |
| 91 | 93 | QPDF pdf; |
| 92 | 94 | pdf.processFile(infilename); |
| 93 | - std::vector<QPDFObjectHandle> pages = pdf.getAllPages(); | |
| 95 | + std::vector<QPDFPageObjectHelper> pages = | |
| 96 | + QPDFPageDocumentHelper(pdf).getAllPages(); | |
| 94 | 97 | int pageno = 0; |
| 95 | - for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin(); | |
| 98 | + for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); | |
| 96 | 99 | iter != pages.end(); ++iter) |
| 97 | 100 | { |
| 98 | - QPDFObjectHandle page = *iter; | |
| 101 | + QPDFPageObjectHelper& ph(*iter); | |
| 99 | 102 | ++pageno; |
| 100 | 103 | // Pass the contents of a page through our string counter. |
| 101 | 104 | // If it's an even page, capture the output. This |
| ... | ... | @@ -105,14 +108,14 @@ int main(int argc, char* argv[]) |
| 105 | 108 | if (pageno % 2) |
| 106 | 109 | { |
| 107 | 110 | // Ignore output for odd pages. |
| 108 | - page.filterPageContents(&counter); | |
| 111 | + ph.filterPageContents(&counter); | |
| 109 | 112 | } |
| 110 | 113 | else |
| 111 | 114 | { |
| 112 | 115 | // Write output to stdout for even pages. |
| 113 | 116 | Pl_StdioFile out("stdout", stdout); |
| 114 | 117 | std::cout << "% Contents of page " << pageno << std::endl; |
| 115 | - page.filterPageContents(&counter, &out); | |
| 118 | + ph.filterPageContents(&counter, &out); | |
| 116 | 119 | std::cout << "\n% end " << pageno << std::endl; |
| 117 | 120 | } |
| 118 | 121 | std::cout << "Page " << pageno | ... | ... |
examples/pdf-create.cc
| ... | ... | @@ -6,6 +6,8 @@ |
| 6 | 6 | // |
| 7 | 7 | |
| 8 | 8 | #include <qpdf/QPDF.hh> |
| 9 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 10 | +#include <qpdf/QPDFPageObjectHelper.hh> | |
| 9 | 11 | #include <qpdf/QPDFWriter.hh> |
| 10 | 12 | #include <qpdf/QPDFObjectHandle.hh> |
| 11 | 13 | #include <qpdf/QUtil.hh> |
| ... | ... | @@ -158,10 +160,12 @@ QPDFObjectHandle newInteger(int val) |
| 158 | 160 | return QPDFObjectHandle::newInteger(val); |
| 159 | 161 | } |
| 160 | 162 | |
| 161 | -void add_page(QPDF& pdf, QPDFObjectHandle font, | |
| 163 | +void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font, | |
| 162 | 164 | std::string const& color_space, |
| 163 | 165 | std::string const& filter) |
| 164 | 166 | { |
| 167 | + QPDF& pdf(dh.getQPDF()); | |
| 168 | + | |
| 165 | 169 | // Create a stream to encode our image. QPDFWriter will fill in |
| 166 | 170 | // the length and will respect our filters based on stream data |
| 167 | 171 | // mode. Since we are not specifying, QPDFWriter will compress |
| ... | ... | @@ -222,7 +226,7 @@ void add_page(QPDF& pdf, QPDFObjectHandle font, |
| 222 | 226 | page.replaceKey("/Resources", resources); |
| 223 | 227 | |
| 224 | 228 | // Add the page to the PDF file |
| 225 | - pdf.addPage(page, false); | |
| 229 | + dh.addPage(page, false); | |
| 226 | 230 | } |
| 227 | 231 | |
| 228 | 232 | static void check(char const* filename, |
| ... | ... | @@ -249,18 +253,19 @@ static void check(char const* filename, |
| 249 | 253 | |
| 250 | 254 | QPDF pdf; |
| 251 | 255 | pdf.processFile(filename); |
| 252 | - std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages(); | |
| 256 | + QPDFPageDocumentHelper dh(pdf); | |
| 257 | + std::vector<QPDFPageObjectHelper> pages = dh.getAllPages(); | |
| 253 | 258 | if (n_color_spaces * n_filters != pages.size()) |
| 254 | 259 | { |
| 255 | 260 | throw std::logic_error("incorrect number of pages"); |
| 256 | 261 | } |
| 257 | 262 | size_t pageno = 1; |
| 258 | 263 | bool errors = false; |
| 259 | - for (std::vector<QPDFObjectHandle>::const_iterator page_iter = | |
| 264 | + for (std::vector<QPDFPageObjectHelper>::iterator page_iter = | |
| 260 | 265 | pages.begin(); |
| 261 | 266 | page_iter != pages.end(); ++page_iter) |
| 262 | 267 | { |
| 263 | - QPDFObjectHandle page = *page_iter; | |
| 268 | + QPDFPageObjectHelper& page(*page_iter); | |
| 264 | 269 | std::map<std::string, QPDFObjectHandle> images = page.getPageImages(); |
| 265 | 270 | if (images.size() != 1) |
| 266 | 271 | { |
| ... | ... | @@ -391,13 +396,14 @@ static void create_pdf(char const* filename) |
| 391 | 396 | filters.push_back("null"); |
| 392 | 397 | filters.push_back("/DCTDecode"); |
| 393 | 398 | filters.push_back("/RunLengthDecode"); |
| 399 | + QPDFPageDocumentHelper dh(pdf); | |
| 394 | 400 | for (std::vector<std::string>::iterator c_iter = color_spaces.begin(); |
| 395 | 401 | c_iter != color_spaces.end(); ++c_iter) |
| 396 | 402 | { |
| 397 | 403 | for (std::vector<std::string>::iterator f_iter = filters.begin(); |
| 398 | 404 | f_iter != filters.end(); ++f_iter) |
| 399 | 405 | { |
| 400 | - add_page(pdf, font, *c_iter, *f_iter); | |
| 406 | + add_page(dh, font, *c_iter, *f_iter); | |
| 401 | 407 | } |
| 402 | 408 | } |
| 403 | 409 | ... | ... |
examples/pdf-double-page-size.cc
| ... | ... | @@ -2,6 +2,8 @@ |
| 2 | 2 | #include <string.h> |
| 3 | 3 | #include <stdlib.h> |
| 4 | 4 | #include <qpdf/QPDF.hh> |
| 5 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 6 | +#include <qpdf/QPDFPageObjectHelper.hh> | |
| 5 | 7 | #include <qpdf/QUtil.hh> |
| 6 | 8 | #include <qpdf/Buffer.hh> |
| 7 | 9 | #include <qpdf/QPDFWriter.hh> |
| ... | ... | @@ -77,14 +79,17 @@ int main(int argc, char* argv[]) |
| 77 | 79 | QPDF qpdf; |
| 78 | 80 | qpdf.processFile(infilename, password); |
| 79 | 81 | |
| 80 | - std::vector<QPDFObjectHandle> pages = qpdf.getAllPages(); | |
| 81 | - for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin(); | |
| 82 | + std::vector<QPDFPageObjectHelper> pages = | |
| 83 | + QPDFPageDocumentHelper(qpdf).getAllPages(); | |
| 84 | + for (std::vector<QPDFPageObjectHelper>::iterator iter = | |
| 85 | + pages.begin(); | |
| 82 | 86 | iter != pages.end(); ++iter) |
| 83 | 87 | { |
| 84 | - QPDFObjectHandle& page = *iter; | |
| 88 | + QPDFPageObjectHelper& ph(*iter); | |
| 89 | + QPDFObjectHandle page = ph.getObjectHandle(); | |
| 85 | 90 | |
| 86 | 91 | // Prepend the buffer to the page's contents |
| 87 | - page.addPageContents( | |
| 92 | + ph.addPageContents( | |
| 88 | 93 | QPDFObjectHandle::newStream(&qpdf, content), true); |
| 89 | 94 | |
| 90 | 95 | // Double the size of each of the content boxes | ... | ... |
examples/pdf-filter-tokens.cc
| ... | ... | @@ -12,6 +12,8 @@ |
| 12 | 12 | #include <deque> |
| 13 | 13 | |
| 14 | 14 | #include <qpdf/QPDF.hh> |
| 15 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 16 | +#include <qpdf/QPDFPageObjectHelper.hh> | |
| 15 | 17 | #include <qpdf/QUtil.hh> |
| 16 | 18 | #include <qpdf/QPDFWriter.hh> |
| 17 | 19 | #include <qpdf/QPDFObjectHandle.hh> |
| ... | ... | @@ -207,8 +209,9 @@ int main(int argc, char* argv[]) |
| 207 | 209 | { |
| 208 | 210 | QPDF pdf; |
| 209 | 211 | pdf.processFile(infilename); |
| 210 | - std::vector<QPDFObjectHandle> pages = pdf.getAllPages(); | |
| 211 | - for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin(); | |
| 212 | + std::vector<QPDFPageObjectHelper> pages = | |
| 213 | + QPDFPageDocumentHelper(pdf).getAllPages(); | |
| 214 | + for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); | |
| 212 | 215 | iter != pages.end(); ++iter) |
| 213 | 216 | { |
| 214 | 217 | // Attach two token filters to each page of this file. |
| ... | ... | @@ -216,7 +219,7 @@ int main(int argc, char* argv[]) |
| 216 | 219 | // are retrieved in any other way, the filters will be |
| 217 | 220 | // applied. See comments on the filters for additional |
| 218 | 221 | // details. |
| 219 | - QPDFObjectHandle page = *iter; | |
| 222 | + QPDFPageObjectHelper& page(*iter); | |
| 220 | 223 | page.addContentTokenFilter(new StringReverser); |
| 221 | 224 | page.addContentTokenFilter(new ColorToGray); |
| 222 | 225 | } | ... | ... |
examples/pdf-invert-images.cc
| ... | ... | @@ -2,6 +2,8 @@ |
| 2 | 2 | #include <string.h> |
| 3 | 3 | #include <stdlib.h> |
| 4 | 4 | #include <qpdf/QPDF.hh> |
| 5 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 6 | +#include <qpdf/QPDFPageObjectHelper.hh> | |
| 5 | 7 | #include <qpdf/QUtil.hh> |
| 6 | 8 | #include <qpdf/Buffer.hh> |
| 7 | 9 | #include <qpdf/QPDFWriter.hh> |
| ... | ... | @@ -97,11 +99,12 @@ int main(int argc, char* argv[]) |
| 97 | 99 | PointerHolder<QPDFObjectHandle::StreamDataProvider> p = inv; |
| 98 | 100 | |
| 99 | 101 | // For each page... |
| 100 | - std::vector<QPDFObjectHandle> pages = qpdf.getAllPages(); | |
| 101 | - for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin(); | |
| 102 | + std::vector<QPDFPageObjectHelper> pages = | |
| 103 | + QPDFPageDocumentHelper(qpdf).getAllPages(); | |
| 104 | + for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); | |
| 102 | 105 | iter != pages.end(); ++iter) |
| 103 | 106 | { |
| 104 | - QPDFObjectHandle& page = *iter; | |
| 107 | + QPDFPageObjectHelper& page(*iter); | |
| 105 | 108 | // Get all images on the page. |
| 106 | 109 | std::map<std::string, QPDFObjectHandle> images = |
| 107 | 110 | page.getPageImages(); | ... | ... |
examples/pdf-parse-content.cc
| ... | ... | @@ -3,6 +3,8 @@ |
| 3 | 3 | #include <stdlib.h> |
| 4 | 4 | |
| 5 | 5 | #include <qpdf/QPDF.hh> |
| 6 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 7 | +#include <qpdf/QPDFPageObjectHelper.hh> | |
| 6 | 8 | #include <qpdf/QUtil.hh> |
| 7 | 9 | |
| 8 | 10 | static char const* whoami = 0; |
| ... | ... | @@ -68,13 +70,14 @@ int main(int argc, char* argv[]) |
| 68 | 70 | { |
| 69 | 71 | QPDF pdf; |
| 70 | 72 | pdf.processFile(filename); |
| 71 | - std::vector<QPDFObjectHandle> pages = pdf.getAllPages(); | |
| 73 | + std::vector<QPDFPageObjectHelper> pages = | |
| 74 | + QPDFPageDocumentHelper(pdf).getAllPages(); | |
| 72 | 75 | if ((pageno < 1) || (static_cast<size_t>(pageno) > pages.size())) |
| 73 | 76 | { |
| 74 | 77 | usage(); |
| 75 | 78 | } |
| 76 | 79 | |
| 77 | - QPDFObjectHandle page = pages.at(pageno-1); | |
| 80 | + QPDFPageObjectHelper& page = pages.at(pageno-1); | |
| 78 | 81 | ParserCallbacks cb; |
| 79 | 82 | page.parsePageContents(&cb); |
| 80 | 83 | } | ... | ... |
examples/pdf-split-pages.cc
| ... | ... | @@ -5,6 +5,7 @@ |
| 5 | 5 | // |
| 6 | 6 | |
| 7 | 7 | #include <qpdf/QPDF.hh> |
| 8 | +#include <qpdf/QPDFPageDocumentHelper.hh> | |
| 8 | 9 | #include <qpdf/QPDFWriter.hh> |
| 9 | 10 | #include <qpdf/QUtil.hh> |
| 10 | 11 | #include <string> |
| ... | ... | @@ -20,18 +21,19 @@ static void process(char const* whoami, |
| 20 | 21 | { |
| 21 | 22 | QPDF inpdf; |
| 22 | 23 | inpdf.processFile(infile); |
| 23 | - std::vector<QPDFObjectHandle> const& pages = inpdf.getAllPages(); | |
| 24 | + std::vector<QPDFPageObjectHelper> pages = | |
| 25 | + QPDFPageDocumentHelper(inpdf).getAllPages(); | |
| 24 | 26 | int pageno_len = QUtil::int_to_string(pages.size()).length(); |
| 25 | 27 | int pageno = 0; |
| 26 | - for (std::vector<QPDFObjectHandle>::const_iterator iter = pages.begin(); | |
| 28 | + for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); | |
| 27 | 29 | iter != pages.end(); ++iter) |
| 28 | 30 | { |
| 29 | - QPDFObjectHandle page = *iter; | |
| 31 | + QPDFPageObjectHelper& page(*iter); | |
| 30 | 32 | std::string outfile = |
| 31 | 33 | outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf"; |
| 32 | 34 | QPDF outpdf; |
| 33 | 35 | outpdf.emptyPDF(); |
| 34 | - outpdf.addPage(page, false); | |
| 36 | + QPDFPageDocumentHelper(outpdf).addPage(page, false); | |
| 35 | 37 | QPDFWriter outpdfw(outpdf, outfile.c_str()); |
| 36 | 38 | if (static_id) |
| 37 | 39 | { | ... | ... |