mirror of
https://github.com/qpdf/qpdf.git
synced 2024-06-03 19:00:51 +00:00
Convert examples to use new page helper classes
This commit is contained in:
parent
2e7ee23bf6
commit
2e6e1204a5
|
@ -7,7 +7,9 @@
|
||||||
of structural features of PDF while still staying true to qpdf's
|
of structural features of PDF while still staying true to qpdf's
|
||||||
philosophy of not isolating the user from the underlying
|
philosophy of not isolating the user from the underlying
|
||||||
structure. Please see the chapter in the documentation entitled
|
structure. Please see the chapter in the documentation entitled
|
||||||
"Design and Library Notes" for additional discussion.
|
"Design and Library Notes" for additional discussion. The examples
|
||||||
|
have also been updated to use QPDFPageDocumentHelper and
|
||||||
|
QPDFPageObjctHelper when performing page-level operations.
|
||||||
|
|
||||||
2018-06-19 Jay Berkenbilt <ejb@ql.org>
|
2018-06-19 Jay Berkenbilt <ejb@ql.org>
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
#include <qpdf/QTC.hh>
|
#include <qpdf/QTC.hh>
|
||||||
|
|
||||||
|
@ -44,12 +45,13 @@ void print_lines(std::vector<int>& numbers)
|
||||||
|
|
||||||
void generate_page_map(QPDF& qpdf)
|
void generate_page_map(QPDF& qpdf)
|
||||||
{
|
{
|
||||||
std::vector<QPDFObjectHandle> pages = qpdf.getAllPages();
|
QPDFPageDocumentHelper dh(qpdf);
|
||||||
|
std::vector<QPDFPageObjectHelper> pages = dh.getAllPages();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
||||||
iter != pages.end(); ++iter)
|
iter != pages.end(); ++iter)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle& oh = *iter;
|
QPDFObjectHandle oh = (*iter).getObjectHandle();
|
||||||
page_map[oh.getObjGen()] = ++n;
|
page_map[oh.getObjGen()] = ++n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
#include <qpdf/QPDFObjectHandle.hh>
|
#include <qpdf/QPDFObjectHandle.hh>
|
||||||
#include <qpdf/Pl_StdioFile.hh>
|
#include <qpdf/Pl_StdioFile.hh>
|
||||||
|
@ -90,12 +92,13 @@ int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
QPDF pdf;
|
QPDF pdf;
|
||||||
pdf.processFile(infilename);
|
pdf.processFile(infilename);
|
||||||
std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
|
std::vector<QPDFPageObjectHelper> pages =
|
||||||
|
QPDFPageDocumentHelper(pdf).getAllPages();
|
||||||
int pageno = 0;
|
int pageno = 0;
|
||||||
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
||||||
iter != pages.end(); ++iter)
|
iter != pages.end(); ++iter)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle page = *iter;
|
QPDFPageObjectHelper& ph(*iter);
|
||||||
++pageno;
|
++pageno;
|
||||||
// Pass the contents of a page through our string counter.
|
// Pass the contents of a page through our string counter.
|
||||||
// If it's an even page, capture the output. This
|
// If it's an even page, capture the output. This
|
||||||
|
@ -105,14 +108,14 @@ int main(int argc, char* argv[])
|
||||||
if (pageno % 2)
|
if (pageno % 2)
|
||||||
{
|
{
|
||||||
// Ignore output for odd pages.
|
// Ignore output for odd pages.
|
||||||
page.filterPageContents(&counter);
|
ph.filterPageContents(&counter);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Write output to stdout for even pages.
|
// Write output to stdout for even pages.
|
||||||
Pl_StdioFile out("stdout", stdout);
|
Pl_StdioFile out("stdout", stdout);
|
||||||
std::cout << "% Contents of page " << pageno << std::endl;
|
std::cout << "% Contents of page " << pageno << std::endl;
|
||||||
page.filterPageContents(&counter, &out);
|
ph.filterPageContents(&counter, &out);
|
||||||
std::cout << "\n% end " << pageno << std::endl;
|
std::cout << "\n% end " << pageno << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << "Page " << pageno
|
std::cout << "Page " << pageno
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
||||||
#include <qpdf/QPDFWriter.hh>
|
#include <qpdf/QPDFWriter.hh>
|
||||||
#include <qpdf/QPDFObjectHandle.hh>
|
#include <qpdf/QPDFObjectHandle.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
|
@ -158,10 +160,12 @@ QPDFObjectHandle newInteger(int val)
|
||||||
return QPDFObjectHandle::newInteger(val);
|
return QPDFObjectHandle::newInteger(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_page(QPDF& pdf, QPDFObjectHandle font,
|
void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font,
|
||||||
std::string const& color_space,
|
std::string const& color_space,
|
||||||
std::string const& filter)
|
std::string const& filter)
|
||||||
{
|
{
|
||||||
|
QPDF& pdf(dh.getQPDF());
|
||||||
|
|
||||||
// Create a stream to encode our image. QPDFWriter will fill in
|
// Create a stream to encode our image. QPDFWriter will fill in
|
||||||
// the length and will respect our filters based on stream data
|
// the length and will respect our filters based on stream data
|
||||||
// mode. Since we are not specifying, QPDFWriter will compress
|
// mode. Since we are not specifying, QPDFWriter will compress
|
||||||
|
@ -222,7 +226,7 @@ void add_page(QPDF& pdf, QPDFObjectHandle font,
|
||||||
page.replaceKey("/Resources", resources);
|
page.replaceKey("/Resources", resources);
|
||||||
|
|
||||||
// Add the page to the PDF file
|
// Add the page to the PDF file
|
||||||
pdf.addPage(page, false);
|
dh.addPage(page, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void check(char const* filename,
|
static void check(char const* filename,
|
||||||
|
@ -249,18 +253,19 @@ static void check(char const* filename,
|
||||||
|
|
||||||
QPDF pdf;
|
QPDF pdf;
|
||||||
pdf.processFile(filename);
|
pdf.processFile(filename);
|
||||||
std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages();
|
QPDFPageDocumentHelper dh(pdf);
|
||||||
|
std::vector<QPDFPageObjectHelper> pages = dh.getAllPages();
|
||||||
if (n_color_spaces * n_filters != pages.size())
|
if (n_color_spaces * n_filters != pages.size())
|
||||||
{
|
{
|
||||||
throw std::logic_error("incorrect number of pages");
|
throw std::logic_error("incorrect number of pages");
|
||||||
}
|
}
|
||||||
size_t pageno = 1;
|
size_t pageno = 1;
|
||||||
bool errors = false;
|
bool errors = false;
|
||||||
for (std::vector<QPDFObjectHandle>::const_iterator page_iter =
|
for (std::vector<QPDFPageObjectHelper>::iterator page_iter =
|
||||||
pages.begin();
|
pages.begin();
|
||||||
page_iter != pages.end(); ++page_iter)
|
page_iter != pages.end(); ++page_iter)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle page = *page_iter;
|
QPDFPageObjectHelper& page(*page_iter);
|
||||||
std::map<std::string, QPDFObjectHandle> images = page.getPageImages();
|
std::map<std::string, QPDFObjectHandle> images = page.getPageImages();
|
||||||
if (images.size() != 1)
|
if (images.size() != 1)
|
||||||
{
|
{
|
||||||
|
@ -391,13 +396,14 @@ static void create_pdf(char const* filename)
|
||||||
filters.push_back("null");
|
filters.push_back("null");
|
||||||
filters.push_back("/DCTDecode");
|
filters.push_back("/DCTDecode");
|
||||||
filters.push_back("/RunLengthDecode");
|
filters.push_back("/RunLengthDecode");
|
||||||
|
QPDFPageDocumentHelper dh(pdf);
|
||||||
for (std::vector<std::string>::iterator c_iter = color_spaces.begin();
|
for (std::vector<std::string>::iterator c_iter = color_spaces.begin();
|
||||||
c_iter != color_spaces.end(); ++c_iter)
|
c_iter != color_spaces.end(); ++c_iter)
|
||||||
{
|
{
|
||||||
for (std::vector<std::string>::iterator f_iter = filters.begin();
|
for (std::vector<std::string>::iterator f_iter = filters.begin();
|
||||||
f_iter != filters.end(); ++f_iter)
|
f_iter != filters.end(); ++f_iter)
|
||||||
{
|
{
|
||||||
add_page(pdf, font, *c_iter, *f_iter);
|
add_page(dh, font, *c_iter, *f_iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
#include <qpdf/Buffer.hh>
|
#include <qpdf/Buffer.hh>
|
||||||
#include <qpdf/QPDFWriter.hh>
|
#include <qpdf/QPDFWriter.hh>
|
||||||
|
@ -77,14 +79,17 @@ int main(int argc, char* argv[])
|
||||||
QPDF qpdf;
|
QPDF qpdf;
|
||||||
qpdf.processFile(infilename, password);
|
qpdf.processFile(infilename, password);
|
||||||
|
|
||||||
std::vector<QPDFObjectHandle> pages = qpdf.getAllPages();
|
std::vector<QPDFPageObjectHelper> pages =
|
||||||
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
|
QPDFPageDocumentHelper(qpdf).getAllPages();
|
||||||
|
for (std::vector<QPDFPageObjectHelper>::iterator iter =
|
||||||
|
pages.begin();
|
||||||
iter != pages.end(); ++iter)
|
iter != pages.end(); ++iter)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle& page = *iter;
|
QPDFPageObjectHelper& ph(*iter);
|
||||||
|
QPDFObjectHandle page = ph.getObjectHandle();
|
||||||
|
|
||||||
// Prepend the buffer to the page's contents
|
// Prepend the buffer to the page's contents
|
||||||
page.addPageContents(
|
ph.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
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
#include <qpdf/QPDFWriter.hh>
|
#include <qpdf/QPDFWriter.hh>
|
||||||
#include <qpdf/QPDFObjectHandle.hh>
|
#include <qpdf/QPDFObjectHandle.hh>
|
||||||
|
@ -207,8 +209,9 @@ int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
QPDF pdf;
|
QPDF pdf;
|
||||||
pdf.processFile(infilename);
|
pdf.processFile(infilename);
|
||||||
std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
|
std::vector<QPDFPageObjectHelper> pages =
|
||||||
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
|
QPDFPageDocumentHelper(pdf).getAllPages();
|
||||||
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
||||||
iter != pages.end(); ++iter)
|
iter != pages.end(); ++iter)
|
||||||
{
|
{
|
||||||
// Attach two token filters to each page of this file.
|
// Attach two token filters to each page of this file.
|
||||||
|
@ -216,7 +219,7 @@ int main(int argc, char* argv[])
|
||||||
// are retrieved in any other way, the filters will be
|
// are retrieved in any other way, the filters will be
|
||||||
// applied. See comments on the filters for additional
|
// applied. See comments on the filters for additional
|
||||||
// details.
|
// details.
|
||||||
QPDFObjectHandle page = *iter;
|
QPDFPageObjectHelper& page(*iter);
|
||||||
page.addContentTokenFilter(new StringReverser);
|
page.addContentTokenFilter(new StringReverser);
|
||||||
page.addContentTokenFilter(new ColorToGray);
|
page.addContentTokenFilter(new ColorToGray);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
#include <qpdf/Buffer.hh>
|
#include <qpdf/Buffer.hh>
|
||||||
#include <qpdf/QPDFWriter.hh>
|
#include <qpdf/QPDFWriter.hh>
|
||||||
|
@ -97,11 +99,12 @@ int main(int argc, char* argv[])
|
||||||
PointerHolder<QPDFObjectHandle::StreamDataProvider> p = inv;
|
PointerHolder<QPDFObjectHandle::StreamDataProvider> p = inv;
|
||||||
|
|
||||||
// For each page...
|
// For each page...
|
||||||
std::vector<QPDFObjectHandle> pages = qpdf.getAllPages();
|
std::vector<QPDFPageObjectHelper> pages =
|
||||||
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
|
QPDFPageDocumentHelper(qpdf).getAllPages();
|
||||||
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
||||||
iter != pages.end(); ++iter)
|
iter != pages.end(); ++iter)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle& page = *iter;
|
QPDFPageObjectHelper& page(*iter);
|
||||||
// Get all images on the page.
|
// Get all images on the page.
|
||||||
std::map<std::string, QPDFObjectHandle> images =
|
std::map<std::string, QPDFObjectHandle> images =
|
||||||
page.getPageImages();
|
page.getPageImages();
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
|
|
||||||
static char const* whoami = 0;
|
static char const* whoami = 0;
|
||||||
|
@ -68,13 +70,14 @@ int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
QPDF pdf;
|
QPDF pdf;
|
||||||
pdf.processFile(filename);
|
pdf.processFile(filename);
|
||||||
std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
|
std::vector<QPDFPageObjectHelper> pages =
|
||||||
|
QPDFPageDocumentHelper(pdf).getAllPages();
|
||||||
if ((pageno < 1) || (static_cast<size_t>(pageno) > pages.size()))
|
if ((pageno < 1) || (static_cast<size_t>(pageno) > pages.size()))
|
||||||
{
|
{
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
|
||||||
QPDFObjectHandle page = pages.at(pageno-1);
|
QPDFPageObjectHelper& page = pages.at(pageno-1);
|
||||||
ParserCallbacks cb;
|
ParserCallbacks cb;
|
||||||
page.parsePageContents(&cb);
|
page.parsePageContents(&cb);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <qpdf/QPDF.hh>
|
#include <qpdf/QPDF.hh>
|
||||||
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
||||||
#include <qpdf/QPDFWriter.hh>
|
#include <qpdf/QPDFWriter.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -20,18 +21,19 @@ static void process(char const* whoami,
|
||||||
{
|
{
|
||||||
QPDF inpdf;
|
QPDF inpdf;
|
||||||
inpdf.processFile(infile);
|
inpdf.processFile(infile);
|
||||||
std::vector<QPDFObjectHandle> const& pages = inpdf.getAllPages();
|
std::vector<QPDFPageObjectHelper> pages =
|
||||||
|
QPDFPageDocumentHelper(inpdf).getAllPages();
|
||||||
int pageno_len = QUtil::int_to_string(pages.size()).length();
|
int pageno_len = QUtil::int_to_string(pages.size()).length();
|
||||||
int pageno = 0;
|
int pageno = 0;
|
||||||
for (std::vector<QPDFObjectHandle>::const_iterator iter = pages.begin();
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
||||||
iter != pages.end(); ++iter)
|
iter != pages.end(); ++iter)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle page = *iter;
|
QPDFPageObjectHelper& page(*iter);
|
||||||
std::string outfile =
|
std::string outfile =
|
||||||
outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf";
|
outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf";
|
||||||
QPDF outpdf;
|
QPDF outpdf;
|
||||||
outpdf.emptyPDF();
|
outpdf.emptyPDF();
|
||||||
outpdf.addPage(page, false);
|
QPDFPageDocumentHelper(outpdf).addPage(page, false);
|
||||||
QPDFWriter outpdfw(outpdf, outfile.c_str());
|
QPDFWriter outpdfw(outpdf, outfile.c_str());
|
||||||
if (static_id)
|
if (static_id)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user