pdf_form_list_fields.go hosted by Oembed Proxy for GitHub
  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
/*
 * Lists form fields in a PDF file.
 *
 * Run as: go run pdf_form_list_fields.go input.pdf
 */

package main

import (
	"errors"
	"fmt"
	"os"

	"github.com/unidoc/unipdf/v3/common/license"
	"github.com/unidoc/unipdf/v3/core"
	"github.com/unidoc/unipdf/v3/model"
)

func init() {
	// Make sure to load your metered License API key prior to using the library.
	// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
	err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
	if err != nil {
		panic(err)
	}
}

func main() {
	if len(os.Args) < 2 {
		fmt.Printf("Usage: go run pdf_forms_list_fields.go input.pdf [input2.pdf] ...\n")
		os.Exit(1)
	}

	for _, inputPath := range os.Args[1:len(os.Args)] {
		err := listFormFields(inputPath)
		if err != nil {
			fmt.Printf("Error: %v\n", err)
		}
	}
}

func listFormFields(inputPath string) error {
	f, err := os.Open(inputPath)
	if err != nil {
		return err
	}
	fmt.Printf("Input file: %s\n", inputPath)

	defer f.Close()

	pdfReader, err := model.NewPdfReader(f)
	if err != nil {
		return err
	}

	acroForm := pdfReader.AcroForm
	if acroForm == nil {
		fmt.Printf(" No formdata present\n")
		return nil
	}

	fmt.Printf(" AcroForm (%p)\n", acroForm)
	fmt.Printf(" NeedAppearances: %v\n", acroForm.NeedAppearances)
	fmt.Printf(" SigFlags: %v\n", acroForm.SigFlags)
	fmt.Printf(" CO: %v\n", acroForm.CO)
	fmt.Printf(" DR: %v\n", acroForm.DR)
	fmt.Printf(" DA: %v\n", acroForm.DA)
	fmt.Printf(" Q: %v\n", acroForm.Q)
	fmt.Printf(" XFA: %v\n", acroForm.XFA)
	if acroForm.Fields != nil {
		fmt.Printf(" #Fields: %d\n", len(acroForm.AllFields()))
	} else {
		fmt.Printf("No fields set\n")
	}
	fmt.Printf(" =====\n")

	fields := acroForm.AllFields()

	for idx, field := range fields {
		fmt.Printf("=====\n")
		fmt.Printf("Field %d\n", idx+1)
		if !field.IsTerminal() {
			fmt.Printf("- Skipping over non-terminal field\n")
			continue
		}

		fullname, err := field.FullName()
		if err != nil {
			fmt.Printf("Error: %v\n", err)
		}
		fflags := field.Flags()
		fmt.Printf("Name: %v\n", fullname)
		fmt.Printf(" Flags: %s (%d)\n", fflags, fflags)

		ctx := field.GetContext()
		switch t := ctx.(type) {
		case *model.PdfFieldButton:
			fmt.Printf(" Button\n")
			if t.IsCheckbox() {
				fmt.Printf(" - Checkbox\n")
			}
			if t.IsPush() {
				fmt.Printf(" - Push\n")
			}
			if t.IsRadio() {
				fmt.Printf(" - Radio\n")
			}
			fmt.Printf(" - '%v'\n", t.V)

		case *model.PdfFieldText:
			fmt.Printf(" Text\n")
			fmt.Printf(" - DA: %v\n", getDictEntry(field, "DA"))
			fmt.Printf(" - '%v'\n", t.V)
			if str, ok := core.GetString(t.V); ok {
				fmt.Printf(" - Decoded: '%s'\n", str.Decoded())
			}

		case *model.PdfFieldChoice:
			fmt.Printf(" Choice\n")
			fmt.Printf(" - '%v'\n", t.V)

		case *model.PdfFieldSignature:
			fmt.Printf(" Signature\n")
			fmt.Printf(" - '%v'\n", t.V)

		default:
			fmt.Printf(" Unknown\n")
			continue
		}

		fmt.Printf(" Annotations: %d\n", len(field.Annotations))
		for j, wa := range field.Annotations {
			fmt.Printf(" - Annotation %d \n", j+1)

			// Note: The P field is optional
			if wa.P != nil {
				pageind, ok := wa.P.(*core.PdfIndirectObject)
				if !ok {
					fmt.Printf("not indirect, got: %v\n", pageind)
					fmt.Printf("%")
					return errors.New("Type check error")
				}
				_, pagenum, err := pdfReader.PageFromIndirectObject(pageind)
				if err != nil {
					return err
				}
				fmt.Printf(" - Page number: %d\n", pagenum)
			} else {
				// If P not set, go through pages and look for match.
				// TODO: Make a map of widget annotations to page numbers a priori.
				for pageidx, page := range pdfReader.PageList {
					annotations, err := page.GetAnnotations()
					if err != nil {
						return err
					}
					for _, annot := range annotations {
						switch t := annot.GetContext().(type) {
						case *model.PdfAnnotationWidget:
							if wa == t {
								fmt.Printf(" - Page number: %d\n", pageidx+1)
							}
						}
					}
				}
			}
			fmt.Printf(" - Rect: %+v\n", wa.Rect)
			fmt.Printf(" - wa.AS: %v\n", wa.AS)
			fmt.Printf(" - wa.AP: %v\n", wa.AP)
			fmt.Printf(" - wa.F:  %v\n", wa.F)

			// Example of how to fetch the appearance stream data.
			if apDict, has := core.GetDict(wa.AP); has {
				n, has := core.GetStream(apDict.Get("N"))
				if has {
					decoded, err := core.DecodeStream(n)
					if err != nil {
						fmt.Printf("Decoding error: %v\n", err)
						return err
					}
					fmt.Printf("   - N: '%s'\n", decoded)
				} else {
					fmt.Printf("   - N not set\n")
				}

				if d, has := core.GetDict(apDict.Get("D")); has {
					appKey := core.MakeName("Off")
					if appname, has := core.GetName(wa.AS); has {
						appKey = appname
					}

					fmt.Printf("   - D dict: % s\n", d.Keys())
					fmt.Printf("   - App Key: '%s'\n", *appKey)
					if radioApp, has := core.GetStream(d.Get(*appKey)); has {
						decoded, err := core.DecodeStream(radioApp)
						if err != nil {
							fmt.Printf("  - Decoding error: %v\n", err)
							return err
						}

						fmt.Printf("   - Radio appearance: '%s'\n", decoded)
					}

				}

			} else {
				fmt.Printf("   - Appearance dict not present: %s\n", apDict)
			}
		}
	}

	return nil
}

// Returns the object corresponding with the specified
// key in the field or from the field's parent.
// Return nil if not found.
func getDictEntry(f *model.PdfField, key core.PdfObjectName) core.PdfObject {
	for _, fc := range []*model.PdfField{f, f.Parent} {
		if fc == nil {
			continue
		}
		if d, ok := core.GetDict(fc.GetContainingPdfObject()); ok {
			if val := d.Get(key); val != nil {
				return val
			}
		}
	}

	return nil
}