aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pdf-attach-file.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-04-02 23:14:10 +0200
committerJay Berkenbilt <ejb@ql.org>2022-04-04 14:10:40 +0200
commit12f1eb15ca3fed6310402847559a7c99d3c77847 (patch)
tree8935675b623c6f3b4914b8b44f7fa5f2816a9241 /examples/pdf-attach-file.cc
parentf20fa61eb4c323eb1642c69c236b3d9a1f8b2cdb (diff)
downloadqpdf-12f1eb15ca3fed6310402847559a7c99d3c77847.tar.zst
Programmatically apply new formatting to code
Run this: for i in **/*.cc **/*.c **/*.h **/*.hh; do clang-format < $i >| $i.new && mv $i.new $i done
Diffstat (limited to 'examples/pdf-attach-file.cc')
-rw-r--r--examples/pdf-attach-file.cc134
1 files changed, 62 insertions, 72 deletions
diff --git a/examples/pdf-attach-file.cc b/examples/pdf-attach-file.cc
index 2cc30262..964d247d 100644
--- a/examples/pdf-attach-file.cc
+++ b/examples/pdf-attach-file.cc
@@ -1,11 +1,11 @@
#include <qpdf/QPDF.hh>
-#include <qpdf/QUtil.hh>
-#include <qpdf/QPDFWriter.hh>
#include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
#include <qpdf/QPDFFileSpecObjectHelper.hh>
+#include <qpdf/QPDFWriter.hh>
+#include <qpdf/QUtil.hh>
-#include <iostream>
#include <cstring>
+#include <iostream>
//
// This example attaches a file to an input file, adds a page to the
@@ -17,9 +17,11 @@
static char const* whoami = 0;
-static void usage(std::string const& msg)
+static void
+usage(std::string const& msg)
{
- std::cerr << msg << std::endl << std::endl
+ std::cerr << msg << std::endl
+ << std::endl
<< "Usage: " << whoami << " options" << std::endl
<< "Options:" << std::endl
<< " --infile infile.pdf" << std::endl
@@ -30,33 +32,36 @@ static void usage(std::string const& msg)
exit(2);
}
-static void process(char const* infilename, char const* password,
- char const* attachment, char const* mimetype,
- char const* outfilename)
+static void
+process(
+ char const* infilename,
+ char const* password,
+ char const* attachment,
+ char const* mimetype,
+ char const* outfilename)
{
QPDF q;
q.processFile(infilename, password);
// Create an indirect object for the built-in Helvetica font. This
// uses the qpdf literal syntax introduced in qpdf 10.6.
- auto f1 = q.makeIndirectObject(
- "<<"
- " /Type /Font"
- " /Subtype /Type1"
- " /Name /F1"
- " /BaseFont /Helvetica"
- " /Encoding /WinAnsiEncoding"
- ">>"_qpdf);
+ auto f1 = q.makeIndirectObject("<<"
+ " /Type /Font"
+ " /Subtype /Type1"
+ " /Name /F1"
+ " /BaseFont /Helvetica"
+ " /Encoding /WinAnsiEncoding"
+ ">>"_qpdf);
// Create a resources dictionary with fonts. This uses the new
// parse introduced in qpdf 10.2 that takes a QPDF* and allows
// indirect object references.
- auto resources = q.makeIndirectObject(
- QPDFObjectHandle::parse(
- &q,
- "<<"
- " /Font <<"
- " /F1 " + f1.unparse() +
+ auto resources = q.makeIndirectObject(QPDFObjectHandle::parse(
+ &q,
+ "<<"
+ " /Font <<"
+ " /F1 " +
+ f1.unparse() +
" >>"
">>"));
@@ -66,8 +71,7 @@ static void process(char const* infilename, char const* password,
<< std::endl;
auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment);
- if (mimetype)
- {
+ if (mimetype) {
// Get an embedded file stream and set mimetype
auto ef = QPDFEFStreamObjectHelper(fs.getEmbeddedFileStream());
ef.setSubtype(mimetype);
@@ -102,17 +106,16 @@ static void process(char const* infilename, char const* password,
apdict.replaceKey("/Type", "/XObject"_qpdf);
apdict.replaceKey("/Subtype", "/Form"_qpdf);
apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);
- auto annot = q.makeIndirectObject(
- QPDFObjectHandle::parse(
- &q,
- "<<"
- " /AP <<"
- " /N " + ap.unparse() +
+ auto annot = q.makeIndirectObject(QPDFObjectHandle::parse(
+ &q,
+ "<<"
+ " /AP <<"
+ " /N " +
+ ap.unparse() +
" >>"
- " /Contents "
- + QPDFObjectHandle::newUnicodeString(attachment).unparse() +
- " /FS " + fs.getObjectHandle().unparse() +
- " /NM " +
+ " /Contents " +
+ QPDFObjectHandle::newUnicodeString(attachment).unparse() +
+ " /FS " + fs.getObjectHandle().unparse() + " /NM " +
QPDFObjectHandle::newUnicodeString(attachment).unparse() +
" /Rect [ 72 700 92 720 ]"
" /Subtype /FileAttachment"
@@ -134,12 +137,16 @@ static void process(char const* infilename, char const* password,
auto page = QPDFObjectHandle::parse(
&q,
"<<"
- " /Annots [ " + annot.unparse() + " ]"
- " /Contents " + contents.unparse() +
- " /MediaBox [0 0 612 792]"
- " /Resources " + resources.unparse() +
- " /Type /Page"
- ">>");
+ " /Annots [ " +
+ annot.unparse() +
+ " ]"
+ " /Contents " +
+ contents.unparse() +
+ " /MediaBox [0 0 612 792]"
+ " /Resources " +
+ resources.unparse() +
+ " /Type /Page"
+ ">>");
// Add the page.
q.addPage(page, true);
@@ -151,7 +158,8 @@ static void process(char const* infilename, char const* password,
w.write();
}
-int main(int argc, char* argv[])
+int
+main(int argc, char* argv[])
{
whoami = QUtil::getWhoami(argv[0]);
@@ -162,64 +170,46 @@ int main(int argc, char* argv[])
char const* mimetype = 0;
auto check_arg = [](char const* arg, std::string const& msg) {
- if (arg == nullptr)
- {
+ if (arg == nullptr) {
usage(msg);
}
};
- for (int i = 1; i < argc; ++i)
- {
+ for (int i = 1; i < argc; ++i) {
char* arg = argv[i];
- char* next = argv[i+1];
- if (strcmp(arg, "--infile") == 0)
- {
+ char* next = argv[i + 1];
+ if (strcmp(arg, "--infile") == 0) {
check_arg(next, "--infile takes an argument");
infilename = next;
++i;
- }
- else if (strcmp(arg, "--password") == 0)
- {
+ } else if (strcmp(arg, "--password") == 0) {
check_arg(next, "--password takes an argument");
password = next;
++i;
- }
- else if (strcmp(arg, "--attachment") == 0)
- {
+ } else if (strcmp(arg, "--attachment") == 0) {
check_arg(next, "--attachment takes an argument");
attachment = next;
++i;
- }
- else if (strcmp(arg, "--outfile") == 0)
- {
+ } else if (strcmp(arg, "--outfile") == 0) {
check_arg(next, "--outfile takes an argument");
outfilename = next;
++i;
- }
- else if (strcmp(arg, "--mimetype") == 0)
- {
+ } else if (strcmp(arg, "--mimetype") == 0) {
check_arg(next, "--mimetype takes an argument");
mimetype = next;
++i;
- }
- else
- {
+ } else {
usage("unknown argument " + std::string(arg));
}
}
- if (! (infilename && attachment && outfilename))
- {
+ if (!(infilename && attachment && outfilename)) {
usage("required arguments were not provided");
}
- try
- {
+ try {
process(infilename, password, attachment, mimetype, outfilename);
- }
- catch (std::exception &e)
- {
- std::cerr << whoami << " exception: "
- << e.what() << std::endl;
+ } catch (std::exception& e) {
+ std::cerr << whoami << " exception: " << e.what() << std::endl;
exit(2);
}