Commit 2e6e1204a51b4aba4657712a34e9447d928b5fc6

Authored by Jay Berkenbilt
1 parent 2e7ee23b

Convert examples to use new page helper classes

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