2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-05 08:02:11 +00:00

Tidy pdf-create example

Convert loop to use range-based for statement.
Remove unnecessary variables.
This commit is contained in:
m-holger 2022-02-26 15:42:47 +00:00 committed by Jay Berkenbilt
parent 33e8195c3a
commit 1253d1be60

View File

@ -182,15 +182,15 @@ void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font,
size_t width = p->getWidth(); size_t width = p->getWidth();
size_t height = p->getHeight(); size_t height = p->getHeight();
QPDFObjectHandle image = QPDFObjectHandle::newStream(&pdf); QPDFObjectHandle image = QPDFObjectHandle::newStream(&pdf);
image.replaceDict("<<" auto image_dict = "<<"
" /Type /XObject" " /Type /XObject"
" /Subtype /Image" " /Subtype /Image"
" /BitsPerComponent 8" " /BitsPerComponent 8"
">>"_qpdf); ">>"_qpdf;
QPDFObjectHandle image_dict = image.getDict();
image_dict.replaceKey("/ColorSpace", newName(color_space)); image_dict.replaceKey("/ColorSpace", newName(color_space));
image_dict.replaceKey("/Width", newInteger(width)); image_dict.replaceKey("/Width", newInteger(width));
image_dict.replaceKey("/Height", newInteger(height)); image_dict.replaceKey("/Height", newInteger(height));
image.replaceDict(image_dict);
// Provide the stream data. // Provide the stream data.
image.replaceStreamData(provider, image.replaceStreamData(provider,
@ -211,21 +211,16 @@ void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font,
resources.replaceKey("/Font", rfont); resources.replaceKey("/Font", rfont);
resources.replaceKey("/XObject", xobject); resources.replaceKey("/XObject", xobject);
QPDFObjectHandle mediabox = QPDFObjectHandle::newArray();
mediabox.appendItem(newInteger(0));
mediabox.appendItem(newInteger(0));
mediabox.appendItem(newInteger(612));
mediabox.appendItem(newInteger(392));
// Create the page content stream // Create the page content stream
QPDFObjectHandle contents = createPageContents( QPDFObjectHandle contents = createPageContents(
pdf, color_space + " with filter " + filter); pdf, color_space + " with filter " + filter);
// Create the page dictionary // Create the page dictionary
QPDFObjectHandle page = pdf.makeIndirectObject( QPDFObjectHandle page = pdf.makeIndirectObject(
QPDFObjectHandle::newDictionary()); "<<"
page.replaceKey("/Type", newName("/Page")); " /Type /Page"
page.replaceKey("/MediaBox", mediabox); " /MediaBox [0 0 612 392]"
">>"_qpdf);
page.replaceKey("/Contents", contents); page.replaceKey("/Contents", contents);
page.replaceKey("/Resources", resources); page.replaceKey("/Resources", resources);
@ -257,20 +252,16 @@ static void check(char const* filename,
QPDF pdf; QPDF pdf;
pdf.processFile(filename); pdf.processFile(filename);
QPDFPageDocumentHelper dh(pdf); auto pages = QPDFPageDocumentHelper(pdf).getAllPages();
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<QPDFPageObjectHelper>::iterator page_iter = for (auto& page : pages)
pages.begin();
page_iter != pages.end(); ++page_iter)
{ {
QPDFPageObjectHelper& page(*page_iter); auto images = page.getImages();
std::map<std::string, QPDFObjectHandle> images = page.getImages();
if (images.size() != 1) if (images.size() != 1)
{ {
throw std::logic_error("incorrect number of images on page"); throw std::logic_error("incorrect number of images on page");
@ -279,8 +270,7 @@ static void check(char const* filename,
// Check filter and color space. // Check filter and color space.
std::string desired_color_space = std::string desired_color_space =
color_spaces[(pageno - 1) / n_color_spaces]; color_spaces[(pageno - 1) / n_color_spaces];
std::string desired_filter = std::string desired_filter = filters[(pageno - 1) % n_filters];
filters[(pageno - 1) % n_filters];
// In the default mode, QPDFWriter will compress with // In the default mode, QPDFWriter will compress with
// /FlateDecode if no filters are provided. // /FlateDecode if no filters are provided.
if (desired_filter == "null") if (desired_filter == "null")
@ -310,7 +300,7 @@ static void check(char const* filename,
if (! this_errors) if (! this_errors)
{ {
// Check image data // Check image data
PointerHolder<Buffer> actual_data = auto actual_data =
image.getStreamData(qpdf_dl_all); image.getStreamData(qpdf_dl_all);
ImageProvider* p = new ImageProvider(desired_color_space, "null"); ImageProvider* p = new ImageProvider(desired_color_space, "null");
PointerHolder<QPDFObjectHandle::StreamDataProvider> provider(p); PointerHolder<QPDFObjectHandle::StreamDataProvider> provider(p);
@ -399,13 +389,11 @@ static void create_pdf(char const* filename)
filters.push_back("/DCTDecode"); filters.push_back("/DCTDecode");
filters.push_back("/RunLengthDecode"); filters.push_back("/RunLengthDecode");
QPDFPageDocumentHelper dh(pdf); QPDFPageDocumentHelper dh(pdf);
for (std::vector<std::string>::iterator c_iter = color_spaces.begin(); for (auto const& color_space : color_spaces)
c_iter != color_spaces.end(); ++c_iter)
{ {
for (std::vector<std::string>::iterator f_iter = filters.begin(); for (auto const& filter : filters)
f_iter != filters.end(); ++f_iter)
{ {
add_page(dh, font, *c_iter, *f_iter); add_page(dh, font, color_space, filter);
} }
} }