aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2013-01-20 21:26:45 +0100
committerJay Berkenbilt <ejb@ql.org>2013-01-20 21:35:39 +0100
commitf81152311e5737e5e0de9dd9462311f306c6921b (patch)
treee015c0efd7022cc259e898255db872fa52b73ca1 /examples
parent1d88955fa68fb7fb0fd2d705bc80655edb7a5972 (diff)
downloadqpdf-f81152311e5737e5e0de9dd9462311f306c6921b.tar.zst
Add QPDFObjectHandle::parseContentStream method
This method allows parsing of the PDF objects in a content stream or array of content streams.
Diffstat (limited to 'examples')
-rw-r--r--examples/build.mk3
-rw-r--r--examples/pdf-parse-content.cc97
-rw-r--r--examples/qtest/parse-content.test17
-rw-r--r--examples/qtest/parse-content/content.out11
-rw-r--r--examples/qtest/parse-content/input.pdfbin0 -> 799 bytes
5 files changed, 127 insertions, 1 deletions
diff --git a/examples/build.mk b/examples/build.mk
index 12734b1b..bcb4440e 100644
--- a/examples/build.mk
+++ b/examples/build.mk
@@ -4,7 +4,8 @@ BINS_examples = \
pdf-npages \
pdf-double-page-size \
pdf-invert-images \
- pdf-create
+ pdf-create \
+ pdf-parse-content
CBINS_examples = pdf-linearize
TARGETS_examples = $(foreach B,$(BINS_examples) $(CBINS_examples),examples/$(OUTPUT_DIR)/$(call binname,$(B)))
diff --git a/examples/pdf-parse-content.cc b/examples/pdf-parse-content.cc
new file mode 100644
index 00000000..1c3cae16
--- /dev/null
+++ b/examples/pdf-parse-content.cc
@@ -0,0 +1,97 @@
+#include <iostream>
+#include <string.h>
+#include <stdlib.h>
+
+#include <qpdf/QPDF.hh>
+#include <qpdf/QUtil.hh>
+
+static char const* whoami = 0;
+
+void usage()
+{
+ std::cerr << "Usage: " << whoami << " filename page-number" << std::endl
+ << "Prints a dump of the objects in the content streams"
+ << " of the given page." << std::endl
+ << "Pages are numbered from 1." << std::endl;
+ exit(2);
+}
+
+class ParserCallbacks: public QPDFObjectHandle::ParserCallbacks
+{
+ public:
+ virtual ~ParserCallbacks()
+ {
+ }
+
+ virtual void handleObject(QPDFObjectHandle);
+ virtual void handleEOF();
+};
+
+void
+ParserCallbacks::handleObject(QPDFObjectHandle obj)
+{
+ if (obj.isInlineImage())
+ {
+ std::string val = obj.getInlineImageValue();
+ std::cout << "inline image: ";
+ char buf[3];
+ buf[2] = '\0';
+ for (size_t i = 0; i < val.length(); ++i)
+ {
+ sprintf(buf, "%02x", (unsigned char)(val[i]));
+ std::cout << buf;
+ }
+ std::cout << std::endl;
+ }
+ 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]);
+
+ // For libtool's sake....
+ if (strncmp(whoami, "lt-", 3) == 0)
+ {
+ whoami += 3;
+ }
+
+ if (argc != 3)
+ {
+ usage();
+ }
+ char const* filename = argv[1];
+ int pageno = atoi(argv[2]);
+
+ try
+ {
+ QPDF pdf;
+ pdf.processFile(filename);
+ std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
+ if ((pageno < 1) || (pageno > (int)pages.size()))
+ {
+ usage();
+ }
+
+ QPDFObjectHandle page = pages[pageno-1];
+ QPDFObjectHandle contents = page.getKey("/Contents");
+ ParserCallbacks cb;
+ QPDFObjectHandle::parseContentStream(contents, &cb);
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << whoami << ": " << e.what() << std::endl;
+ exit(2);
+ }
+
+ return 0;
+}
diff --git a/examples/qtest/parse-content.test b/examples/qtest/parse-content.test
new file mode 100644
index 00000000..a73566f8
--- /dev/null
+++ b/examples/qtest/parse-content.test
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+require 5.008;
+BEGIN { $^W = 1; }
+use strict;
+
+chdir("parse-content");
+
+require TestDriver;
+
+my $td = new TestDriver('pdf-parse-content');
+
+$td->runtest("parse content",
+ {$td->COMMAND => "pdf-parse-content input.pdf 1"},
+ {$td->FILE => "content.out", $td->EXIT_STATUS => 0},
+ $td->NORMALIZE_NEWLINES);
+
+$td->report(1);
diff --git a/examples/qtest/parse-content/content.out b/examples/qtest/parse-content/content.out
new file mode 100644
index 00000000..9c07edc2
--- /dev/null
+++ b/examples/qtest/parse-content/content.out
@@ -0,0 +1,11 @@
+BT
+/F1
+24
+Tf
+72
+720
+Td
+(Potato)
+Tj
+ET
+-EOF-
diff --git a/examples/qtest/parse-content/input.pdf b/examples/qtest/parse-content/input.pdf
new file mode 100644
index 00000000..cd319591
--- /dev/null
+++ b/examples/qtest/parse-content/input.pdf
Binary files differ