|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Create form fields in the TypeScript PDF Viewer | Syncfusion |
| 4 | +description: Learn how to add each PDF form field using the PDF Viewer UI and how to create them programmatically in the Syncfusion TypeScript PDF Viewer. |
| 5 | +platform: document-processing |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Create form fields in TypeScript PDF Viewer |
| 11 | + |
| 12 | +The PDF Viewer component supports interactive form field design, including drawing, dragging, and resizing fields directly on the page. Click the Form Field icon on the toolbar to add a field and place it on the document. You can also create and manage form fields programmatically using the API. |
| 13 | + |
| 14 | +The PDF Viewer supports the following form field types: |
| 15 | + |
| 16 | +- Textbox |
| 17 | +- Password |
| 18 | +- CheckBox |
| 19 | +- RadioButton |
| 20 | +- ListBox |
| 21 | +- DropDown |
| 22 | +- Signature field |
| 23 | +- Initial field |
| 24 | + |
| 25 | +## Add the form field dynamically |
| 26 | + |
| 27 | +Click the Form Field icon on the toolbar, then click on the PDF to draw a form field. See the following GIF for reference. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Drag the form field |
| 32 | + |
| 33 | +Drag the selected form field to reposition it within the PDF document. See the following GIF for reference. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +## Resize the form field |
| 38 | + |
| 39 | +Resize the selected form field using the resize handles on the field boundary. See the following GIF for reference. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +## Textbox |
| 44 | + |
| 45 | +### Add Textbox |
| 46 | + |
| 47 | +1) Open the Form Designer toolbar. |
| 48 | +2) Select Textbox, then click/tap on the page to place it. |
| 49 | +3) Resize/move as needed and set properties in the property panel. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +### Add Textbox Programmatically |
| 54 | + |
| 55 | +To add a Textbox programmatically, call addFormField with type 'Textbox' and pass as `TextFieldSettings` object. The example below adds a textbox when the document loads. |
| 56 | + |
| 57 | +```ts |
| 58 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, TextFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 59 | + |
| 60 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 61 | + |
| 62 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 63 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 64 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 65 | + |
| 66 | +pdfviewer.appendTo('#PdfViewer'); |
| 67 | + |
| 68 | +pdfviewer.documentLoad = () => { |
| 69 | + pdfviewer.formDesignerModule.addFormField('Textbox', { |
| 70 | + name: 'First Name', |
| 71 | + bounds: { X: 146, Y: 229, Width: 150, Height: 24 } |
| 72 | + } as TextFieldSettings); |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +## Password |
| 77 | + |
| 78 | +### Add Password |
| 79 | + |
| 80 | +1) Open the Form Designer toolbar. |
| 81 | +2) Select Password, then place it on the page. |
| 82 | +3) Configure tooltip, required, max length, etc. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +### Add Password Programmatically |
| 87 | + |
| 88 | +To add a Password field programmatically, call addFormField with type 'Password' and pass as `PasswordFieldSettings` object. The example below adds the field when the document loads. |
| 89 | + |
| 90 | +```ts |
| 91 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PasswordFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 92 | + |
| 93 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 94 | + |
| 95 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 96 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 97 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 98 | + |
| 99 | +pdfviewer.appendTo('#PdfViewer'); |
| 100 | + |
| 101 | +pdfviewer.documentLoad = () => { |
| 102 | + pdfviewer.formDesignerModule.addFormField('Password', { |
| 103 | + name: 'Account Password', |
| 104 | + bounds: { X: 148, Y: 270, Width: 180, Height: 24 } |
| 105 | + } as PasswordFieldSettings); |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +## CheckBox |
| 110 | + |
| 111 | +### Add CheckBox |
| 112 | + |
| 113 | +1) Choose CheckBox in the Form Designer toolbar. |
| 114 | +2) Click on the page to place, duplicate for multiple options if needed. |
| 115 | +3) Use property panel to set IsChecked, tooltip, and appearance. |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +### Add CheckBox Programmatically |
| 120 | + |
| 121 | +To add a CheckBox programmatically, call `addFormField` with type 'CheckBox' and pass as `CheckBoxFieldSettings` object. Set isChecked and bounds as needed. The example below adds the field when the document loads. |
| 122 | + |
| 123 | +```ts |
| 124 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, CheckBoxFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 125 | + |
| 126 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 127 | + |
| 128 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 129 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 130 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 131 | + |
| 132 | +pdfviewer.appendTo('#PdfViewer'); |
| 133 | + |
| 134 | +pdfviewer.documentLoad = () => { |
| 135 | + pdfviewer.formDesignerModule.addFormField('CheckBox', { |
| 136 | + name: 'Subscribe', |
| 137 | + isChecked: false, |
| 138 | + bounds: { X: 56, Y: 664, Width: 20, Height: 20 } |
| 139 | + } as CheckBoxFieldSettings); |
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | +## RadioButton |
| 144 | + |
| 145 | +### Add RadioButton |
| 146 | + |
| 147 | +1) Select RadioButton in the Form Designer toolbar. |
| 148 | +2) Place buttons sharing the same Name to create a group (e.g., Gender). |
| 149 | +3) Use property panel to set selection, colors, and tooltip. |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +### Add RadioButton Programmatically |
| 154 | + |
| 155 | +To add radio buttons programmatically, call addFormField with type 'RadioButton' and pass as `RadioButtonFieldSettings` object. Use the same name to group buttons. The example below adds two radio buttons when the document loads. |
| 156 | + |
| 157 | +```ts |
| 158 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, RadioButtonFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 159 | + |
| 160 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 161 | + |
| 162 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 163 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 164 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 165 | + |
| 166 | +pdfviewer.appendTo('#PdfViewer'); |
| 167 | + |
| 168 | +pdfviewer.documentLoad = () => { |
| 169 | + // Group by name: 'Gender' |
| 170 | + pdfviewer.formDesignerModule.addFormField('RadioButton', { |
| 171 | + name: 'Gender', |
| 172 | + isSelected: false, |
| 173 | + bounds: { X: 148, Y: 289, Width: 18, Height: 18 } |
| 174 | + } as RadioButtonFieldSettings); |
| 175 | + |
| 176 | + pdfviewer.formDesignerModule.addFormField('RadioButton', { |
| 177 | + name: 'Gender', |
| 178 | + isSelected: false, |
| 179 | + bounds: { X: 292, Y: 289, Width: 18, Height: 18 } |
| 180 | + } as RadioButtonFieldSettings); |
| 181 | +}; |
| 182 | +``` |
| 183 | + |
| 184 | +## ListBox |
| 185 | + |
| 186 | +### Add ListBox |
| 187 | + |
| 188 | +1) Choose ListBox in the Form Designer toolbar. |
| 189 | +2) Place the field and add items in the property panel. |
| 190 | +3) Configure font, size, and selection behavior. |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | +### Add ListBox Programmatically |
| 195 | + |
| 196 | +To add a ListBox programmatically, call `addFormField` with type 'ListBox' and pass as `ListBoxFieldSettings` object, including an options array for the items. The example below adds the field when the document loads. |
| 197 | + |
| 198 | +```ts |
| 199 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, ListBoxFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 200 | + |
| 201 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 202 | + |
| 203 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 204 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 205 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 206 | + |
| 207 | +pdfviewer.appendTo('#PdfViewer'); |
| 208 | + |
| 209 | +pdfviewer.documentLoad = () => { |
| 210 | + const options = [ |
| 211 | + { itemName: 'Item 1', itemValue: 'item1' }, |
| 212 | + { itemName: 'Item 2', itemValue: 'item2' }, |
| 213 | + { itemName: 'Item 3', itemValue: 'item3' } |
| 214 | + ]; |
| 215 | + |
| 216 | + pdfviewer.formDesignerModule.addFormField('ListBox', { |
| 217 | + name: 'States', |
| 218 | + options, |
| 219 | + bounds: { X: 380, Y: 320, Width: 150, Height: 60 } |
| 220 | + } as ListBoxFieldSettings); |
| 221 | +}; |
| 222 | +``` |
| 223 | + |
| 224 | +## DropDown |
| 225 | + |
| 226 | +### Add DropDown |
| 227 | + |
| 228 | +1) Select DropDown in the Form Designer toolbar. |
| 229 | +2) Place the field, then add items via the property panel. |
| 230 | +3) Adjust appearance and default value. |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | +### Add DropDown Programmatically |
| 235 | + |
| 236 | +To add a DropDown programmatically, call addFormField with type 'DropDown' and pass a `DropdownFieldSettings` object with an options array. The example below adds the field when the document loads. |
| 237 | + |
| 238 | +```ts |
| 239 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, DropdownFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 240 | + |
| 241 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 242 | + |
| 243 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 244 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 245 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 246 | + |
| 247 | +pdfviewer.appendTo('#PdfViewer'); |
| 248 | + |
| 249 | +pdfviewer.documentLoad = () => { |
| 250 | + const options = [ |
| 251 | + { itemName: 'Item 1', itemValue: 'item1' }, |
| 252 | + { itemName: 'Item 2', itemValue: 'item2' }, |
| 253 | + { itemName: 'Item 3', itemValue: 'item3' } |
| 254 | + ]; |
| 255 | + |
| 256 | + pdfviewer.formDesignerModule.addFormField('DropDown', { |
| 257 | + name: 'Country', |
| 258 | + options, |
| 259 | + bounds: { X: 560, Y: 320, Width: 150, Height: 24 } |
| 260 | + } as DropdownFieldSettings); |
| 261 | +}; |
| 262 | +``` |
| 263 | + |
| 264 | +## Signature field |
| 265 | + |
| 266 | +### Add Signature field |
| 267 | + |
| 268 | +1) Select Signature field in the Form Designer toolbar. |
| 269 | +2) Place the field where the signer should sign. |
| 270 | +3) Configure indicator text, thickness, tooltip, and required state. |
| 271 | + |
| 272 | + |
| 273 | + |
| 274 | +### Add Signature field Programmatically |
| 275 | + |
| 276 | +To add a Signature field programmatically, call addFormField with type 'SignatureField' and pass a `SignatureFieldSettings` object. The example below adds the field when the document loads. |
| 277 | + |
| 278 | +```ts |
| 279 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, SignatureFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 280 | + |
| 281 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 282 | + |
| 283 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 284 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 285 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 286 | + |
| 287 | +pdfviewer.appendTo('#PdfViewer'); |
| 288 | + |
| 289 | +pdfviewer.documentLoad = () => { |
| 290 | + pdfviewer.formDesignerModule.addFormField('SignatureField', { |
| 291 | + name: 'Sign', |
| 292 | + bounds: { X: 57, Y: 923, Width: 200, Height: 43 } |
| 293 | + } as SignatureFieldSettings); |
| 294 | +}; |
| 295 | +``` |
| 296 | + |
| 297 | +## Initial field |
| 298 | + |
| 299 | +### Add Initial field |
| 300 | + |
| 301 | +1) Select Initial field in the Form Designer toolbar. |
| 302 | +2) Place the field where initials are required. |
| 303 | +3) Configure indicator text, tooltip, and required state. |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | +### Add Initial field Programmatically |
| 308 | + |
| 309 | +To add an Initial field programmatically, call addFormField with type 'InitialField' and pass an `InitialFieldSettings` object. The example below adds the field when the document loads. |
| 310 | + |
| 311 | +```ts |
| 312 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, InitialFieldSettings } from '@syncfusion/ej2-pdfviewer'; |
| 313 | + |
| 314 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 315 | + |
| 316 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 317 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"; |
| 318 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 319 | + |
| 320 | +pdfviewer.appendTo('#PdfViewer'); |
| 321 | + |
| 322 | +pdfviewer.documentLoad = () => { |
| 323 | + pdfviewer.formDesignerModule.addFormField('InitialField', { |
| 324 | + name: 'Initial', |
| 325 | + bounds: { X: 148, Y: 466, Width: 200, Height: 43 } |
| 326 | + } as InitialFieldSettings); |
| 327 | +}; |
| 328 | +``` |
| 329 | + |
| 330 | +## setFormFieldMode programmatically |
| 331 | + |
| 332 | +The `setFormFieldMode` method enables adding a form field dynamically by specifying the field type. For example, the following adds a Password field when a button is clicked. |
| 333 | + |
| 334 | +```html |
| 335 | +<button id="addPasswordField">Add Password Field</button> |
| 336 | +``` |
| 337 | +```ts |
| 338 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, |
| 339 | + BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner} from '@syncfusion/ej2-pdfviewer'; |
| 340 | + |
| 341 | +PdfViewer.Inject( Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, |
| 342 | + BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
| 343 | + |
| 344 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 345 | +pdfviewer.documentPath = 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf'; |
| 346 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 347 | +pdfviewer.appendTo('#PdfViewer'); |
| 348 | + |
| 349 | +document.getElementById('addPasswordField').addEventListener('click', function () { |
| 350 | + pdfviewer.formDesignerModule.setFormFieldMode("Password"); |
| 351 | + //In setFormFieldModule-You can pass the required field to be added like Textbox, Checkbox etc., |
| 352 | +}); |
| 353 | +``` |
0 commit comments