From 397b097c469db89a49e5a6c2035a0beee2e4d117 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 21 Jun 2018 15:00:00 -0400 Subject: Allow setting a form field's value --- libqpdf/QPDFAcroFormDocumentHelper.cc | 35 +++++++++++++++++++++++++++++ libqpdf/QPDFFormFieldObjectHelper.cc | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) (limited to 'libqpdf') diff --git a/libqpdf/QPDFAcroFormDocumentHelper.cc b/libqpdf/QPDFAcroFormDocumentHelper.cc index 7e70fd92..46648ed9 100644 --- a/libqpdf/QPDFAcroFormDocumentHelper.cc +++ b/libqpdf/QPDFAcroFormDocumentHelper.cc @@ -250,3 +250,38 @@ QPDFAcroFormDocumentHelper::traverseField( QPDFFormFieldObjectHelper(our_field); } } + +bool +QPDFAcroFormDocumentHelper::getNeedAppearances() +{ + bool result = false; + QPDFObjectHandle acroform = this->qpdf.getRoot().getKey("/AcroForm"); + if (acroform.isDictionary() && + acroform.getKey("/NeedAppearances").isBool()) + { + result = acroform.getKey("/NeedAppearances").getBoolValue(); + } + return result; +} + +void +QPDFAcroFormDocumentHelper::setNeedAppearances(bool val) +{ + QPDFObjectHandle acroform = this->qpdf.getRoot().getKey("/AcroForm"); + if (! acroform.isDictionary()) + { + this->qpdf.getRoot().warnIfPossible( + "ignoring call to QPDFAcroFormDocumentHelper::setNeedAppearances" + " on a file that lacks an /AcroForm dictionary"); + return; + } + if (val) + { + acroform.replaceKey("/NeedAppearances", + QPDFObjectHandle::newBool(true)); + } + else + { + acroform.removeKey("/NeedAppearances"); + } +} diff --git a/libqpdf/QPDFFormFieldObjectHelper.cc b/libqpdf/QPDFFormFieldObjectHelper.cc index e6445af3..283b632d 100644 --- a/libqpdf/QPDFFormFieldObjectHelper.cc +++ b/libqpdf/QPDFFormFieldObjectHelper.cc @@ -1,5 +1,6 @@ #include #include +#include QPDFFormFieldObjectHelper::Members::~Members() { @@ -188,3 +189,44 @@ QPDFFormFieldObjectHelper::getQuadding() } return result; } + +void +QPDFFormFieldObjectHelper::setFieldAttribute( + std::string const& key, QPDFObjectHandle value) +{ + this->oh.replaceKey(key, value); +} + +void +QPDFFormFieldObjectHelper::setFieldAttribute( + std::string const& key, std::string const& utf8_value) +{ + this->oh.replaceKey(key, QPDFObjectHandle::newUnicodeString(utf8_value)); +} + +void +QPDFFormFieldObjectHelper::setV( + QPDFObjectHandle value, bool need_appearances) +{ + setFieldAttribute("/V", value); + if (need_appearances) + { + QPDF* qpdf = this->oh.getOwningQPDF(); + if (! qpdf) + { + throw std::logic_error( + "QPDFFormFieldObjectHelper::setV called with" + " need_appearances = true on an object that is" + " not associated with an owning QPDF"); + } + QPDFAcroFormDocumentHelper(*qpdf).setNeedAppearances(true); + } +} + +void +QPDFFormFieldObjectHelper::setV( + std::string const& utf8_value, bool need_appearances) +{ + setV(QPDFObjectHandle::newUnicodeString(utf8_value), + need_appearances); +} -- cgit v1.2.3-54-g00ecf