2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdlib>
|
2013-01-20 20:26:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2013-01-20 20:26:45 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
2018-06-18 19:06:51 +00:00
|
|
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
2013-01-20 20:26:45 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2022-07-26 11:37:50 +00:00
|
|
|
static char const* whoami = nullptr;
|
2013-01-20 20:26:45 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << whoami << " filename page-number" << std::endl
|
2022-02-08 14:18:08 +00:00
|
|
|
<< "Prints a dump of the objects in the content streams of the given page."
|
2013-01-20 20:26:45 +00:00
|
|
|
<< std::endl
|
|
|
|
<< "Pages are numbered from 1." << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParserCallbacks: public QPDFObjectHandle::ParserCallbacks
|
|
|
|
{
|
|
|
|
public:
|
2023-05-20 13:25:46 +00:00
|
|
|
~ParserCallbacks() override = default;
|
|
|
|
void contentSize(size_t) override;
|
|
|
|
void handleObject(QPDFObjectHandle, size_t offset, size_t length) override;
|
|
|
|
void handleEOF() override;
|
2013-01-20 20:26:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2019-08-22 23:16:25 +00:00
|
|
|
ParserCallbacks::contentSize(size_t size)
|
2013-01-20 20:26:45 +00:00
|
|
|
{
|
2019-08-22 23:16:25 +00:00
|
|
|
std::cout << "content size: " << size << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParserCallbacks::handleObject(QPDFObjectHandle obj, size_t offset, size_t length)
|
|
|
|
{
|
|
|
|
std::cout << obj.getTypeName() << ", offset=" << offset << ", length=" << length << ": ";
|
2013-01-20 20:26:45 +00:00
|
|
|
if (obj.isInlineImage()) {
|
2013-01-25 13:59:55 +00:00
|
|
|
std::cout << QUtil::hex_encode(obj.getInlineImageValue()) << std::endl;
|
2013-01-20 20:26:45 +00:00
|
|
|
} else {
|
|
|
|
std::cout << obj.unparse() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParserCallbacks::handleEOF()
|
|
|
|
{
|
|
|
|
std::cout << "-EOF-" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
|
|
|
|
if (argc != 3) {
|
2022-02-08 14:18:08 +00:00
|
|
|
usage();
|
2013-01-20 20:26:45 +00:00
|
|
|
}
|
|
|
|
char const* filename = argv[1];
|
2017-08-29 16:27:59 +00:00
|
|
|
int pageno = QUtil::string_to_int(argv[2]);
|
2013-01-20 20:26:45 +00:00
|
|
|
|
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDF pdf;
|
|
|
|
pdf.processFile(filename);
|
2018-06-18 19:06:51 +00:00
|
|
|
std::vector<QPDFPageObjectHelper> pages = QPDFPageDocumentHelper(pdf).getAllPages();
|
2019-06-21 03:35:23 +00:00
|
|
|
if ((pageno < 1) || (QIntC::to_size(pageno) > pages.size())) {
|
2013-01-20 20:26:45 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
QPDFPageObjectHelper& page = pages.at(QIntC::to_size(pageno - 1));
|
2013-01-20 20:26:45 +00:00
|
|
|
ParserCallbacks cb;
|
2021-01-02 19:05:17 +00:00
|
|
|
page.parseContents(&cb);
|
2013-01-20 20:26:45 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << whoami << ": " << e.what() << std::endl;
|
|
|
|
exit(2);
|
2013-01-20 20:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|