PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfField.h
1// SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
2// SPDX-FileCopyrightText: 2020 Francesco Pretto <ceztko@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
4
5#ifndef PDF_FIELD_H
6#define PDF_FIELD_H
7
8#include "PdfDeclarations.h"
9
10#include "PdfName.h"
11#include "PdfString.h"
12#include "PdfAnnotationWidget.h"
13#include "PdfAction.h"
14#include "PdfFieldChildrenCollection.h"
15
16namespace PoDoFo {
17
18class PdfPage;
19class PdfAcroForm;
20class PdfAnnotationWidget;
21class PdfPushButton;
22class PdfCheckBox;
23class PdfRadioButton;
24class PdfTextBox;
25class PdfComboBox;
26class PdfListBox;
27class PdfSignature;
28
29class PODOFO_API PdfField : public PdfDictionaryElement
30{
31 friend class PdfSignature;
32 friend class PdfButton;
33 friend class PdChoiceField;
34 friend class PdfTextBox;
35 friend class PdfPage;
36 friend class PdfAcroForm;
37 friend class PdfAnnotationWidget;
38 friend class PdfFieldChildrenCollectionBase;
39
40protected:
41 enum
42 {
43 PdfButton_NoToggleOff = 0x0004000,
44 PdfButton_Radio = 0x0008000,
45 PdfButton_PushButton = 0x0010000,
46 PdfButton_RadioInUnison = 0x2000000,
47 PdfListField_Combo = 0x0020000,
48 PdfListField_Edit = 0x0040000,
49 PdfListField_Sort = 0x0080000,
50 PdfListField_MultiSelect = 0x0200000,
51 PdfListField_NoSpellcheck = 0x0400000,
52 PdfListField_CommitOnSelChange = 0x4000000
53 };
54
55private:
56 PdfField(PdfAcroForm& acroform, PdfFieldType fieldType,
57 std::shared_ptr<PdfField>&& parent);
58
59 PdfField(PdfAnnotationWidget& widget, PdfFieldType fieldType,
60 std::shared_ptr<PdfField>&& parent);
61
62 PdfField(PdfObject& obj, PdfAcroForm* acroform, PdfFieldType fieldType);
63
64public:
65
67 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfField>& field);
68
69 PdfField* GetParentSafe();
70 const PdfField* GetParentSafe() const;
71
78 void SetHighlightingMode(PdfHighlightingMode mode);
79
82 PdfHighlightingMode GetHighlightingMode() const;
83
90 void SetName(nullable<const PdfString&> name);
91
97 PdfObject* GetValueObject();
98 const PdfObject* GetValueObject() const;
99
101 nullable<const PdfString&> GetName() const;
102
104 nullable<const PdfString&> GetNameRaw() const;
105
109 std::string GetFullName(bool skipEscapePartialName = false) const;
110
116 void SetAlternateName(nullable<const PdfString&> name);
117
119 nullable<const PdfString&> GetAlternateName() const;
120
125 void SetMappingName(nullable<const PdfString&> name);
126
128 nullable<const PdfString&> GetMappingName() const;
129
137 void SetReadOnly(bool readOnly);
138
142 bool IsReadOnly() const;
143
148 void SetRequired(bool required);
149
153 bool IsRequired() const;
154
160 void SetNoExport(bool exprt);
161
165 bool IsNoExport() const;
166
167 void SetMouseEnterAction(const PdfAction& action);
168 void SetMouseLeaveAction(const PdfAction& action);
169 void SetMouseDownAction(const PdfAction& action);
170 void SetMouseUpAction(const PdfAction& action);
171
172 void SetFocusEnterAction(const PdfAction& action);
173 void SetFocusLeaveAction(const PdfAction& action);
174
175 void SetPageOpenAction(const PdfAction& action);
176 void SetPageCloseAction(const PdfAction& action);
177
178 void SetPageVisibleAction(const PdfAction& action);
179 void SetPageInvisibleAction(const PdfAction& action);
180
181 void SetKeystrokeAction(const PdfAction& action);
182 void SetValidateAction(const PdfAction& action);
183
184public:
185 PdfFieldType GetType() const { return m_FieldType; }
186
187 PdfAnnotationWidget* GetWidget() { return m_Widget; }
188
189 const PdfAnnotationWidget* GetWidget() const { return m_Widget; }
190
191 PdfAnnotationWidget& MustGetWidget();
192
193 const PdfAnnotationWidget& MustGetWidget() const;
194
195 PdfFieldChildrenCollectionBase& GetChildren();
196
197 const PdfFieldChildrenCollectionBase& GetChildren() const;
198
199protected:
207 void SetFieldFlag(int64_t value, bool set);
208
215 bool GetFieldFlag(int64_t value, bool defvalue) const;
216
220 static bool GetFieldFlags(const PdfObject& obj, int64_t& value);
221
222 virtual PdfObject* getValueObject() const;
223
224 void AssertTerminalField() const;
225
226 template <typename TField>
227 TField* GetParentTyped(PdfFieldType type) const;
228
229private:
230 template <typename TField>
231 static constexpr PdfFieldType GetFieldType();
232
233 void SetWidget(PdfAnnotationWidget& widget) { m_Widget = &widget; }
234 void SetAcroForm(PdfAcroForm& acroform) { m_AcroForm = &acroform; }
235
236private:
237 // To be called by PdfPage
238 static PdfField& Create(const std::string_view& name,
239 PdfAnnotationWidget& widget, PdfFieldType fieldType);
240
241 // To be called by PdfAcroForm
242 static std::unique_ptr<PdfField> Create(const std::string_view& name,
243 PdfAcroForm& acroform, PdfFieldType fieldType);
244 static std::unique_ptr<PdfField> Create(PdfObject& obj, PdfAcroForm& acroform,
245 PdfFieldType fieldType);
246
247 // To be called by PdfFieldChildrenCollectionBase
248 std::unique_ptr<PdfField> CreateChild();
249 std::unique_ptr<PdfField> CreateChild(PdfPage& page, const Rect& rect);
250 void SetParent(std::shared_ptr<PdfField>&& parent);
251
252private:
253 PdfField(const PdfField& rhs) = delete;
254 PdfField& operator=(const PdfField& rhs) = delete;
255
256private:
257 static std::unique_ptr<PdfField> createField(PdfAnnotationWidget& widget,
258 PdfFieldType type, std::shared_ptr<PdfField>&& parent, bool insertInAcroform);
259
260 static std::unique_ptr<PdfField> createField(PdfAcroForm& acroform, PdfFieldType type,
261 std::shared_ptr<PdfField>&& parent);
262
263 static void linkFieldObjectToParent(const std::shared_ptr<PdfField>& field, PdfField& parentField,
264 cspan<std::string_view> parentKeys, bool setParent, bool moveKeysToParent);
265
266 void init();
267 void initParent();
268 void initChildren();
269 void ensureAccessibilityIfNeeded(const std::string_view& fieldName);
270 void setName(const PdfString& name);
271 void addAlternativeAction(const PdfName& name, const PdfAction& action);
272 static bool tryCreateField(PdfObject& obj, PdfFieldType type,
273 std::unique_ptr<PdfField>& field);
274 std::unique_ptr<PdfField> createChildField(PdfPage* page, const Rect& rect);
275 static PdfFieldType getFieldType(const PdfObject& obj);
276 std::shared_ptr<PdfField> GetPtr();
277 PdfField* getParentTyped(PdfFieldType type) const;
278 std::string_view getFieldTypeDisplayName() const;
279
280private:
281 PdfAnnotationWidget* m_Widget;
282 PdfAcroForm* m_AcroForm;
283 PdfFieldType m_FieldType;
284 nullable<std::shared_ptr<PdfField>> m_Parent;
285 std::unique_ptr<PdfFieldChildrenCollectionBase> m_Children;
286};
287
288template<typename TField>
289TField* PdfField::GetParentTyped(PdfFieldType type) const
290{
291 return static_cast<TField*>(getParentTyped(type));
292}
293
294template<typename TField>
295constexpr PdfFieldType PdfField::GetFieldType()
296{
297 if (std::is_same_v<TField, PdfPushButton>)
298 return PdfFieldType::PushButton;
299 else if (std::is_same_v<TField, PdfCheckBox>)
300 return PdfFieldType::CheckBox;
301 else if (std::is_same_v<TField, PdfRadioButton>)
302 return PdfFieldType::RadioButton;
303 else if (std::is_same_v<TField, PdfTextBox>)
304 return PdfFieldType::TextBox;
305 else if (std::is_same_v<TField, PdfComboBox>)
306 return PdfFieldType::ComboBox;
307 else if (std::is_same_v<TField, PdfListBox>)
308 return PdfFieldType::ListBox;
309 else if (std::is_same_v<TField, PdfSignature>)
310 return PdfFieldType::Signature;
311 else
312 return PdfFieldType::Unknown;
313}
314
315};
316
317#endif // PDF_FIELD_H
This file should be included as the FIRST file in every header of PoDoFo lib.
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfFieldType
The type of PDF field.
Definition PdfDeclarations.h:610
@ Create
Create a new file or truncate existing one for writing/reading.
PdfHighlightingMode
The possible highlighting modes for a PdfField.
Definition PdfDeclarations.h:629