-
-
Notifications
You must be signed in to change notification settings - Fork 448
/
fill.go
1121 lines (938 loc) · 22.4 KB
/
fill.go
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2023 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package form
import (
"bytes"
"sort"
"strconv"
"strings"
pdffont "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/font"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
"github.com/pkg/errors"
)
type DataFormat int
const (
CSV DataFormat = iota
JSON
)
func cacheResIDs(ctx *model.Context, pdf *primitives.PDF) error {
// Iterate over all pages of ctx and prepare a resIds []string for inherited "Font" and "XObject" resources.
for i := 1; i <= ctx.PageCount; i++ {
_, _, inhPA, err := ctx.PageDict(i, true)
if err != nil {
return err
}
if inhPA.Resources["Font"] != nil {
pdf.FontResIDs[i] = inhPA.Resources["Font"].(types.Dict)
}
if inhPA.Resources["XObject"] != nil {
pdf.XObjectResIDs[i] = inhPA.Resources["XObject"].(types.Dict)
}
}
return nil
}
func addImages(ctx *model.Context, pages map[string]*Page) ([]*model.Page, error) {
pdf := &primitives.PDF{
FieldIDs: types.StringSet{},
Fields: types.Array{},
FormFonts: map[string]*primitives.FormFont{},
Pages: map[string]*primitives.PDFPage{},
FontResIDs: map[int]types.Dict{},
XObjectResIDs: map[int]types.Dict{},
Conf: ctx.Configuration,
XRefTable: ctx.XRefTable,
Optimize: ctx.Optimize,
CheckBoxAPs: map[float64]*primitives.AP{},
RadioBtnAPs: map[float64]*primitives.AP{},
OldFieldIDs: types.StringSet{},
Debug: false,
}
if err := cacheResIDs(ctx, pdf); err != nil {
return nil, err
}
// What follows is a quirky way of turning a map of pages into a sorted slice of pages
// including entries for pages that are missing in the map.
var pageNrs []int
for pageNr := range pages {
nr, err := strconv.Atoi(pageNr)
if err != nil {
return nil, errors.Errorf("pdfcpu: invalid page number: %s", pageNr)
}
pageNrs = append(pageNrs, nr)
}
sort.Ints(pageNrs)
pp := []*Page{}
maxPageNr := pageNrs[len(pageNrs)-1]
for i := 1; i <= maxPageNr; i++ {
pp = append(pp, pages[strconv.Itoa(i)])
}
mp := []*model.Page{}
imageMap := model.ImageMap{}
for i, page := range pp {
pageNr := i + 1
_, _, inhPAttrs, err := ctx.PageDict(pageNr, false)
if err != nil {
return nil, err
}
p := model.Page{
MediaBox: inhPAttrs.MediaBox,
CropBox: inhPAttrs.CropBox,
Fm: model.FontMap{},
Im: model.ImageMap{},
AnnotTabs: map[int]model.FieldAnnotation{},
Buf: new(bytes.Buffer),
}
if page == nil {
if pageNr <= pdf.XRefTable.PageCount {
mp = append(mp, nil)
continue
}
}
for _, ib := range page.ImageBoxes {
if err := ib.RenderForFill(pdf, &p, pageNr, imageMap); err != nil {
return nil, err
}
}
mp = append(mp, &p)
}
return mp, nil
}
// CSVFieldAttributes represent the value(s) and the lock state for a field.
type CSVFieldAttributes struct {
Values []string
Lock bool
}
func parsePageNr(s string, ib *primitives.ImageBox) error {
_, err := strconv.Atoi(s)
if err != nil {
return err
}
ib.PageNr = s
return nil
}
func parseWidth(s string, ib *primitives.ImageBox) error {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
ib.Width = f
return nil
}
func parseHeight(s string, ib *primitives.ImageBox) error {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
ib.Height = f
return nil
}
func parsePositionAnchor(s string, ib *primitives.ImageBox) error {
d := strings.Split(s, " ")
if len(d) < 1 || len(d) > 2 {
return errors.Errorf("pdfcpu: illegal position string: need 1 or 2 values, %s\n", s)
}
if len(d) == 1 {
_, err := types.ParsePositionAnchor(s)
if err != nil {
return err
}
ib.Anchor = s
return nil
}
x, err := strconv.ParseFloat(d[0], 64)
if err != nil {
return err
}
y, err := strconv.ParseFloat(d[1], 64)
if err != nil {
return err
}
ib.Position = [2]float64{x, y}
return nil
}
func parsePositionOffset(s string, ib *primitives.ImageBox) error {
d := strings.Split(s, " ")
if len(d) != 2 {
return errors.Errorf("pdfcpu: illegal position offset string: need 2 numeric values, %s\n", s)
}
f, err := strconv.ParseFloat(d[0], 64)
if err != nil {
return err
}
ib.Dx = f
f, err = strconv.ParseFloat(d[1], 64)
if err != nil {
return err
}
ib.Dy = f
return nil
}
func parseImgBackgroundColor(s string, ib *primitives.ImageBox) error {
ib.BackgroundColor = s
return nil
}
func parseImgBorder(s string, ib *primitives.ImageBox) error {
var err error
b := strings.Split(s, " ")
if len(b) == 0 || len(b) > 5 {
return errors.Errorf("pdfcpu: borders: need between 1 and 5 components, %s\n", s)
}
ib.Border = &primitives.Border{}
border := ib.Border
border.Width, err = strconv.Atoi(b[0])
if err != nil {
return err
}
if border.Width == 0 {
return errors.New("pdfcpu: borders: need width > 0")
}
if len(b) == 1 {
return nil
}
st := strings.ToLower(b[1])
if types.MemberOf(st, []string{"bevel", "miter", "round"}) {
border.Style = st
if len(b) > 2 {
border.Color = strings.Join(b[2:], " ")
}
return nil
}
border.Color = strings.Join(b[1:], " ")
return nil
}
type imageBoxParamMap map[string]func(string, *primitives.ImageBox) error
var imgParamMap = imageBoxParamMap{
"bgcolor": parseImgBackgroundColor,
"border": parseImgBorder,
"offset": parsePositionOffset,
"page": parsePageNr,
"position": parsePositionAnchor,
"width": parseWidth,
"height": parseHeight,
}
func (m imageBoxParamMap) processImageBoxArg(paramPrefix, paramValueStr string, ib *primitives.ImageBox) error {
var param string
// Completion support
for k := range m {
if !strings.HasPrefix(k, paramPrefix) {
continue
}
if len(param) > 0 {
return errors.Errorf("pdfcpu: ambiguous parameter prefix \"%s\"", paramPrefix)
}
param = k
}
if param == "" {
return errors.Errorf("pdfcpu: unknown parameter prefix \"%s\"", paramPrefix)
}
return m[param](paramValueStr, ib)
}
func imageBox(s, src, url string) (*primitives.ImageBox, string, error) {
if !strings.HasPrefix(s, "@img") || len(s) < 6 {
return nil, "", errors.Errorf("pdfcpu: parsing cvs fieldNames: missing @img: <%s>", s)
}
s = s[4:]
if s[0] != '(' || s[len(s)-1] != ')' {
return nil, "", errors.Errorf("pdfcpu: parsing cvs fieldNames: corrupted @img: <%s>", s)
}
s = s[1 : len(s)-1]
if len(s) == 0 {
return nil, "", errors.Errorf("pdfcpu: parsing cvs fieldNames: empty @img: <%s>", s)
}
ib := primitives.ImageBox{Src: src, Dx: 0, Dy: 0, Width: 0, Height: 0}
if url != "" {
ib.Url = url
}
ss := strings.Split(s, ",")
for _, s := range ss {
ss1 := strings.Split(s, ":")
if len(ss1) != 2 {
return nil, "", errors.Errorf("pdfcpu: parsing cvs fieldNames: corrupted @img: <%s>", s)
}
paramPrefix := strings.TrimSpace(ss1[0])
paramValueStr := strings.TrimSpace(ss1[1])
if err := imgParamMap.processImageBoxArg(paramPrefix, paramValueStr, &ib); err != nil {
return nil, "", err
}
}
return &ib, ib.PageNr, nil
}
// FieldMap returns structures needed to fill a form via CSV.
func FieldMap(fieldNames, formRecord []string) (map[string]CSVFieldAttributes, map[string]*Page, error) {
fm := map[string]CSVFieldAttributes{}
im := map[string]*Page{}
for i, fieldName := range fieldNames {
var lock bool
if fieldName[0] == '*' {
lock = true
fieldName = fieldName[1:]
if fieldName[0] == '@' {
continue
}
}
vv := strings.Split(formRecord[i], ",")
if fieldName[0] != '@' {
v1 := vv[0]
if len(v1) > 1 && v1[0] == '*' {
lock = true
vv[0] = vv[0][1:]
}
fm[fieldName] = CSVFieldAttributes{Values: vv, Lock: lock}
continue
}
// @img defines a virtual image field by rendering an imageBox.
// For CSV we keep it simple and support the most important imageBox attributes only:
//
// "@img(page:1, pos:40 350, w:290, h:200, bgcol:#F5F5DC, border:5 round LightGray)"
if len(vv) == 0 || len(vv) > 2 {
// Skip invalid image field
continue
}
src, url := "", ""
if len(vv) == 1 && vv[0][0] == '(' {
// link only, no image
url = vv[0][1 : len(vv[0])-1]
} else {
src = vv[0]
if len(vv) == 2 {
url = vv[1][1 : len(vv[1])-1]
}
}
ib, pageNr, err := imageBox(fieldName, src, url)
if err != nil {
return nil, nil, err
}
if ib == nil {
continue
}
p, ok := im[pageNr]
if !ok {
p = &Page{}
im[pageNr] = p
}
p.ImageBoxes = append(p.ImageBoxes, ib)
}
return fm, im, nil
}
// FillDetails returns a closure that returns new form data provided by CSV or JSON.
func FillDetails(form *Form, fieldMap map[string]CSVFieldAttributes) func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool) {
f := form
fm := fieldMap
return func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool) {
if format == CSV {
fa, ok := fm[id]
if ok {
return fa.Values, fa.Lock, true
}
fa, ok = fm[name]
if ok {
return fa.Values, fa.Lock, true
}
return nil, false, false
}
switch fieldType {
case FTCheckBox:
v, lock, ok := form.checkBoxValueAndLock(id, name)
c := "f"
if v {
c = "t"
}
return []string{c}, lock, ok
case FTRadioButtonGroup:
v, lock, ok := form.radioButtonGroupValueAndLock(id, name)
return []string{v}, lock, ok
case FTComboBox:
v, lock, ok := f.comboBoxValueAndLock(id, name)
return []string{v}, lock, ok
case FTListBox:
return f.listBoxValuesAndLock(id, name)
case FTDate:
v, lock, ok := f.dateFieldValueAndLock(id, name)
return []string{v}, lock, ok
case FTText:
v, lock, ok := f.textFieldValueAndLock(id, name)
return []string{v}, lock, ok
}
return nil, false, false
}
}
func fillRadioButtons(ctx *model.Context, d types.Dict, vNew string, v types.Name) error {
for _, o := range d.ArrayEntry("Kids") {
d, err := ctx.DereferenceDict(o)
if err != nil {
return err
}
d1 := d.DictEntry("AP")
if d1 == nil {
return errors.New("pdfcpu: corrupt form field: missing entry AP")
}
d2 := d1.DictEntry("N")
if d2 == nil {
return errors.New("pdfcpu: corrupt AP field: missing entry N")
}
for k := range d2 {
k, err := types.DecodeName(k)
if err != nil {
return err
}
if k != "Off" {
d["AS"] = types.Name("Off")
if k == vNew {
d["AS"] = v
}
break
}
}
}
return nil
}
func fillRadioButtonGroup(
ctx *model.Context,
d types.Dict,
id, name string,
locked bool,
format DataFormat,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ok *bool) error {
vv, lock, found := fillDetails(id, name, FTRadioButtonGroup, format)
if !found {
return nil
}
if locked {
if !lock {
unlockFormField(d)
*ok = true
}
} else {
if lock {
lockFormField(d)
*ok = true
}
}
vNew := vv[0]
vOld := ""
if s := d.NameEntry("V"); s != nil {
n, err := types.DecodeName(*s)
if err != nil {
return err
}
if n != "Off" {
vOld = n
}
}
if vNew == vOld {
return nil
}
s := types.EncodeName(vNew)
v := types.Name(s)
d["V"] = v
if err := fillRadioButtons(ctx, d, vNew, v); err != nil {
return err
}
*ok = true
return nil
}
func fillCheckBox(
ctx *model.Context,
d types.Dict,
id, name string,
locked bool,
format DataFormat,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ok *bool) error {
vv, lock, found := fillDetails(id, name, FTCheckBox, format)
if !found {
return nil
}
if locked {
if !lock {
unlockFormField(d)
*ok = true
}
} else {
if lock {
lockFormField(d)
*ok = true
}
}
s := strings.ToLower(vv[0])
vNew := strings.HasPrefix(s, "t")
vOld := false
if o, found := d.Find("V"); found {
vOld = o.(types.Name) == "Yes"
}
if vNew == vOld {
return nil
}
v := types.Name("Off")
if vNew {
v = types.Name("Yes")
}
d["V"] = v
if _, found := d.Find("AS"); found {
offName, yesName := primitives.CalcCheckBoxASNames(d)
//fmt.Printf("off:<%s> yes:<%s>\n", offName, yesName)
asName := yesName
if v == "Off" {
asName = offName
}
d["AS"] = asName
}
*ok = true
return nil
}
func fillBtn(
ctx *model.Context,
d types.Dict,
id, name string,
locked bool,
format DataFormat,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ok *bool) error {
ff := d.IntEntry("Ff")
if ff != nil && primitives.FieldFlags(*ff)&primitives.FieldPushbutton > 0 {
return nil
}
if len(d.ArrayEntry("Kids")) > 0 {
if err := fillRadioButtonGroup(ctx, d, id, name, locked, format, fillDetails, ok); err != nil {
return err
}
} else {
if err := fillCheckBox(ctx, d, id, name, locked, format, fillDetails, ok); err != nil {
return err
}
}
return nil
}
func fillComboBox(
ctx *model.Context,
d types.Dict,
id, name string,
opts []string,
locked bool,
format DataFormat,
fonts map[string]types.IndirectRef,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ff *int,
ok *bool) error {
vv, lock, found := fillDetails(id, name, FTComboBox, format)
if !found {
return nil
}
vNew := vv[0]
if locked {
if !lock {
unlockFormField(d)
d.Delete("AP")
*ok = true
}
} else if lock {
lockFormField(d)
if err := primitives.EnsureComboBoxAP(ctx, d, vNew, fonts); err != nil {
return err
}
*ok = true
}
vOld := ""
if sl := d.StringLiteralEntry("V"); sl != nil {
s, err := types.StringLiteralToString(*sl)
if err != nil {
return err
}
vOld = s
}
if vNew == vOld {
return nil
}
s, err := types.EscapeUTF16String(vNew)
if err != nil {
return err
}
ind := types.Array{}
for i, o := range opts {
if o == vNew {
ind = append(ind, types.Integer(i))
break
}
}
if len(ind) > 0 {
d["I"] = ind
d["V"] = types.StringLiteral(*s)
} else {
d.Delete("I")
d.Delete("V")
}
*ok = true
return nil
}
func updateListBoxValues(multi bool, d types.Dict, opts, vNew []string) (types.Array, error) {
ind := types.Array{}
if multi {
arr := types.Array{}
for _, v := range vNew {
for i, o := range opts {
if o == v {
ind = append(ind, types.Integer(i))
break
}
}
s, err := types.EscapeUTF16String(v)
if err != nil {
return nil, err
}
arr = append(arr, types.StringLiteral(*s))
}
if len(vNew) > 0 {
d["I"] = ind
d["V"] = arr
} else {
d.Delete("I")
d.Delete("V")
}
return ind, nil
}
v := vNew[0]
s, err := types.EscapeUTF16String(v)
if err != nil {
return nil, err
}
for i, o := range opts {
if o == v {
ind = append(ind, types.Integer(i))
break
}
}
if len(ind) > 0 {
d["I"] = ind
d["V"] = types.StringLiteral(*s)
} else {
d.Delete("I")
d.Delete("V")
}
return ind, nil
}
func fillListBox(
ctx *model.Context,
d types.Dict,
id, name string,
opts []string,
locked bool,
format DataFormat,
fonts map[string]types.IndirectRef,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ff *int,
ok *bool) error {
vNew, lock, found := fillDetails(id, name, FTListBox, format)
if !found {
return nil
}
var vOld []string
multi := primitives.FieldFlags(*ff)&primitives.FieldMultiselect > 0
if !multi {
if sl := d.StringLiteralEntry("V"); sl != nil {
s, err := types.StringLiteralToString(*sl)
if err != nil {
return err
}
vOld = []string{s}
}
} else {
ss, err := parseStringLiteralArray(ctx.XRefTable, d, "V")
if err != nil {
return err
}
vOld = ss
}
if locked {
if !lock {
unlockFormField(d)
*ok = true
}
return nil
}
if lock {
lockFormField(d)
*ok = true
}
if types.EqualSlices(vOld, vNew) {
return nil
}
ind, err := updateListBoxValues(multi, d, opts, vNew)
if err != nil {
return err
}
if err := primitives.EnsureListBoxAP(ctx, d, opts, ind, fonts); err != nil {
return err
}
*ok = true
return nil
}
func fillCh(
ctx *model.Context,
d types.Dict,
id, name string,
locked bool,
format DataFormat,
fonts map[string]types.IndirectRef,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ff *int,
ok *bool) error {
if ff == nil {
return errors.New("pdfcpu: corrupt form field: missing entry Ff")
}
opts, err := parseOptions(ctx.XRefTable, d)
if err != nil {
return err
}
if len(opts) == 0 {
return errors.New("pdfcpu: missing Opts")
}
if primitives.FieldFlags(*ff)&primitives.FieldCombo > 0 {
return fillComboBox(ctx, d, id, name, opts, locked, format, fonts, fillDetails, ff, ok)
}
return fillListBox(ctx, d, id, name, opts, locked, format, fonts, fillDetails, ff, ok)
}
func fillDateField(
ctx *model.Context,
d types.Dict,
id, name, vOld string,
locked bool,
format DataFormat,
fonts map[string]types.IndirectRef,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ff *int,
ok *bool) error {
vv, lock, found := fillDetails(id, name, FTDate, format)
if !found {
return nil
}
if locked {
if !lock {
unlockFormField(d)
*ok = true
}
} else {
if lock {
lockFormField(d)
*ok = true
}
}
vNew := vv[0]
if vNew == vOld {
return nil
}
s, err := types.EscapeUTF16String(vNew)
if err != nil {
return err
}
d["V"] = types.StringLiteral(*s)
if err := primitives.EnsureDateFieldAP(ctx, d, vNew, fonts); err != nil {
return err
}
*ok = true
return nil
}
func fillTextField(
ctx *model.Context,
d types.Dict,
id, name, vOld string,
locked bool,
format DataFormat,
fonts map[string]types.IndirectRef,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ff *int,
ok *bool) error {
vv, lock, found := fillDetails(id, name, FTText, format)
if !found {
return nil
}
if locked {
if !lock {
unlockFormField(d)
*ok = true
}
} else {
if lock {
lockFormField(d)
*ok = true
}
}
vNew := vv[0]
if vNew == vOld {
return nil
}
s, err := types.EscapeUTF16String(vNew)
if err != nil {
return err
}
d["V"] = types.StringLiteral(*s)
multiLine := ff != nil && uint(primitives.FieldFlags(*ff))&uint(primitives.FieldMultiline) > 0
kids := d.ArrayEntry("Kids")
if len(kids) > 0 {
for _, o := range kids {
d, err := ctx.DereferenceDict(o)
if err != nil {
return err
}
if err := primitives.EnsureTextFieldAP(ctx, d, vNew, multiLine, fonts); err != nil {
return err
}
*ok = true
}
return nil
}
if err := primitives.EnsureTextFieldAP(ctx, d, vNew, multiLine, fonts); err != nil {
return err
}
*ok = true
return nil
}
func fillTx(
ctx *model.Context,
d types.Dict,
id, name string,
locked bool,
format DataFormat,
fonts map[string]types.IndirectRef,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ff *int,
ok *bool) error {
df, err := extractDateFormat(ctx.XRefTable, d)
if err != nil {
return err
}
vOld := ""
if o, found := d.Find("V"); found {
sl, _ := o.(types.StringLiteral)
s, err := types.StringLiteralToString(sl)
if err != nil {
return err
}
vOld = s
}
if df != nil {
return fillDateField(ctx, d, id, name, vOld, locked, format, fonts, fillDetails, ff, ok)
}
return fillTextField(ctx, d, id, name, vOld, locked, format, fonts, fillDetails, ff, ok)
}
func fillWidgetAnnots(
ctx *model.Context,
fields types.Array,
indRefs map[types.IndirectRef]bool,
wAnnots model.Annot,
format DataFormat,
fonts map[string]types.IndirectRef,
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ok *bool) error {
for _, indRef := range *(wAnnots.IndRefs) {