2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-16 01:27:07 +00:00
qpdf/examples/pdf-attach-file.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

215 lines
6.8 KiB
C++
Raw Normal View History

2021-02-18 01:14:04 +00:00
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
#include <qpdf/QPDFFileSpecObjectHelper.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QUtil.hh>
#include <cstring>
#include <iostream>
//
// This example attaches a file to an input file, adds a page to the beginning of the file that
// includes a file attachment annotation, and writes the result to an output file. It also
2022-02-20 16:49:31 +00:00
// illustrates a number of new API calls that were added in qpdf 10.2 as well as the use of the qpdf
// literal syntax introduced in qpdf 10.6.
2021-02-18 01:14:04 +00:00
//
static char const* whoami = nullptr;
2021-02-18 01:14:04 +00:00
static void
usage(std::string const& msg)
{
std::cerr << msg << std::endl
<< std::endl
<< "Usage: " << whoami << " options" << std::endl
<< "Options:" << std::endl
<< " --infile infile.pdf" << std::endl
<< " --outfile outfile.pdf" << std::endl
<< " --attachment attachment" << std::endl
2022-02-20 16:49:31 +00:00
<< " [--password infile-password]" << std::endl
<< " [--mimetype attachment mime type]" << std::endl;
2021-02-18 01:14:04 +00:00
exit(2);
}
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(
// force line-break
"<<"
" /Type /Font"
" /Subtype /Type1"
" /Name /F1"
" /BaseFont /Helvetica"
" /Encoding /WinAnsiEncoding"
">>"_qpdf);
2021-02-18 01:14:04 +00:00
// 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(
// line-break
QPDFObjectHandle::parse(
&q,
("<<"
" /Font <<"
" /F1 " +
f1.unparse() +
" >>"
">>")));
2021-02-18 01:14:04 +00:00
// Create a file spec.
2021-02-18 13:03:32 +00:00
std::string key = QUtil::path_basename(attachment);
2021-02-18 01:14:04 +00:00
std::cout << whoami << ": attaching " << attachment << " as " << key << std::endl;
auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment);
if (mimetype) {
// Get an embedded file stream and set mimetype
auto ef = QPDFEFStreamObjectHelper(fs.getEmbeddedFileStream());
ef.setSubtype(mimetype);
}
// Add the embedded file at the document level as an attachment.
auto efdh = QPDFEmbeddedFileDocumentHelper(q);
efdh.replaceEmbeddedFile(key, fs);
// Create a file attachment annotation.
// Create appearance stream for the attachment.
2022-09-26 18:29:26 +00:00
auto ap = q.newStream("0 10 m\n"
"10 0 l\n"
"20 10 l\n"
"10 0 m\n"
"10 20 l\n"
"0 0 20 20 re\n"
"S\n");
2021-02-18 01:14:04 +00:00
auto apdict = ap.getDict();
2022-02-20 16:49:31 +00:00
// The following four lines demonstrate the use of the qpdf literal syntax introduced in
// qpdf 10.6. They could have been written as:
// apdict.replaceKey("/Resources", QPDFObjectHandle::newDictionary());
// apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
// apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
// apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]"));
apdict.replaceKey("/Resources", "<< >>"_qpdf);
apdict.replaceKey("/Type", "/XObject"_qpdf);
apdict.replaceKey("/Subtype", "/Form"_qpdf);
apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);
2021-02-18 01:14:04 +00:00
auto annot = q.makeIndirectObject(QPDFObjectHandle::parse(
&q,
("<<"
" /AP <<"
" /N " +
ap.unparse() +
" >>"
" /Contents " +
QPDFObjectHandle::newUnicodeString(attachment).unparse() + " /FS " +
fs.getObjectHandle().unparse() + " /NM " +
QPDFObjectHandle::newUnicodeString(attachment).unparse() +
" /Rect [ 72 700 92 720 ]"
" /Subtype /FileAttachment"
" /Type /Annot"
">>")));
2021-02-18 01:14:04 +00:00
// Generate contents for the page.
2022-09-26 18:29:26 +00:00
auto contents = q.newStream(("q\n"
"BT\n"
" 102 700 Td\n"
" /F1 16 Tf\n"
" (Here is an attachment.) Tj\n"
"ET\n"
"Q\n"));
2021-02-18 01:14:04 +00:00
// Create the page object.
auto page = QPDFObjectHandle::parse(
&q,
("<<"
" /Annots [ " +
annot.unparse() +
" ]"
" /Contents " +
contents.unparse() +
" /MediaBox [0 0 612 792]"
" /Resources " +
resources.unparse() +
" /Type /Page"
">>"));
2021-02-18 01:14:04 +00:00
// Add the page.
q.addPage(page, true);
QPDFWriter w(q, outfilename);
w.setQDFMode(true);
w.setSuppressOriginalObjectIDs(true);
w.setDeterministicID(true);
w.write();
}
int
main(int argc, char* argv[])
{
whoami = QUtil::getWhoami(argv[0]);
char const* infilename = nullptr;
char const* password = nullptr;
char const* attachment = nullptr;
char const* outfilename = nullptr;
char const* mimetype = nullptr;
2021-02-18 01:14:04 +00:00
auto check_arg = [](char const* arg, std::string const& msg) {
if (arg == nullptr) {
usage(msg);
}
};
for (int i = 1; i < argc; ++i) {
char* arg = argv[i];
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) {
check_arg(next, "--password takes an argument");
password = next;
++i;
} else if (strcmp(arg, "--attachment") == 0) {
check_arg(next, "--attachment takes an argument");
attachment = next;
++i;
} else if (strcmp(arg, "--outfile") == 0) {
check_arg(next, "--outfile takes an argument");
outfilename = next;
++i;
} else if (strcmp(arg, "--mimetype") == 0) {
check_arg(next, "--mimetype takes an argument");
mimetype = next;
++i;
} else {
usage("unknown argument " + std::string(arg));
}
}
if (!(infilename && attachment && outfilename)) {
usage("required arguments were not provided");
}
try {
process(infilename, password, attachment, mimetype, outfilename);
} catch (std::exception& e) {
std::cerr << whoami << " exception: " << e.what() << std::endl;
exit(2);
2021-02-18 01:14:04 +00:00
}
return 0;
}