File indexing completed on 2025-12-10 10:23:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #ifndef FORM_H
0035 #define FORM_H
0036
0037 #include "Annot.h"
0038 #include "CharTypes.h"
0039 #include "Object.h"
0040 #include "poppler_private_export.h"
0041
0042 #include <ctime>
0043
0044 #include <optional>
0045 #include <set>
0046 #include <vector>
0047
0048 class GooString;
0049 class Array;
0050 class Dict;
0051 class Annot;
0052 class AnnotWidget;
0053 class Annots;
0054 class LinkAction;
0055 class GfxResources;
0056 class PDFDoc;
0057 class SignatureInfo;
0058 class X509CertificateInfo;
0059 class SignatureVerificationHandler;
0060
0061 enum FormFieldType
0062 {
0063 formButton,
0064 formText,
0065 formChoice,
0066 formSignature,
0067 formUndef
0068 };
0069
0070 enum FormButtonType
0071 {
0072 formButtonCheck,
0073 formButtonPush,
0074 formButtonRadio
0075 };
0076
0077 enum FormSignatureType
0078 {
0079 adbe_pkcs7_sha1,
0080 adbe_pkcs7_detached,
0081 ETSI_CAdES_detached,
0082 unknown_signature_type,
0083 unsigned_signature_field
0084 };
0085
0086 enum FillValueType
0087 {
0088 fillValue,
0089 fillDefaultValue
0090 };
0091
0092 class Form;
0093 class FormField;
0094 class FormFieldButton;
0095 class FormFieldText;
0096 class FormFieldSignature;
0097 class FormFieldChoice;
0098
0099
0100
0101
0102
0103
0104
0105 class POPPLER_PRIVATE_EXPORT FormWidget
0106 {
0107 public:
0108 virtual ~FormWidget();
0109
0110
0111 bool inRect(double x, double y) const;
0112
0113
0114 void getRect(double *x1, double *y1, double *x2, double *y2) const;
0115
0116 unsigned getID() { return ID; }
0117 void setID(unsigned int i) { ID = i; }
0118
0119 FormField *getField() { return field; }
0120 FormFieldType getType() { return type; }
0121
0122 Object *getObj() { return &obj; }
0123 Ref getRef() { return ref; }
0124
0125 void setChildNum(unsigned i) { childNum = i; }
0126 unsigned getChildNum() { return childNum; }
0127
0128 const GooString *getPartialName() const;
0129 void setPartialName(const GooString &name);
0130 const GooString *getAlternateUiName() const;
0131 const GooString *getMappingName() const;
0132 GooString *getFullyQualifiedName();
0133
0134 bool isModified() const;
0135
0136 bool isReadOnly() const;
0137 void setReadOnly(bool value);
0138
0139 LinkAction *getActivationAction();
0140 std::unique_ptr<LinkAction> getAdditionalAction(Annot::FormAdditionalActionsType type);
0141 bool setAdditionalAction(Annot::FormAdditionalActionsType t, const GooString &js);
0142
0143
0144 static int encodeID(unsigned pageNum, unsigned fieldNum);
0145
0146 static void decodeID(unsigned id, unsigned *pageNum, unsigned *fieldNum);
0147
0148 void createWidgetAnnotation();
0149 AnnotWidget *getWidgetAnnotation() const { return widget; }
0150 void setWidgetAnnotation(AnnotWidget *_widget) { widget = _widget; }
0151
0152 virtual void updateWidgetAppearance() = 0;
0153
0154 void print(int indent = 0);
0155
0156 protected:
0157 FormWidget(PDFDoc *docA, Object *aobj, unsigned num, Ref aref, FormField *fieldA);
0158
0159 AnnotWidget *widget;
0160 FormField *field;
0161 FormFieldType type;
0162 Object obj;
0163 Ref ref;
0164 PDFDoc *doc;
0165 XRef *xref;
0166
0167
0168 unsigned childNum;
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 unsigned ID;
0179 };
0180
0181
0182
0183
0184
0185 class POPPLER_PRIVATE_EXPORT FormWidgetButton : public FormWidget
0186 {
0187 public:
0188 FormWidgetButton(PDFDoc *docA, Object *dictObj, unsigned num, Ref ref, FormField *p);
0189 ~FormWidgetButton() override;
0190
0191 FormButtonType getButtonType() const;
0192
0193 void setState(bool state);
0194 bool getState() const;
0195
0196 const char *getOnStr() const;
0197 void setAppearanceState(const char *state);
0198 void updateWidgetAppearance() override;
0199
0200 protected:
0201 FormFieldButton *parent() const;
0202 GooString *onStr;
0203 };
0204
0205
0206
0207
0208
0209 class POPPLER_PRIVATE_EXPORT FormWidgetText : public FormWidget
0210 {
0211 public:
0212 FormWidgetText(PDFDoc *docA, Object *dictObj, unsigned num, Ref ref, FormField *p);
0213
0214 const GooString *getContent() const;
0215
0216
0217 void setContent(const GooString *new_content);
0218
0219 void setAppearanceContent(const GooString *new_content);
0220
0221 void updateWidgetAppearance() override;
0222
0223 bool isMultiline() const;
0224 bool isPassword() const;
0225 bool isFileSelect() const;
0226 bool noSpellCheck() const;
0227 bool noScroll() const;
0228 bool isComb() const;
0229 bool isRichText() const;
0230 int getMaxLen() const;
0231
0232 double getTextFontSize();
0233
0234 void setTextFontSize(int fontSize);
0235
0236 protected:
0237 FormFieldText *parent() const;
0238 };
0239
0240
0241
0242
0243
0244 class POPPLER_PRIVATE_EXPORT FormWidgetChoice : public FormWidget
0245 {
0246 public:
0247 FormWidgetChoice(PDFDoc *docA, Object *dictObj, unsigned num, Ref ref, FormField *p);
0248 ~FormWidgetChoice() override;
0249
0250 int getNumChoices() const;
0251
0252 const GooString *getChoice(int i) const;
0253 const GooString *getExportVal(int i) const;
0254
0255 void select(int i);
0256
0257
0258 void toggle(int i);
0259
0260
0261 void deselectAll();
0262
0263
0264
0265 void setEditChoice(const GooString *new_content);
0266
0267 const GooString *getEditChoice() const;
0268
0269 void updateWidgetAppearance() override;
0270 bool isSelected(int i) const;
0271
0272 bool isCombo() const;
0273 bool hasEdit() const;
0274 bool isMultiSelect() const;
0275 bool noSpellCheck() const;
0276 bool commitOnSelChange() const;
0277 bool isListBox() const;
0278
0279 protected:
0280 bool _checkRange(int i) const;
0281 FormFieldChoice *parent() const;
0282 };
0283
0284
0285
0286
0287
0288 class POPPLER_PRIVATE_EXPORT FormWidgetSignature : public FormWidget
0289 {
0290 public:
0291 FormWidgetSignature(PDFDoc *docA, Object *dictObj, unsigned num, Ref ref, FormField *p);
0292 void updateWidgetAppearance() override;
0293
0294 FormSignatureType signatureType() const;
0295 void setSignatureType(FormSignatureType fst);
0296
0297
0298 SignatureInfo *validateSignature(bool doVerifyCert, bool forceRevalidation, time_t validationTime, bool ocspRevocationCheck, bool enableAIA);
0299
0300
0301
0302 std::vector<Goffset> getSignedRangeBounds() const;
0303
0304
0305
0306
0307
0308
0309
0310
0311 bool signDocument(const std::string &filename, const std::string &certNickname, const std::string &password, const GooString *reason = nullptr, const GooString *location = nullptr, const std::optional<GooString> &ownerPassword = {},
0312 const std::optional<GooString> &userPassword = {});
0313
0314
0315 bool signDocumentWithAppearance(const std::string &filename, const std::string &certNickname, const std::string &password, const GooString *reason = nullptr, const GooString *location = nullptr,
0316 const std::optional<GooString> &ownerPassword = {}, const std::optional<GooString> &userPassword = {}, const GooString &signatureText = {}, const GooString &signatureTextLeft = {}, double fontSize = {},
0317 double leftFontSize = {}, std::unique_ptr<AnnotColor> &&fontColor = {}, double borderWidth = {}, std::unique_ptr<AnnotColor> &&borderColor = {}, std::unique_ptr<AnnotColor> &&backgroundColor = {});
0318
0319
0320
0321
0322 std::optional<GooString> getCheckedSignature(Goffset *checkedFileSize);
0323
0324 const GooString *getSignature() const;
0325
0326 private:
0327 bool createSignature(Object &vObj, Ref vRef, const GooString &name, int placeholderLength, const GooString *reason = nullptr, const GooString *location = nullptr);
0328 bool getObjectStartEnd(const GooString &filename, int objNum, Goffset *objStart, Goffset *objEnd, const std::optional<GooString> &ownerPassword, const std::optional<GooString> &userPassword);
0329 bool updateOffsets(FILE *f, Goffset objStart, Goffset objEnd, Goffset *sigStart, Goffset *sigEnd, Goffset *fileSize);
0330
0331 bool updateSignature(FILE *f, Goffset sigStart, Goffset sigEnd, const GooString *signature);
0332 };
0333
0334
0335
0336
0337
0338
0339
0340
0341 class POPPLER_PRIVATE_EXPORT FormField
0342 {
0343 public:
0344 FormField(PDFDoc *docA, Object &&aobj, const Ref aref, FormField *parent, std::set<int> *usedParents, FormFieldType t = formUndef);
0345
0346 virtual ~FormField();
0347
0348
0349 FormFieldType getType() const { return type; }
0350 Object *getObj() { return &obj; }
0351 Ref getRef() { return ref; }
0352
0353 void setReadOnly(bool value);
0354 bool isReadOnly() const { return readOnly; }
0355 void setStandAlone(bool value) { standAlone = value; }
0356 bool isStandAlone() const { return standAlone; }
0357
0358 GooString *getDefaultAppearance() const { return defaultAppearance; }
0359 void setDefaultAppearance(const std::string &appearance);
0360
0361 bool hasTextQuadding() const { return hasQuadding; }
0362 VariableTextQuadding getTextQuadding() const { return quadding; }
0363
0364 const GooString *getPartialName() const { return partialName; }
0365 void setPartialName(const GooString &name);
0366 const GooString *getAlternateUiName() const { return alternateUiName; }
0367 const GooString *getMappingName() const { return mappingName; }
0368 GooString *getFullyQualifiedName();
0369
0370 FormWidget *findWidgetByRef(Ref aref);
0371 int getNumWidgets() const { return terminal ? numChildren : 0; }
0372 FormWidget *getWidget(int i) const { return terminal ? widgets[i] : nullptr; }
0373 int getNumChildren() const { return !terminal ? numChildren : 0; }
0374 FormField *getChildren(int i) const { return children[i]; }
0375
0376
0377 virtual void fillChildrenSiblingsID();
0378
0379 void createWidgetAnnotations();
0380
0381 void printTree(int indent = 0);
0382 virtual void print(int indent = 0);
0383 virtual void reset(const std::vector<std::string> &excludedFields);
0384 void resetChildren(const std::vector<std::string> &excludedFields);
0385 FormField *findFieldByRef(Ref aref);
0386 FormField *findFieldByFullyQualifiedName(const std::string &name);
0387
0388 protected:
0389 void _createWidget(Object *obj, Ref aref);
0390 void createChildren(std::set<int> *usedParents);
0391 void updateChildrenAppearance();
0392 bool isAmongExcludedFields(const std::vector<std::string> &excludedFields);
0393
0394 FormFieldType type;
0395 Ref ref;
0396 bool terminal;
0397 Object obj;
0398 PDFDoc *doc;
0399 XRef *xref;
0400 FormField **children;
0401 FormField *parent;
0402 int numChildren;
0403 FormWidget **widgets;
0404 bool readOnly;
0405
0406 GooString *partialName;
0407 GooString *alternateUiName;
0408 GooString *mappingName;
0409 GooString *fullyQualifiedName;
0410
0411
0412 GooString *defaultAppearance;
0413 bool hasQuadding;
0414 VariableTextQuadding quadding;
0415
0416
0417 bool standAlone;
0418
0419 private:
0420 FormField() { }
0421 };
0422
0423
0424
0425
0426
0427 class FormFieldButton : public FormField
0428 {
0429 public:
0430 FormFieldButton(PDFDoc *docA, Object &&dict, const Ref ref, FormField *parent, std::set<int> *usedParents);
0431
0432 FormButtonType getButtonType() const { return btype; }
0433
0434 bool noToggleToOff() const { return noAllOff; }
0435
0436
0437 bool setState(const char *state, bool ignoreToggleOff = false);
0438 bool getState(const char *state) const;
0439
0440 const char *getAppearanceState() const { return appearanceState.isName() ? appearanceState.getName() : nullptr; }
0441 const char *getDefaultAppearanceState() const { return defaultAppearanceState.isName() ? defaultAppearanceState.getName() : nullptr; }
0442
0443 void fillChildrenSiblingsID() override;
0444
0445 void setNumSiblings(int num);
0446 void setSibling(int i, FormFieldButton *id) { siblings[i] = id; }
0447
0448
0449 FormFieldButton *getSibling(int i) const { return siblings[i]; }
0450 int getNumSiblings() const { return numSiblings; }
0451
0452 void print(int indent) override;
0453 void reset(const std::vector<std::string> &excludedFields) override;
0454
0455 ~FormFieldButton() override;
0456
0457 protected:
0458 void updateState(const char *state);
0459
0460 FormFieldButton **siblings;
0461
0462 int numSiblings;
0463
0464 FormButtonType btype;
0465 int size;
0466 int active_child;
0467 bool noAllOff;
0468 Object appearanceState;
0469 Object defaultAppearanceState;
0470 };
0471
0472
0473
0474
0475
0476 class FormFieldText : public FormField
0477 {
0478 public:
0479 FormFieldText(PDFDoc *docA, Object &&dictObj, const Ref ref, FormField *parent, std::set<int> *usedParents);
0480
0481 const GooString *getContent() const { return content; }
0482 const GooString *getAppearanceContent() const { return internalContent ? internalContent : content; }
0483 void setContentCopy(const GooString *new_content);
0484 void setAppearanceContentCopy(const GooString *new_content);
0485 ~FormFieldText() override;
0486
0487 bool isMultiline() const { return multiline; }
0488 bool isPassword() const { return password; }
0489 bool isFileSelect() const { return fileSelect; }
0490 bool noSpellCheck() const { return doNotSpellCheck; }
0491 bool noScroll() const { return doNotScroll; }
0492 bool isComb() const { return comb; }
0493 bool isRichText() const { return richText; }
0494
0495 int getMaxLen() const { return maxLen; }
0496
0497
0498 double getTextFontSize();
0499
0500 void setTextFontSize(int fontSize);
0501
0502 void print(int indent) override;
0503 void reset(const std::vector<std::string> &excludedFields) override;
0504
0505 static int tokenizeDA(const std::string &daString, std::vector<std::string> *daToks, const char *searchTok);
0506
0507 protected:
0508 int parseDA(std::vector<std::string> *daToks);
0509 void fillContent(FillValueType fillType);
0510
0511 GooString *content;
0512 GooString *internalContent;
0513 GooString *defaultContent;
0514 bool multiline;
0515 bool password;
0516 bool fileSelect;
0517 bool doNotSpellCheck;
0518 bool doNotScroll;
0519 bool comb;
0520 bool richText;
0521 int maxLen;
0522 };
0523
0524
0525
0526
0527
0528 class FormFieldChoice : public FormField
0529 {
0530 public:
0531 FormFieldChoice(PDFDoc *docA, Object &&aobj, const Ref ref, FormField *parent, std::set<int> *usedParents);
0532
0533 ~FormFieldChoice() override;
0534
0535 int getNumChoices() const { return numChoices; }
0536 const GooString *getChoice(int i) const { return choices ? choices[i].optionName : nullptr; }
0537 const GooString *getExportVal(int i) const { return choices ? choices[i].exportVal : nullptr; }
0538
0539 const GooString *getSelectedChoice() const;
0540
0541
0542 void select(int i);
0543
0544
0545 void toggle(int i);
0546
0547
0548 void deselectAll();
0549
0550
0551 void setEditChoice(const GooString *new_content);
0552
0553 const GooString *getEditChoice() const;
0554
0555 bool isSelected(int i) const { return choices[i].selected; }
0556
0557 int getNumSelected();
0558
0559 bool isCombo() const { return combo; }
0560 bool hasEdit() const { return edit; }
0561 bool isMultiSelect() const { return multiselect; }
0562 bool noSpellCheck() const { return doNotSpellCheck; }
0563 bool commitOnSelChange() const { return doCommitOnSelChange; }
0564 bool isListBox() const { return !combo; }
0565
0566 int getTopIndex() const { return topIdx; }
0567
0568 void print(int indent) override;
0569 void reset(const std::vector<std::string> &excludedFields) override;
0570
0571 protected:
0572 void unselectAll();
0573 void updateSelection();
0574 void fillChoices(FillValueType fillType);
0575
0576 bool combo;
0577 bool edit;
0578 bool multiselect;
0579 bool doNotSpellCheck;
0580 bool doCommitOnSelChange;
0581
0582 struct ChoiceOpt
0583 {
0584 GooString *exportVal;
0585 GooString *optionName;
0586 bool selected;
0587 };
0588
0589 int numChoices;
0590 ChoiceOpt *choices;
0591 bool *defaultChoices;
0592 GooString *editedChoice;
0593 int topIdx;
0594 };
0595
0596
0597
0598
0599
0600 class POPPLER_PRIVATE_EXPORT FormFieldSignature : public FormField
0601 {
0602 public:
0603 FormFieldSignature(PDFDoc *docA, Object &&dict, const Ref ref, FormField *parent, std::set<int> *usedParents);
0604
0605
0606 SignatureInfo *validateSignature(bool doVerifyCert, bool forceRevalidation, time_t validationTime, bool ocspRevocationCheck, bool enableAIA);
0607
0608
0609
0610 std::vector<Goffset> getSignedRangeBounds() const;
0611
0612
0613
0614
0615 std::optional<GooString> getCheckedSignature(Goffset *checkedFileSize);
0616
0617 ~FormFieldSignature() override;
0618 Object *getByteRange() { return &byte_range; }
0619 const GooString *getSignature() const { return signature; }
0620 void setSignature(const GooString &sig);
0621 FormSignatureType getSignatureType() const { return signature_type; }
0622 void setSignatureType(FormSignatureType t) { signature_type = t; }
0623
0624 const GooString &getCustomAppearanceContent() const;
0625 void setCustomAppearanceContent(const GooString &s);
0626
0627 const GooString &getCustomAppearanceLeftContent() const;
0628 void setCustomAppearanceLeftContent(const GooString &s);
0629
0630 double getCustomAppearanceLeftFontSize() const;
0631 void setCustomAppearanceLeftFontSize(double size);
0632
0633
0634 Ref getImageResource() const;
0635 void setImageResource(const Ref imageResourceA);
0636
0637 void setCertificateInfo(std::unique_ptr<X509CertificateInfo> &);
0638
0639 FormWidget *getCreateWidget();
0640
0641 private:
0642 void parseInfo();
0643 void hashSignedDataBlock(SignatureVerificationHandler *handler, Goffset block_len);
0644
0645 FormSignatureType signature_type;
0646 Object byte_range;
0647 GooString *signature;
0648 SignatureInfo *signature_info;
0649 GooString customAppearanceContent;
0650 GooString customAppearanceLeftContent;
0651 double customAppearanceLeftFontSize = 20;
0652 Ref imageResource = Ref::INVALID();
0653 std::unique_ptr<X509CertificateInfo> certificate_info;
0654
0655 void print(int indent) override;
0656 };
0657
0658
0659
0660
0661
0662
0663
0664 class POPPLER_PRIVATE_EXPORT Form
0665 {
0666 public:
0667 explicit Form(PDFDoc *doc);
0668
0669 ~Form();
0670
0671 Form(const Form &) = delete;
0672 Form &operator=(const Form &) = delete;
0673
0674
0675 static Object fieldLookup(Dict *field, const char *key);
0676
0677
0678
0679
0680 static FormField *createFieldFromDict(Object &&obj, PDFDoc *docA, const Ref aref, FormField *parent, std::set<int> *usedParents);
0681
0682
0683
0684 std::string findFontInDefaultResources(const std::string &fontFamily, const std::string &fontStyle) const;
0685
0686 struct AddFontResult
0687 {
0688 std::string fontName;
0689 Ref ref;
0690 };
0691
0692
0693
0694 AddFontResult addFontToDefaultResources(const std::string &fontFamily, const std::string &fontStyle);
0695
0696
0697
0698 std::string getFallbackFontForChar(Unicode uChar, const GfxFont &fontToEmulate) const;
0699
0700
0701
0702
0703
0704 std::vector<AddFontResult> ensureFontsForAllCharacters(const GooString *unicodeText, const std::string &pdfFontNameToEmulate, GfxResources *fieldResources = nullptr);
0705
0706 bool getNeedAppearances() const { return needAppearances; }
0707 int getNumFields() const { return numFields; }
0708 FormField *getRootField(int i) const { return rootFields[i]; }
0709 const GooString *getDefaultAppearance() const { return defaultAppearance; }
0710 VariableTextQuadding getTextQuadding() const { return quadding; }
0711 GfxResources *getDefaultResources() const { return defaultResources; }
0712 Object *getDefaultResourcesObj() { return &resDict; }
0713
0714 FormWidget *findWidgetByRef(Ref aref);
0715 FormField *findFieldByRef(Ref aref) const;
0716 FormField *findFieldByFullyQualifiedName(const std::string &name) const;
0717
0718 void postWidgetsLoad();
0719
0720 const std::vector<Ref> &getCalculateOrder() const { return calculateOrder; }
0721
0722 void reset(const std::vector<std::string> &fields, bool excludeFields);
0723
0724 private:
0725
0726
0727 AddFontResult addFontToDefaultResources(const std::string &filepath, int faceIndex, const std::string &fontFamily, const std::string &fontStyle);
0728
0729 AddFontResult doGetAddFontToDefaultResources(Unicode uChar, const GfxFont &fontToEmulate);
0730
0731 FormField **rootFields;
0732 int numFields;
0733 int size;
0734 PDFDoc *const doc;
0735 bool needAppearances;
0736 GfxResources *defaultResources;
0737 Object resDict;
0738 std::vector<Ref> calculateOrder;
0739
0740
0741 GooString *defaultAppearance;
0742 VariableTextQuadding quadding;
0743 };
0744
0745
0746
0747
0748
0749 class POPPLER_PRIVATE_EXPORT FormPageWidgets
0750 {
0751 public:
0752 FormPageWidgets(Annots *annots, unsigned int page, Form *form);
0753 ~FormPageWidgets();
0754
0755 FormPageWidgets(const FormPageWidgets &) = delete;
0756 FormPageWidgets &operator=(const FormPageWidgets &) = delete;
0757
0758 int getNumWidgets() const { return numWidgets; }
0759 FormWidget *getWidget(int i) const { return widgets[i]; }
0760 void addWidgets(const std::vector<FormField *> &addedWidgets, unsigned int page);
0761
0762 private:
0763 FormWidget **widgets;
0764 int numWidgets;
0765 int size;
0766 };
0767
0768 #endif