|
| 1 | +// This example demonstrates how to create a PDF with form fields |
| 2 | +// using the UniPDF library. The PDF will contain multiple text fields |
| 3 | +// along with submit and reset buttons, and the form fields will be |
| 4 | +// properly tagged in the document structure tree for accessibility compliance. |
| 5 | +// |
| 6 | +// The example covers best practices for PDF/UA compliance: |
| 7 | +// 1. Each form field has an associated label with a tooltip. |
| 8 | +// 2. The document structure tree is constructed with K dictionaries |
| 9 | +// to represent the hierarchical structure of the form elements. |
| 10 | +// 3. Each label is associated with its corresponding form field using |
| 11 | +// marked content IDs (MCID). |
| 12 | +// 4. The submit button is configured to submit the form data to a specified URL. |
| 13 | +// 5. The reset button is configured to reset the specified fields to their default values. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "log" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/unidoc/unipdf/v4/annotator" |
| 22 | + "github.com/unidoc/unipdf/v4/common/license" |
| 23 | + "github.com/unidoc/unipdf/v4/contentstream/draw" |
| 24 | + "github.com/unidoc/unipdf/v4/core" |
| 25 | + "github.com/unidoc/unipdf/v4/creator" |
| 26 | + "github.com/unidoc/unipdf/v4/model" |
| 27 | +) |
| 28 | + |
| 29 | +func init() { |
| 30 | + // Make sure to load your metered License API key prior to using the library. |
| 31 | + // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io |
| 32 | + err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) |
| 33 | + if err != nil { |
| 34 | + panic(err) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func main() { |
| 39 | + textFieldsDef := []struct { |
| 40 | + Label string |
| 41 | + Name string |
| 42 | + Rect []float64 |
| 43 | + Tooltip string |
| 44 | + }{ |
| 45 | + {Label: "Full Name", Name: "full_name", Rect: []float64{123.97, 619.02, 343.99, 633.6}, Tooltip: "Enter your full name"}, |
| 46 | + {Label: "Address 1", Name: "address_line_1", Rect: []float64{123.97, 596.82, 343.99, 611.4}, Tooltip: "Enter your primary address"}, |
| 47 | + {Label: "Address 2", Name: "address_line_2", Rect: []float64{123.97, 574.28, 343.99, 588.86}, Tooltip: "Enter your secondary address (optional)"}, |
| 48 | + } |
| 49 | + |
| 50 | + c := creator.New() |
| 51 | + page := c.NewPage() |
| 52 | + _, pageHeight, err := page.Size() |
| 53 | + if err != nil { |
| 54 | + log.Fatal(err) |
| 55 | + } |
| 56 | + |
| 57 | + // Construct the StructTreeRoot. |
| 58 | + str := model.NewStructTreeRoot() |
| 59 | + |
| 60 | + // Construct base K dictionary. |
| 61 | + docK := model.NewKDictionary() |
| 62 | + docK.S = core.MakeName(string(model.StructureTypeDocument)) |
| 63 | + |
| 64 | + str.AddKDict(docK) |
| 65 | + |
| 66 | + form := model.NewPdfAcroForm() |
| 67 | + fields := core.MakeArray() |
| 68 | + |
| 69 | + // Create text fields and its label |
| 70 | + for idx, fdef := range textFieldsDef { |
| 71 | + opt := annotator.TextFieldOptions{} |
| 72 | + textf, err := annotator.NewTextField(page, fdef.Name, fdef.Rect, opt) |
| 73 | + if err != nil { |
| 74 | + log.Fatal(err) |
| 75 | + } |
| 76 | + |
| 77 | + textf.DV = core.MakeString("") // Set default value for the field. |
| 78 | + textf.V = core.MakeString("") // Set current value for the field. |
| 79 | + textf.TU = core.MakeString(fdef.Tooltip) // Set tooltip for the field. |
| 80 | + |
| 81 | + *form.Fields = append(*form.Fields, textf.PdfField) |
| 82 | + page.AddAnnotation(textf.Annotations[0].PdfAnnotation) |
| 83 | + |
| 84 | + y := pageHeight - fdef.Rect[1] |
| 85 | + |
| 86 | + p := c.NewStyledParagraph() |
| 87 | + p.SetText(fdef.Label) |
| 88 | + p.SetPos(fdef.Rect[0]-80, y-10) |
| 89 | + |
| 90 | + // Tag the label paragraph and generate its K dictionary. |
| 91 | + // This will be used to associate the label with the form field. |
| 92 | + p.SetStructureType(model.StructureTypeForm) |
| 93 | + |
| 94 | + // Set unique ID for each field label. |
| 95 | + p.SetMarkedContentID(int64(idx)) |
| 96 | + |
| 97 | + k, err := p.GenerateKDict() |
| 98 | + if err != nil { |
| 99 | + log.Fatalf("Error: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + k.Alt = core.MakeString(fdef.Tooltip) // Set alternative text for the label. |
| 103 | + |
| 104 | + docK.AddKChild(k) |
| 105 | + |
| 106 | + err = c.Draw(p) |
| 107 | + if err != nil { |
| 108 | + log.Fatal(err) |
| 109 | + } |
| 110 | + |
| 111 | + line := c.NewLine(fdef.Rect[0], y, fdef.Rect[2], y) |
| 112 | + err = c.Draw(line) |
| 113 | + if err != nil { |
| 114 | + log.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + fields.Append(textf.ToPdfObject()) |
| 118 | + } |
| 119 | + |
| 120 | + err = addSubmitButton(page, form) |
| 121 | + if err != nil { |
| 122 | + log.Fatal(err) |
| 123 | + } |
| 124 | + |
| 125 | + err = addResetButton(page, form, fields) |
| 126 | + if err != nil { |
| 127 | + log.Fatal(err) |
| 128 | + } |
| 129 | + |
| 130 | + c.SetForms(form) |
| 131 | + c.SetStructTreeRoot(str) |
| 132 | + |
| 133 | + err = c.WriteToFile("pdf_tag_form.pdf") |
| 134 | + if err != nil { |
| 135 | + log.Fatal(err) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// Add Submit button that will submit all field values. |
| 140 | +func addSubmitButton(page *model.PdfPage, form *model.PdfAcroForm) error { |
| 141 | + optSubmit := annotator.FormSubmitActionOptions{ |
| 142 | + Url: "https://unidoc.io", |
| 143 | + Rectangle: draw.Rectangle{ |
| 144 | + X: 400.0, |
| 145 | + Y: 400.0, |
| 146 | + Width: 50.0, |
| 147 | + Height: 20.0, |
| 148 | + FillColor: model.NewPdfColorDeviceRGB(0.0, 1.0, 0.0), |
| 149 | + }, |
| 150 | + Label: "Submit", |
| 151 | + LabelColor: model.NewPdfColorDeviceRGB(1.0, 0.0, 0.0), |
| 152 | + } |
| 153 | + |
| 154 | + btnSubmitField, err := annotator.NewFormSubmitButtonField(page, optSubmit) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + |
| 159 | + *form.Fields = append(*form.Fields, btnSubmitField.PdfField) |
| 160 | + page.AddAnnotation(btnSubmitField.Annotations[0].PdfAnnotation) |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +// Add Reset button that would reset the specified fields to its default value. |
| 166 | +func addResetButton(page *model.PdfPage, form *model.PdfAcroForm, fields *core.PdfObjectArray) error { |
| 167 | + optReset := annotator.FormResetActionOptions{ |
| 168 | + Rectangle: draw.Rectangle{ |
| 169 | + X: 100.0, |
| 170 | + Y: 400.0, |
| 171 | + Width: 50.0, |
| 172 | + Height: 20.0, |
| 173 | + FillColor: model.NewPdfColorDeviceGray(0.5), |
| 174 | + }, |
| 175 | + Label: "Reset", |
| 176 | + LabelColor: model.NewPdfColorDeviceGray(1.0), |
| 177 | + Fields: fields, |
| 178 | + } |
| 179 | + |
| 180 | + btnResetField, err := annotator.NewFormResetButtonField(page, optReset) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + |
| 185 | + // Add widget to existing form. |
| 186 | + *form.Fields = append(*form.Fields, btnResetField.PdfField) |
| 187 | + page.AddAnnotation(btnResetField.Annotations[0].PdfAnnotation) |
| 188 | + |
| 189 | + return nil |
| 190 | +} |
0 commit comments