mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
AcroForms implementation (data structures start)
This commit is contained in:
213
PdfForQtLib/sources/pdfform.cpp
Normal file
213
PdfForQtLib/sources/pdfform.cpp
Normal file
@ -0,0 +1,213 @@
|
||||
// Copyright (C) 2020 Jakub Melka
|
||||
//
|
||||
// This file is part of PdfForQt.
|
||||
//
|
||||
// PdfForQt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// PdfForQt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include "pdfform.h"
|
||||
#include "pdfdocument.h"
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
|
||||
PDFForm PDFForm::parse(const PDFObjectStorage* storage, PDFObject object)
|
||||
{
|
||||
PDFForm form;
|
||||
|
||||
if (const PDFDictionary* formDictionary = storage->getDictionaryFromObject(object))
|
||||
{
|
||||
PDFDocumentDataLoaderDecorator loader(storage);
|
||||
|
||||
std::vector<PDFObjectReference> fieldRoots = loader.readReferenceArrayFromDictionary(formDictionary, "Fields");
|
||||
form.m_formFields.reserve(fieldRoots.size());
|
||||
for (const PDFObjectReference& fieldRootReference : fieldRoots)
|
||||
{
|
||||
form.m_formFields.emplace_back(PDFFormField::parse(storage, fieldRootReference, nullptr));
|
||||
}
|
||||
|
||||
form.m_needAppearances = loader.readBooleanFromDictionary(formDictionary, "NeedAppearances", false);
|
||||
form.m_signatureFlags = static_cast<SignatureFlags>(loader.readIntegerFromDictionary(formDictionary, "SigFlags", 0));
|
||||
form.m_calculationOrder = loader.readReferenceArrayFromDictionary(formDictionary, "CO");
|
||||
form.m_resources = formDictionary->get("DR");
|
||||
form.m_defaultAppearance = loader.readOptionalStringFromDictionary(formDictionary, "DA");
|
||||
form.m_quadding = loader.readOptionalIntegerFromDictionary(formDictionary, "Q");
|
||||
form.m_xfa = formDictionary->get("XFA");
|
||||
}
|
||||
|
||||
return form;
|
||||
}
|
||||
|
||||
PDFFormFieldPointer PDFFormField::parse(const PDFObjectStorage* storage, PDFObjectReference reference, PDFFormField* parentField)
|
||||
{
|
||||
PDFFormFieldPointer result;
|
||||
|
||||
if (const PDFDictionary* fieldDictionary = storage->getDictionaryFromObject(storage->getObjectByReference(reference)))
|
||||
{
|
||||
PDFFormField* formField = nullptr;
|
||||
PDFFormFieldButton* formFieldButton = nullptr;
|
||||
PDFFormFieldText* formFieldText = nullptr;
|
||||
PDFFormFieldChoice* formFieldChoice = nullptr;
|
||||
PDFFormFieldSignature* formFieldSignature = nullptr;
|
||||
|
||||
constexpr const std::array<std::pair<const char*, FieldType>, 4> fieldTypes = {
|
||||
std::pair<const char*, FieldType>{ "Btn", FieldType::Button },
|
||||
std::pair<const char*, FieldType>{ "Tx", FieldType::Text },
|
||||
std::pair<const char*, FieldType>{ "Ch", FieldType::Choice },
|
||||
std::pair<const char*, FieldType>{ "Sig", FieldType::Signature }
|
||||
};
|
||||
|
||||
PDFDocumentDataLoaderDecorator loader(storage);
|
||||
FieldType formFieldType = parentField ? parentField->getFieldType() : FieldType::Invalid;
|
||||
if (fieldDictionary->hasKey(FT))
|
||||
{
|
||||
formFieldType = loader.readEnumByName(fieldDictionary->get("FT"), fieldTypes.begin(), fieldTypes.end(), FieldType::Invalid);
|
||||
}
|
||||
|
||||
switch (formFieldType)
|
||||
{
|
||||
case FieldType::Invalid:
|
||||
formField = new PDFFormField();
|
||||
break;
|
||||
|
||||
case FieldType::Button:
|
||||
formFieldButton = new PDFFormFieldButton();
|
||||
formField = formFieldButton;
|
||||
break;
|
||||
|
||||
case FieldType::Text:
|
||||
formFieldText = new PDFFormFieldText();
|
||||
formField = formFieldText;
|
||||
break;
|
||||
|
||||
case FieldType::Choice:
|
||||
formFieldChoice = new PDFFormFieldChoice();
|
||||
formField = formFieldChoice;
|
||||
break;
|
||||
|
||||
case FieldType::Signature:
|
||||
formFieldSignature = new PDFFormFieldSignature();
|
||||
formField = formFieldSignature;
|
||||
break;
|
||||
|
||||
default:
|
||||
Q_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
result.reset(formField);
|
||||
|
||||
std::vector<PDFObjectReference> kids = loader.readReferenceArrayFromDictionary(fieldDictionary, "Kids");
|
||||
if (kids.empty())
|
||||
{
|
||||
// This means, that field's dictionary is merged with annotation's dictionary,
|
||||
// so, we will add pointer to self to the form widgets. But we must test, if we
|
||||
// really have merged annotation's dictionary - we test it by checking for 'Subtype'
|
||||
// presence.
|
||||
if (loader.readNameFromDictionary(fieldDictionary, "Subtype") == "Widget")
|
||||
{
|
||||
formField->m_widgets.emplace_back(PDFFormWidget::parse(storage, reference, formField));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise we must scan all references, and determine, if kid is another form field,
|
||||
// or it is a widget annotation. Widget annotations has required field 'Subtype', which
|
||||
// has value 'Widget', form field has required field 'Parent' (for non-root fields).
|
||||
|
||||
for (const PDFObjectReference& kid : kids)
|
||||
{
|
||||
if (const PDFDictionary* childDictionary = storage->getDictionaryFromObject(storage->getObjectByReference(kid)))
|
||||
{
|
||||
const bool isWidget = loader.readNameFromDictionary(childDictionary, "Subtype") == "Widget";
|
||||
const bool isField = loader.readReferenceFromDictionary(childDictionary, "Parent").isValid();
|
||||
|
||||
if (isField)
|
||||
{
|
||||
// This is form field (potentially merged with widget)
|
||||
formField->m_childFields.emplace_back(PDFFormField::parse(storage, kid, formField));
|
||||
}
|
||||
else if (isWidget)
|
||||
{
|
||||
// This is pure widget (with no form field)
|
||||
formField->m_widgets.emplace_back(PDFFormWidget::parse(storage, kid, formField));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PDFObject parentV = parentField ? parentField->getValue() : PDFObject();
|
||||
PDFObject parentDV = parentField ? parentField->getDefaultValue() : PDFObject();
|
||||
FieldFlags parentFlags = parentField ? parentField->getFlags() : None;
|
||||
|
||||
formField->m_selfReference = reference;
|
||||
formField->m_fieldType = formFieldType;
|
||||
formField->m_parentField = parentField;
|
||||
formField->m_fieldNames[Partial] = loader.readTextStringFromDictionary(fieldDictionary, "T", QString());
|
||||
formField->m_fieldNames[UserCaption] = loader.readTextStringFromDictionary(fieldDictionary, "TU", QString());
|
||||
formField->m_fieldNames[Export] = loader.readTextStringFromDictionary(fieldDictionary, "TM", QString());
|
||||
formField->m_fieldNames[FullyQualified] = parentField ? QString("%1.%2").arg(parentField->getName(FullyQualified), formField->m_fieldNames[Partial]) : formField->m_fieldNames[Partial];
|
||||
formField->m_fieldFlags = fieldDictionary->hasKey("Ff") ? static_cast<FieldFlags>(loader.readIntegerFromDictionary(fieldDictionary, "Ff", 0)) : parentFlags;
|
||||
formField->m_value = fieldDictionary->hasKey("V") ? fieldDictionary->get("V") : parentV;
|
||||
formField->m_defaultValue = fieldDictionary->hasKey("DV") ? fieldDictionary->get("DV") : parentDV;
|
||||
formField->m_additionalActions = PDFAnnotationAdditionalActions::parse(storage, fieldDictionary->get("AA"));
|
||||
|
||||
if (formFieldButton)
|
||||
{
|
||||
formFieldButton->m_options = loader.readTextStringList(fieldDictionary->get("Opt"));
|
||||
}
|
||||
|
||||
if (formFieldText)
|
||||
{
|
||||
PDFInteger maxLengthDefault = 0;
|
||||
|
||||
if (PDFFormFieldText* parentTextField = dynamic_cast<PDFFormFieldText*>(parentField))
|
||||
{
|
||||
maxLengthDefault = parentTextField->getTextMaximalLength();
|
||||
}
|
||||
|
||||
formFieldText->m_maxLength = loader.readIntegerFromDictionary(fieldDictionary, "MaxLen", maxLengthDefault);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
PDFFormWidget::PDFFormWidget(PDFObjectReference widget, PDFFormField* parentField) :
|
||||
m_widget(widget),
|
||||
m_parentField(parentField)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PDFFormWidget PDFFormWidget::parse(const PDFObjectStorage* storage, PDFObjectReference reference, PDFFormField* parentField)
|
||||
{
|
||||
Q_UNUSED(storage);
|
||||
return PDFFormWidget(reference, parentField);
|
||||
}
|
||||
|
||||
PDFFormFieldButton::ButtonType PDFFormFieldButton::getButtonType() const
|
||||
{
|
||||
if (m_fieldFlags.testFlag(PushButton))
|
||||
{
|
||||
return ButtonType::PushButton;
|
||||
}
|
||||
else if (m_fieldFlags.testFlag(Radio))
|
||||
{
|
||||
return ButtonType::RadioButton;
|
||||
}
|
||||
|
||||
return ButtonType::CheckBox;
|
||||
}
|
||||
|
||||
} // namespace pdf
|
Reference in New Issue
Block a user