Repository navigation
Expand file tree
/
Copy pathforms.go
More file actions
69 lines (57 loc) · 1.55 KB
/
Copy pathforms.go
File metadata and controls
69 lines (57 loc) · 1.55 KB
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
package pdfsign
import (
"fmt"
"github.com/digitorus/pdf"
"github.com/digitorus/pdfsign/forms"
"github.com/digitorus/pdfsign/internal/acroform"
)
// FormFields returns all form fields in the document.
func (d *Document) FormFields() []FormField {
return forms.Extract(d.rdr)
}
// SetField sets the value of a form field.
func (d *Document) SetField(name string, value any) error {
// Staging field updates to be applied during Write()
if d.pendingFields == nil {
d.pendingFields = make(map[string]any)
}
d.pendingFields[name] = value
return nil
}
// SetFields sets multiple form field values.
func (d *Document) SetFields(fields map[string]any) error {
for name, value := range fields {
if err := d.SetField(name, value); err != nil {
return err
}
}
return nil
}
// applyPendingFields resolves pending field updates to object IDs and generated content.
func (d *Document) applyPendingFields() (map[uint32][]byte, error) {
if len(d.pendingFields) == 0 {
return nil, nil
}
updates := make(map[uint32][]byte)
// Map field names to their PDF values
fieldMap := make(map[string]pdf.Value)
acroform.Fields(d.rdr.Trailer().Key("Root"), func(f acroform.Field) bool {
if f.Type != "" {
fieldMap[f.Name] = f.Dict
}
return true
})
for name, value := range d.pendingFields {
v, ok := fieldMap[name]
if !ok {
return nil, fmt.Errorf("field %s not found in document", name)
}
update, err := forms.GenerateUpdate(v, value)
if err != nil {
return nil, err
}
ptr := v.GetPtr()
updates[ptr.GetID()] = update
}
return updates, nil
}