1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
// Copyright 2016 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "fpdfsdk/cpdfsdk_interform.h"
#include <algorithm>
#include <memory>
#include <vector>
#include "core/fpdfapi/page/cpdf_page.h"
#include "core/fpdfapi/parser/cfdf_document.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fpdfdoc/cpdf_actionfields.h"
#include "core/fpdfdoc/cpdf_interform.h"
#include "core/fxge/cfx_graphstatedata.h"
#include "core/fxge/cfx_pathdata.h"
#include "core/fxge/cfx_renderdevice.h"
#include "fpdfsdk/cba_annotiterator.h"
#include "fpdfsdk/cpdfsdk_annot.h"
#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
#include "fpdfsdk/cpdfsdk_pageview.h"
#include "fpdfsdk/cpdfsdk_widget.h"
#include "fpdfsdk/formfiller/cffl_formfiller.h"
#include "fpdfsdk/fsdk_actionhandler.h"
#include "fpdfsdk/fsdk_define.h"
#include "fpdfsdk/fxedit/fxet_edit.h"
#include "fpdfsdk/ipdfsdk_annothandler.h"
#include "fpdfsdk/javascript/ijs_event_context.h"
#include "fpdfsdk/javascript/ijs_runtime.h"
#include "third_party/base/stl_util.h"
#ifdef PDF_ENABLE_XFA
#include "fpdfsdk/cpdfsdk_xfawidget.h"
#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
#include "xfa/fxfa/cxfa_eventparam.h"
#include "xfa/fxfa/cxfa_ffdocview.h"
#include "xfa/fxfa/cxfa_ffwidget.h"
#include "xfa/fxfa/cxfa_ffwidgethandler.h"
#endif // PDF_ENABLE_XFA
CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv)
: m_pFormFillEnv(pFormFillEnv),
m_pInterForm(
pdfium::MakeUnique<CPDF_InterForm>(m_pFormFillEnv->GetPDFDocument())),
#ifdef PDF_ENABLE_XFA
m_bXfaCalculate(true),
m_bXfaValidationsEnabled(true),
#endif // PDF_ENABLE_XFA
m_bCalculate(true),
m_bBusy(false),
m_iHighlightAlpha(0) {
m_pInterForm->SetFormNotify(this);
for (int i = 0; i < kNumFieldTypes; ++i)
m_bNeedHightlight[i] = false;
}
CPDFSDK_InterForm::~CPDFSDK_InterForm() {
m_Map.clear();
#ifdef PDF_ENABLE_XFA
m_XFAMap.clear();
#endif // PDF_ENABLE_XFA
}
bool CPDFSDK_InterForm::HighlightWidgets() {
return false;
}
CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
bool bNext) const {
auto pIterator = pdfium::MakeUnique<CBA_AnnotIterator>(
pWidget->GetPageView(), CPDF_Annot::Subtype::WIDGET);
if (bNext)
return static_cast<CPDFSDK_Widget*>(pIterator->GetNextAnnot(pWidget));
return static_cast<CPDFSDK_Widget*>(pIterator->GetPrevAnnot(pWidget));
}
CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
if (!pControl || !m_pInterForm)
return nullptr;
CPDFSDK_Widget* pWidget = nullptr;
const auto it = m_Map.find(pControl);
if (it != m_Map.end())
pWidget = it->second;
if (pWidget)
return pWidget;
CPDF_Dictionary* pControlDict = pControl->GetWidget();
CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
CPDFSDK_PageView* pPage = nullptr;
if (CPDF_Dictionary* pPageDict = pControlDict->GetDictFor("P")) {
int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
if (nPageIndex >= 0)
pPage = m_pFormFillEnv->GetPageView(nPageIndex);
}
if (!pPage) {
int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
if (nPageIndex >= 0)
pPage = m_pFormFillEnv->GetPageView(nPageIndex);
}
if (!pPage)
return nullptr;
return static_cast<CPDFSDK_Widget*>(pPage->GetAnnotByDict(pControlDict));
}
void CPDFSDK_InterForm::GetWidgets(
const CFX_WideString& sFieldName,
std::vector<CPDFSDK_Annot::ObservedPtr>* widgets) const {
for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) {
CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
ASSERT(pFormField);
GetWidgets(pFormField, widgets);
}
}
void CPDFSDK_InterForm::GetWidgets(
CPDF_FormField* pField,
std::vector<CPDFSDK_Annot::ObservedPtr>* widgets) const {
for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
CPDF_FormControl* pFormCtrl = pField->GetControl(i);
ASSERT(pFormCtrl);
CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
if (pWidget)
widgets->emplace_back(pWidget);
}
}
int CPDFSDK_InterForm::GetPageIndexByAnnotDict(
CPDF_Document* pDocument,
CPDF_Dictionary* pAnnotDict) const {
ASSERT(pAnnotDict);
for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) {
if (CPDF_Array* pAnnots = pPageDict->GetArrayFor("Annots")) {
for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) {
CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j);
if (pAnnotDict == pDict)
return i;
}
}
}
}
return -1;
}
void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
CPDFSDK_Widget* pWidget) {
m_Map[pControl] = pWidget;
}
void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
m_Map.erase(pControl);
}
void CPDFSDK_InterForm::EnableCalculate(bool bEnabled) {
m_bCalculate = bEnabled;
}
bool CPDFSDK_InterForm::IsCalculateEnabled() const {
return m_bCalculate;
}
#ifdef PDF_ENABLE_XFA
void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget,
CPDFSDK_XFAWidget* pWidget) {
ASSERT(hWidget);
m_XFAMap[hWidget] = pWidget;
}
void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) {
ASSERT(hWidget);
m_XFAMap.erase(hWidget);
}
CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(CXFA_FFWidget* hWidget) {
ASSERT(hWidget);
auto it = m_XFAMap.find(hWidget);
return it != m_XFAMap.end() ? it->second : nullptr;
}
void CPDFSDK_InterForm::XfaEnableCalculate(bool bEnabled) {
m_bXfaCalculate = bEnabled;
}
bool CPDFSDK_InterForm::IsXfaCalculateEnabled() const {
return m_bXfaCalculate;
}
bool CPDFSDK_InterForm::IsXfaValidationsEnabled() {
return m_bXfaValidationsEnabled;
}
void CPDFSDK_InterForm::XfaSetValidationsEnabled(bool bEnabled) {
m_bXfaValidationsEnabled = bEnabled;
}
void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField,
bool bSynchronizeElse) {
for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
pWidget->Synchronize(bSynchronizeElse);
}
}
#endif // PDF_ENABLE_XFA
void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
if (!m_pFormFillEnv->IsJSInitiated())
return;
if (m_bBusy)
return;
m_bBusy = true;
if (!IsCalculateEnabled()) {
m_bBusy = false;
return;
}
IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime();
int nSize = m_pInterForm->CountFieldsInCalculationOrder();
for (int i = 0; i < nSize; i++) {
CPDF_FormField* pField = m_pInterForm->GetFieldInCalculationOrder(i);
if (!pField)
continue;
int nType = pField->GetFieldType();
if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
continue;
CPDF_AAction aAction = pField->GetAdditionalAction();
if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Calculate))
continue;
CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
if (!action.GetDict())
continue;
CFX_WideString csJS = action.GetJavaScript();
if (csJS.IsEmpty())
continue;
IJS_EventContext* pContext = pRuntime->NewEventContext();
CFX_WideString sOldValue = pField->GetValue();
CFX_WideString sValue = sOldValue;
bool bRC = true;
pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
CFX_WideString sInfo;
bool bRet = pContext->RunScript(csJS, &sInfo);
pRuntime->ReleaseEventContext(pContext);
if (bRet && bRC && sValue.Compare(sOldValue) != 0)
pField->SetValue(sValue, true);
}
m_bBusy = false;
}
CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
bool& bFormatted) {
CFX_WideString sValue = pFormField->GetValue();
if (!m_pFormFillEnv->IsJSInitiated()) {
bFormatted = false;
return sValue;
}
IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime();
if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX &&
pFormField->CountSelectedItems() > 0) {
int index = pFormField->GetSelectedIndex(0);
if (index >= 0)
sValue = pFormField->GetOptionLabel(index);
}
bFormatted = false;
CPDF_AAction aAction = pFormField->GetAdditionalAction();
if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
if (action.GetDict()) {
CFX_WideString script = action.GetJavaScript();
if (!script.IsEmpty()) {
CFX_WideString Value = sValue;
IJS_EventContext* pContext = pRuntime->NewEventContext();
pContext->OnField_Format(pFormField, Value, true);
CFX_WideString sInfo;
bool bRet = pContext->RunScript(script, &sInfo);
pRuntime->ReleaseEventContext(pContext);
if (bRet) {
sValue = Value;
bFormatted = true;
}
}
}
}
return sValue;
}
void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
const CFX_WideString* sValue,
bool bValueChanged) {
for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
ASSERT(pFormCtrl);
if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
pWidget->ResetAppearance(sValue, bValueChanged);
}
}
void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
auto* formfiller = m_pFormFillEnv->GetInteractiveFormFiller();
for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
ASSERT(pFormCtrl);
if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
m_pFormFillEnv->Invalidate(
pPage, formfiller->GetViewBBox(
m_pFormFillEnv->GetPageView(pPage, false), pWidget));
}
}
}
bool CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
const CFX_WideString& csValue) {
CPDF_AAction aAction = pFormField->GetAdditionalAction();
if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke))
return true;
CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
if (!action.GetDict())
return true;
CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHandler();
PDFSDK_FieldAction fa;
fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(0);
fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(0);
fa.sValue = csValue;
pActionHandler->DoAction_FieldJavaScript(
action, CPDF_AAction::KeyStroke, m_pFormFillEnv.Get(), pFormField, fa);
return fa.bRC;
}
bool CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
const CFX_WideString& csValue) {
CPDF_AAction aAction = pFormField->GetAdditionalAction();
if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate))
return true;
CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
if (!action.GetDict())
return true;
CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHandler();
PDFSDK_FieldAction fa;
fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(0);
fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(0);
fa.sValue = csValue;
pActionHandler->DoAction_FieldJavaScript(
action, CPDF_AAction::Validate, m_pFormFillEnv.Get(), pFormField, fa);
return fa.bRC;
}
bool CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
ASSERT(action.GetDict());
CPDF_ActionFields af(&action);
std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
bool bHide = action.GetHideStatus();
bool bChanged = false;
for (CPDF_FormField* pField : fields) {
for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
CPDF_FormControl* pControl = pField->GetControl(i);
ASSERT(pControl);
if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
uint32_t nFlags = pWidget->GetFlags();
nFlags &= ~ANNOTFLAG_INVISIBLE;
nFlags &= ~ANNOTFLAG_NOVIEW;
if (bHide)
nFlags |= ANNOTFLAG_HIDDEN;
else
nFlags &= ~ANNOTFLAG_HIDDEN;
pWidget->SetFlags(nFlags);
pWidget->GetPageView()->UpdateView(pWidget);
bChanged = true;
}
}
}
return bChanged;
}
bool CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
CFX_WideString sDestination = action.GetFilePath();
if (sDestination.IsEmpty())
return false;
CPDF_Dictionary* pActionDict = action.GetDict();
if (pActionDict->KeyExist("Fields")) {
CPDF_ActionFields af(&action);
uint32_t dwFlags = action.GetFlags();
std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
if (!fields.empty()) {
bool bIncludeOrExclude = !(dwFlags & 0x01);
if (!m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
return false;
return SubmitFields(sDestination, fields, bIncludeOrExclude, false);
}
}
if (!m_pInterForm->CheckRequiredFields(nullptr, true))
return false;
return SubmitForm(sDestination, false);
}
bool CPDFSDK_InterForm::SubmitFields(const CFX_WideString& csDestination,
const std::vector<CPDF_FormField*>& fields,
bool bIncludeOrExclude,
bool bUrlEncoded) {
CFX_ByteTextBuf textBuf;
ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
uint8_t* pBuffer = textBuf.GetBuffer();
FX_STRSIZE nBufSize = textBuf.GetLength();
if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
return false;
m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
return true;
}
bool CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
CFX_WideString csTxtFile) {
return true;
}
bool CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
FX_STRSIZE& nBufSize) {
std::unique_ptr<CFDF_Document> pFDF =
CFDF_Document::ParseMemory(pBuf, nBufSize);
if (!pFDF)
return true;
CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictFor("FDF");
if (!pMainDict)
return false;
CPDF_Array* pFields = pMainDict->GetArrayFor("Fields");
if (!pFields)
return false;
CFX_ByteTextBuf fdfEncodedData;
for (uint32_t i = 0; i < pFields->GetCount(); i++) {
CPDF_Dictionary* pField = pFields->GetDictAt(i);
if (!pField)
continue;
CFX_WideString name;
name = pField->GetUnicodeTextFor("T");
CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
CFX_ByteString csBValue = pField->GetStringFor("V");
CFX_WideString csWValue = PDF_DecodeText(csBValue);
CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
name_b.ReleaseBuffer();
fdfEncodedData << "=";
fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength());
csValue_b.ReleaseBuffer();
if (i != pFields->GetCount() - 1)
fdfEncodedData << "&";
}
nBufSize = fdfEncodedData.GetLength();
pBuf = FX_Alloc(uint8_t, nBufSize);
memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
return true;
}
bool CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
const std::vector<CPDF_FormField*>& fields,
bool bIncludeOrExclude,
CFX_ByteTextBuf& textBuf) {
std::unique_ptr<CFDF_Document> pFDF = m_pInterForm->ExportToFDF(
m_pFormFillEnv->JS_docGetFilePath(), fields, bIncludeOrExclude, false);
return pFDF ? pFDF->WriteBuf(textBuf) : false;
}
CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
const CFX_WideString& sFileExt) {
return L"";
}
bool CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination,
bool bUrlEncoded) {
if (sDestination.IsEmpty())
return false;
if (!m_pFormFillEnv || !m_pInterForm)
return false;
std::unique_ptr<CFDF_Document> pFDFDoc =
m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false);
if (!pFDFDoc)
return false;
CFX_ByteTextBuf FdfBuffer;
if (!pFDFDoc->WriteBuf(FdfBuffer))
return false;
uint8_t* pBuffer = FdfBuffer.GetBuffer();
FX_STRSIZE nBufSize = FdfBuffer.GetLength();
if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
return false;
m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
if (bUrlEncoded)
FX_Free(pBuffer);
return true;
}
bool CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
std::unique_ptr<CFDF_Document> pFDF =
m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false);
return pFDF && pFDF->WriteBuf(textBuf);
}
bool CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
ASSERT(action.GetDict());
CPDF_Dictionary* pActionDict = action.GetDict();
if (!pActionDict->KeyExist("Fields"))
return m_pInterForm->ResetForm(true);
CPDF_ActionFields af(&action);
uint32_t dwFlags = action.GetFlags();
std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true);
}
bool CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
return false;
}
std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects(
const std::vector<CPDF_Object*>& objects) const {
std::vector<CPDF_FormField*> fields;
for (CPDF_Object* pObject : objects) {
if (pObject && pObject->IsString()) {
CFX_WideString csName = pObject->GetUnicodeText();
CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
if (pField)
fields.push_back(pField);
}
}
return fields;
}
int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField,
const CFX_WideString& csValue) {
int nType = pField->GetFieldType();
if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
return 0;
if (!OnKeyStrokeCommit(pField, csValue))
return -1;
if (!OnValidate(pField, csValue))
return -1;
return 1;
}
void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
#ifdef PDF_ENABLE_XFA
SynchronizeField(pField, false);
#endif // PDF_ENABLE_XFA
int nType = pField->GetFieldType();
if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
OnCalculate(pField);
bool bFormatted = false;
CFX_WideString sValue = OnFormat(pField, bFormatted);
ResetFieldAppearance(pField, bFormatted ? &sValue : nullptr, true);
UpdateField(pField);
}
}
int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField,
const CFX_WideString& csValue) {
if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
return 0;
if (!OnKeyStrokeCommit(pField, csValue))
return -1;
if (!OnValidate(pField, csValue))
return -1;
return 1;
}
void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
return;
OnCalculate(pField);
ResetFieldAppearance(pField, nullptr, true);
UpdateField(pField);
}
void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) {
int nType = pField->GetFieldType();
if (nType != FIELDTYPE_CHECKBOX && nType != FIELDTYPE_RADIOBUTTON)
return;
OnCalculate(pField);
UpdateField(pField);
}
int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) {
return 0;
}
void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) {
OnCalculate(nullptr);
}
int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) {
return 0;
}
void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) {
OnCalculate(nullptr);
}
bool CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
if (nFieldType < 1 || nFieldType > kNumFieldTypes)
return false;
return m_bNeedHightlight[nFieldType - 1];
}
void CPDFSDK_InterForm::RemoveAllHighLight() {
for (int i = 0; i < kNumFieldTypes; ++i)
m_bNeedHightlight[i] = false;
}
void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
if (nFieldType < 0 || nFieldType > kNumFieldTypes)
return;
switch (nFieldType) {
case 0: {
for (int i = 0; i < kNumFieldTypes; ++i) {
m_aHighlightColor[i] = clr;
m_bNeedHightlight[i] = true;
}
break;
}
default: {
m_aHighlightColor[nFieldType - 1] = clr;
m_bNeedHightlight[nFieldType - 1] = true;
break;
}
}
}
FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
if (nFieldType < 0 || nFieldType > kNumFieldTypes)
return FXSYS_RGB(255, 255, 255);
if (nFieldType == 0)
return m_aHighlightColor[0];
return m_aHighlightColor[nFieldType - 1];
}
|