2018-06-21 19:54:02 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
2018-06-21 19:54:02 +00:00
|
|
|
#include <qpdf/QPDFWriter.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-06-21 19:54:02 +00:00
|
|
|
|
|
|
|
static char const* whoami = 0;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
usage()
|
2018-06-21 19:54:02 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf value"
|
2022-02-08 14:18:08 +00:00
|
|
|
<< std::endl
|
|
|
|
<< "Set the value of all text fields to a specified value"
|
2018-06-21 19:54:02 +00:00
|
|
|
<< std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2018-06-21 19:54:02 +00:00
|
|
|
{
|
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (argc != 4) {
|
2018-06-21 19:54:02 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* infilename = argv[1];
|
|
|
|
char const* outfilename = argv[2];
|
|
|
|
char const* value = argv[3];
|
|
|
|
|
|
|
|
// This is a contrived example that just goes through a file page
|
|
|
|
// by page and sets the value of any text fields it finds to a
|
|
|
|
// fixed value as given on the command line. The purpose here is
|
|
|
|
// to illustrate use of the helper classes around interactive
|
|
|
|
// forms.
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDF qpdf;
|
|
|
|
qpdf.processFile(infilename);
|
2018-06-21 19:54:02 +00:00
|
|
|
|
|
|
|
// We will iterate through form fields by starting at the page
|
|
|
|
// level and looking at each field for each page. We could
|
|
|
|
// also called QPDFAcroFormDocumentHelper::getFormFields to
|
|
|
|
// iterate at the field level, but doing it as below
|
|
|
|
// illustrates how we can map from annotations to fields.
|
|
|
|
|
|
|
|
QPDFAcroFormDocumentHelper afdh(qpdf);
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& page: QPDFPageDocumentHelper(qpdf).getAllPages()) {
|
2018-06-21 19:54:02 +00:00
|
|
|
// Get all widget annotations for each page. Widget
|
|
|
|
// annotations are the ones that contain the details of
|
|
|
|
// what's in a form field.
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& annot: afdh.getWidgetAnnotationsForPage(page)) {
|
2018-06-21 19:54:02 +00:00
|
|
|
// For each annotation, find its associated field. If
|
2020-02-22 17:10:13 +00:00
|
|
|
// it's a text field, set its value.
|
2018-06-21 19:54:02 +00:00
|
|
|
QPDFFormFieldObjectHelper ffh =
|
2022-04-30 17:23:18 +00:00
|
|
|
afdh.getFieldForAnnotation(annot);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (ffh.getFieldType() == "/Tx") {
|
2020-02-22 17:10:13 +00:00
|
|
|
// Set the value. Passing false as the second
|
|
|
|
// value prevents qpdf from setting
|
|
|
|
// /NeedAppearances to true (but will not turn it
|
|
|
|
// off if it's already on), so we call
|
|
|
|
// generateAppearance after setting the value. You
|
|
|
|
// may or may not want to do this depending on
|
|
|
|
// whether the appearance streams generated by
|
|
|
|
// qpdf are good enough for your purposes. For
|
|
|
|
// additional details, please see comments in
|
|
|
|
// QPDFFormFieldObjectHelper.hh for this method.
|
|
|
|
ffh.setV(value, false);
|
2022-04-30 17:23:18 +00:00
|
|
|
ffh.generateAppearance(annot);
|
2018-06-21 19:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Write out a new file
|
|
|
|
QPDFWriter w(qpdf, outfilename);
|
2018-06-21 19:54:02 +00:00
|
|
|
w.setStaticID(true); // for testing only
|
2022-02-08 14:18:08 +00:00
|
|
|
w.write();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << whoami << " processing file " << infilename << ": "
|
|
|
|
<< e.what() << std::endl;
|
|
|
|
exit(2);
|
2018-06-21 19:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|