2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 02:49:00 +00:00

Convert examples to use new page helper classes

This commit is contained in:
Jay Berkenbilt 2018-06-18 15:06:51 -04:00
parent 2e7ee23bf6
commit 2e6e1204a5
9 changed files with 60 additions and 31 deletions

View File

@ -7,7 +7,9 @@
of structural features of PDF while still staying true to qpdf's
philosophy of not isolating the user from the underlying
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>

View File

@ -2,6 +2,7 @@
#include <string.h>
#include <stdlib.h>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QTC.hh>
@ -44,12 +45,13 @@ void print_lines(std::vector<int>& numbers)
void generate_page_map(QPDF& qpdf)
{
std::vector<QPDFObjectHandle> pages = qpdf.getAllPages();
QPDFPageDocumentHelper dh(qpdf);
std::vector<QPDFPageObjectHelper> pages = dh.getAllPages();
int n = 0;
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
iter != pages.end(); ++iter)
{
QPDFObjectHandle& oh = *iter;
QPDFObjectHandle oh = (*iter).getObjectHandle();
page_map[oh.getObjGen()] = ++n;
}
}

View File

@ -10,6 +10,8 @@
#include <stdlib.h>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/Pl_StdioFile.hh>
@ -90,12 +92,13 @@ int main(int argc, char* argv[])
{
QPDF pdf;
pdf.processFile(infilename);
std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
std::vector<QPDFPageObjectHelper> pages =
QPDFPageDocumentHelper(pdf).getAllPages();
int pageno = 0;
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
iter != pages.end(); ++iter)
{
QPDFObjectHandle page = *iter;
QPDFPageObjectHelper& ph(*iter);
++pageno;
// Pass the contents of a page through our string counter.
// If it's an even page, capture the output. This
@ -105,14 +108,14 @@ int main(int argc, char* argv[])
if (pageno % 2)
{
// Ignore output for odd pages.
page.filterPageContents(&counter);
ph.filterPageContents(&counter);
}
else
{
// Write output to stdout for even pages.
Pl_StdioFile out("stdout", stdout);
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 << "Page " << pageno

View File

@ -6,6 +6,8 @@
//
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QUtil.hh>
@ -158,10 +160,12 @@ QPDFObjectHandle newInteger(int 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& filter)
{
QPDF& pdf(dh.getQPDF());
// Create a stream to encode our image. QPDFWriter will fill in
// the length and will respect our filters based on stream data
// mode. Since we are not specifying, QPDFWriter will compress
@ -222,7 +226,7 @@ void add_page(QPDF& pdf, QPDFObjectHandle font,
page.replaceKey("/Resources", resources);
// Add the page to the PDF file
pdf.addPage(page, false);
dh.addPage(page, false);
}
static void check(char const* filename,
@ -249,18 +253,19 @@ static void check(char const* filename,
QPDF pdf;
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())
{
throw std::logic_error("incorrect number of pages");
}
size_t pageno = 1;
bool errors = false;
for (std::vector<QPDFObjectHandle>::const_iterator page_iter =
for (std::vector<QPDFPageObjectHelper>::iterator page_iter =
pages.begin();
page_iter != pages.end(); ++page_iter)
{
QPDFObjectHandle page = *page_iter;
QPDFPageObjectHelper& page(*page_iter);
std::map<std::string, QPDFObjectHandle> images = page.getPageImages();
if (images.size() != 1)
{
@ -391,13 +396,14 @@ static void create_pdf(char const* filename)
filters.push_back("null");
filters.push_back("/DCTDecode");
filters.push_back("/RunLengthDecode");
QPDFPageDocumentHelper dh(pdf);
for (std::vector<std::string>::iterator c_iter = color_spaces.begin();
c_iter != color_spaces.end(); ++c_iter)
{
for (std::vector<std::string>::iterator f_iter = filters.begin();
f_iter != filters.end(); ++f_iter)
{
add_page(pdf, font, *c_iter, *f_iter);
add_page(dh, font, *c_iter, *f_iter);
}
}

View File

@ -2,6 +2,8 @@
#include <string.h>
#include <stdlib.h>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/Buffer.hh>
#include <qpdf/QPDFWriter.hh>
@ -77,14 +79,17 @@ int main(int argc, char* argv[])
QPDF qpdf;
qpdf.processFile(infilename, password);
std::vector<QPDFObjectHandle> pages = qpdf.getAllPages();
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
std::vector<QPDFPageObjectHelper> pages =
QPDFPageDocumentHelper(qpdf).getAllPages();
for (std::vector<QPDFPageObjectHelper>::iterator iter =
pages.begin();
iter != pages.end(); ++iter)
{
QPDFObjectHandle& page = *iter;
QPDFPageObjectHelper& ph(*iter);
QPDFObjectHandle page = ph.getObjectHandle();
// Prepend the buffer to the page's contents
page.addPageContents(
ph.addPageContents(
QPDFObjectHandle::newStream(&qpdf, content), true);
// Double the size of each of the content boxes

View File

@ -12,6 +12,8 @@
#include <deque>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QPDFObjectHandle.hh>
@ -207,8 +209,9 @@ int main(int argc, char* argv[])
{
QPDF pdf;
pdf.processFile(infilename);
std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
std::vector<QPDFPageObjectHelper> pages =
QPDFPageDocumentHelper(pdf).getAllPages();
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
iter != pages.end(); ++iter)
{
// 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
// applied. See comments on the filters for additional
// details.
QPDFObjectHandle page = *iter;
QPDFPageObjectHelper& page(*iter);
page.addContentTokenFilter(new StringReverser);
page.addContentTokenFilter(new ColorToGray);
}

View File

@ -2,6 +2,8 @@
#include <string.h>
#include <stdlib.h>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/Buffer.hh>
#include <qpdf/QPDFWriter.hh>
@ -97,11 +99,12 @@ int main(int argc, char* argv[])
PointerHolder<QPDFObjectHandle::StreamDataProvider> p = inv;
// For each page...
std::vector<QPDFObjectHandle> pages = qpdf.getAllPages();
for (std::vector<QPDFObjectHandle>::iterator iter = pages.begin();
std::vector<QPDFPageObjectHelper> pages =
QPDFPageDocumentHelper(qpdf).getAllPages();
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
iter != pages.end(); ++iter)
{
QPDFObjectHandle& page = *iter;
QPDFPageObjectHelper& page(*iter);
// Get all images on the page.
std::map<std::string, QPDFObjectHandle> images =
page.getPageImages();

View File

@ -3,6 +3,8 @@
#include <stdlib.h>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QUtil.hh>
static char const* whoami = 0;
@ -68,13 +70,14 @@ int main(int argc, char* argv[])
{
QPDF pdf;
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()))
{
usage();
}
QPDFObjectHandle page = pages.at(pageno-1);
QPDFPageObjectHelper& page = pages.at(pageno-1);
ParserCallbacks cb;
page.parsePageContents(&cb);
}

View File

@ -5,6 +5,7 @@
//
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QUtil.hh>
#include <string>
@ -20,18 +21,19 @@ static void process(char const* whoami,
{
QPDF inpdf;
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 = 0;
for (std::vector<QPDFObjectHandle>::const_iterator iter = pages.begin();
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
iter != pages.end(); ++iter)
{
QPDFObjectHandle page = *iter;
QPDFPageObjectHelper& page(*iter);
std::string outfile =
outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf";
QPDF outpdf;
outpdf.emptyPDF();
outpdf.addPage(page, false);
QPDFPageDocumentHelper(outpdf).addPage(page, false);
QPDFWriter outpdfw(outpdf, outfile.c_str());
if (static_id)
{