PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfField.h
1
7#ifndef PDF_FIELD_H
8#define PDF_FIELD_H
9
10#include "PdfDeclarations.h"
11
12#include "PdfName.h"
13#include "PdfString.h"
14#include "PdfAnnotationWidget.h"
15#include "PdfAction.h"
16#include "PdfFieldChildrenCollection.h"
17
18namespace PoDoFo {
19
20class PdfPage;
21class PdfAcroForm;
22class PdfAnnotationWidget;
23class PdfPushButton;
24class PdfCheckBox;
25class PdfRadioButton;
26class PdfTextBox;
27class PdfComboBox;
28class PdfListBox;
29class PdfSignature;
30
31class PODOFO_API PdfField : public PdfDictionaryElement
32{
33 friend class PdfSignature;
34 friend class PdfButton;
35 friend class PdChoiceField;
36 friend class PdfTextBox;
37 friend class PdfPage;
38 friend class PdfAcroForm;
39 friend class PdfAnnotationWidget;
40 friend class PdfFieldChildrenCollectionBase;
41
42protected:
43 enum
44 {
45 PdfButton_NoToggleOff = 0x0004000,
46 PdfButton_Radio = 0x0008000,
47 PdfButton_PushButton = 0x0010000,
48 PdfButton_RadioInUnison = 0x2000000,
49 PdfListField_Combo = 0x0020000,
50 PdfListField_Edit = 0x0040000,
51 PdfListField_Sort = 0x0080000,
52 PdfListField_MultiSelect = 0x0200000,
53 PdfListField_NoSpellcheck = 0x0400000,
54 PdfListField_CommitOnSelChange = 0x4000000
55 };
56
57private:
58 PdfField(PdfAcroForm& acroform, PdfFieldType fieldType,
59 const std::shared_ptr<PdfField>& parent);
60
61 PdfField(PdfAnnotationWidget& widget, PdfFieldType fieldType,
62 const std::shared_ptr<PdfField>& parent);
63
64 PdfField(PdfObject& obj, PdfAcroForm* acroform, PdfFieldType fieldType);
65
66public:
67
70 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfField>& field);
71
72 PdfField* GetParentSafe();
73 const PdfField* GetParentSafe() const;
74
82 void SetHighlightingMode(PdfHighlightingMode mode);
83
88 PdfHighlightingMode GetHighlightingMode() const;
89
97 void SetName(nullable<const PdfString&> name);
98
105 PdfObject* GetValueObject();
106 const PdfObject* GetValueObject() const;
107
110 nullable<const PdfString&> GetName() const;
111
114 nullable<const PdfString&> GetNameRaw() const;
115
120 std::string GetFullName(bool skipEscapePartialName = false) const;
121
129 void SetAlternateName(nullable<const PdfString&> name);
130
133 nullable<const PdfString&> GetAlternateName() const;
134
141 void SetMappingName(nullable<const PdfString&> name);
142
145 nullable<const PdfString&> GetMappingName() const;
146
155 void SetReadOnly(bool readOnly);
156
162 bool IsReadOnly() const;
163
169 void SetRequired(bool required);
170
176 bool IsRequired() const;
177
184 void SetNoExport(bool exprt);
185
191 bool IsNoExport() const;
192
193 void SetMouseEnterAction(const PdfAction& action);
194 void SetMouseLeaveAction(const PdfAction& action);
195 void SetMouseDownAction(const PdfAction& action);
196 void SetMouseUpAction(const PdfAction& action);
197
198 void SetFocusEnterAction(const PdfAction& action);
199 void SetFocusLeaveAction(const PdfAction& action);
200
201 void SetPageOpenAction(const PdfAction& action);
202 void SetPageCloseAction(const PdfAction& action);
203
204 void SetPageVisibleAction(const PdfAction& action);
205 void SetPageInvisibleAction(const PdfAction& action);
206
207 void SetKeystrokeAction(const PdfAction& action);
208 void SetValidateAction(const PdfAction& action);
209
210public:
211 PdfFieldType GetType() const { return m_FieldType; }
212
213 PdfAnnotationWidget* GetWidget() { return m_Widget; }
214
215 const PdfAnnotationWidget* GetWidget() const { return m_Widget; }
216
217 PdfAnnotationWidget& MustGetWidget();
218
219 const PdfAnnotationWidget& MustGetWidget() const;
220
221 PdfFieldChildrenCollectionBase& GetChildren();
222
223 const PdfFieldChildrenCollectionBase& GetChildren() const;
224
225protected:
235 void SetFieldFlag(int64_t value, bool set);
236
245 bool GetFieldFlag(int64_t value, bool defvalue) const;
246
252 static bool GetFieldFlags(const PdfObject& obj, int64_t& value);
253
254 virtual PdfObject* getValueObject() const;
255
256 void AssertTerminalField() const;
257
258 template <typename TField>
259 TField* GetParentTyped(PdfFieldType type) const;
260
261private:
262 template <typename TField>
263 static constexpr PdfFieldType GetFieldType();
264
265 void SetWidget(PdfAnnotationWidget& widget) { m_Widget = &widget; }
266 void SetAcroForm(PdfAcroForm& acroform) { m_AcroForm = &acroform; }
267
268private:
269 // To be called by PdfPage
270 static PdfField& Create(const std::string_view& name,
271 PdfAnnotationWidget& widget, PdfFieldType fieldType);
272
273 // To be called by PdfAcroForm
274 static std::unique_ptr<PdfField> Create(const std::string_view& name,
275 PdfAcroForm& acroform, PdfFieldType fieldType);
276 static std::unique_ptr<PdfField> Create(PdfObject& obj, PdfAcroForm& acroform,
277 PdfFieldType fieldType);
278
279 // To be called by PdfFieldChildrenCollectionBase
280 std::unique_ptr<PdfField> CreateChild();
281 std::unique_ptr<PdfField> CreateChild(PdfPage& page, const Rect& rect);
282 void SetParent(const std::shared_ptr<PdfField>& parent) { m_Parent = parent; }
283
284private:
285 PdfField(const PdfField& rhs) = delete;
286 PdfField& operator=(const PdfField& rhs) = delete;
287
288private:
289 static std::unique_ptr<PdfField> createField(PdfAnnotationWidget& widget,
290 PdfFieldType type, const std::shared_ptr<PdfField>& parent, bool insertInAcroform);
291
292 static std::unique_ptr<PdfField> createField(PdfAcroForm& acroform, PdfFieldType type,
293 const std::shared_ptr<PdfField>& parent);
294
295 static void linkFieldObjectToParent(const std::shared_ptr<PdfField>& field, PdfField& parentField,
296 const std::vector<std::string>& parentKeys, bool setParent, bool moveKeysToParent);
297
298 void init();
299 void initParent();
300 void initChildren();
301 void setName(const PdfString& name);
302 void addAlternativeAction(const PdfName& name, const PdfAction& action);
303 static bool tryCreateField(PdfObject& obj, PdfFieldType type,
304 std::unique_ptr<PdfField>& field);
305 std::unique_ptr<PdfField> createChildField(PdfPage* page, const Rect& rect);
306 static PdfFieldType getFieldType(const PdfObject& obj);
307 std::shared_ptr<PdfField> GetPtr();
308 PdfField* getParentTyped(PdfFieldType type) const;
309
310private:
311 PdfAnnotationWidget* m_Widget;
312 PdfAcroForm* m_AcroForm;
313 PdfFieldType m_FieldType;
314 nullable<std::shared_ptr<PdfField>> m_Parent;
315 std::unique_ptr<PdfFieldChildrenCollectionBase> m_Children;
316};
317
318template<typename TField>
319TField* PdfField::GetParentTyped(PdfFieldType type) const
320{
321 return static_cast<TField*>(getParentTyped(type));
322}
323
324template<typename TField>
325constexpr PdfFieldType PdfField::GetFieldType()
326{
327 if (std::is_same_v<TField, PdfPushButton>)
328 return PdfFieldType::PushButton;
329 else if (std::is_same_v<TField, PdfCheckBox>)
330 return PdfFieldType::CheckBox;
331 else if (std::is_same_v<TField, PdfRadioButton>)
332 return PdfFieldType::RadioButton;
333 else if (std::is_same_v<TField, PdfTextBox>)
334 return PdfFieldType::TextBox;
335 else if (std::is_same_v<TField, PdfComboBox>)
336 return PdfFieldType::ComboBox;
337 else if (std::is_same_v<TField, PdfListBox>)
338 return PdfFieldType::ListBox;
339 else if (std::is_same_v<TField, PdfSignature>)
340 return PdfFieldType::Signature;
341 else
342 return PdfFieldType::Unknown;
343}
344
345};
346
347#endif // PDF_FIELD_H
SPDX-FileCopyrightText: (C) 2005 Dominik Seichter domseichter@web.de SPDX-FileCopyrightText: (C) 2020...
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
PdfFieldType
The type of PDF field.
Definition PdfDeclarations.h:650
@ Create
Create a new file or truncate existing one for writing/reading.
PdfHighlightingMode
The possible highlighting modes for a PdfField.
Definition PdfDeclarations.h:670