diff --git a/ChangeLog b/ChangeLog index 76340fb1..baf568c4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2021-02-18 Jay Berkenbilt + * Add QPDFAcroFormDocumentHelper::addFormField, which adds a new + form field, initializing the AcroForm dictionary if needed. + * Add QPDFPageObjectHelper::getMatrixForFormXObjectPlacement, which returns the transformation matrix required to map from a form field's coordinate system into a specific rectangle within diff --git a/include/qpdf/QPDFAcroFormDocumentHelper.hh b/include/qpdf/QPDFAcroFormDocumentHelper.hh index 2c22a5da..d417fc08 100644 --- a/include/qpdf/QPDFAcroFormDocumentHelper.hh +++ b/include/qpdf/QPDFAcroFormDocumentHelper.hh @@ -106,6 +106,13 @@ class QPDFAcroFormDocumentHelper: public QPDFDocumentHelper bool hasAcroForm(); + // Add a form field, initializing the document's AcroForm + // dictionary if needed. Calling this method invalidates the + // cache, which makes it possible to add a field that is not yet + // associated with an annotation or page. + QPDF_DLL + void addFormField(QPDFFormFieldObjectHelper); + // Return a vector of all terminal fields in a document. Terminal // fields are fields that have no children that are also fields. // Terminal fields may still have children that are annotations. diff --git a/libqpdf/QPDFAcroFormDocumentHelper.cc b/libqpdf/QPDFAcroFormDocumentHelper.cc index 0fcc81f6..9f800a8e 100644 --- a/libqpdf/QPDFAcroFormDocumentHelper.cc +++ b/libqpdf/QPDFAcroFormDocumentHelper.cc @@ -32,6 +32,26 @@ QPDFAcroFormDocumentHelper::hasAcroForm() return this->qpdf.getRoot().hasKey("/AcroForm"); } +void +QPDFAcroFormDocumentHelper::addFormField(QPDFFormFieldObjectHelper ff) +{ + invalidateCache(); + auto acroform = this->qpdf.getRoot().getKey("/AcroForm"); + if (! acroform.isDictionary()) + { + acroform = this->qpdf.makeIndirectObject( + QPDFObjectHandle::newDictionary()); + this->qpdf.getRoot().replaceKey("/AcroForm", acroform); + } + auto fields = acroform.getKey("/Fields"); + if (! fields.isArray()) + { + fields = QPDFObjectHandle::newArray(); + acroform.replaceKey("/Fields", fields); + } + fields.appendItem(ff.getObjectHandle()); +} + std::vector QPDFAcroFormDocumentHelper::getFormFields() { diff --git a/manual/qpdf-manual.xml b/manual/qpdf-manual.xml index 83d178a7..dd03f8d8 100644 --- a/manual/qpdf-manual.xml +++ b/manual/qpdf-manual.xml @@ -5286,12 +5286,20 @@ print "\n"; Add - QPDFPageObjectHelper::getMatrixForFormXObjectPlacement + QPDFPageObjectHelper::getMatrixForFormXObjectPlacement, which returns the transformation matrix required to map from a form field's coordinate system into a specific rectangle within the page. + + + Add method + QPDFAcroFormDocumentHelper::addFormField, + which adds a new form field, initializing the AcroForm + dictionary if needed. + + Add QUtil::path_basename to return the