useWidgetsCreate
Returns a function that creates form widgets programmatically and resolves with their snapshots. To let users place widgets by clicking the page instead, switch the interaction tool to { type: 'addWidget', widgetProps } (see useSetInteractionMode).
const createWidgets = useWidgetsCreate()
const widgets = await createWidgets(props)
const widgets = await createWidgets([props1, props2])Each props object requires:
| Prop | Meaning |
|---|---|
pageId | The page to create the widget on. |
widgetType | 'text', 'choice', 'multiChoice', 'checkbox', 'radio', 'pushButton', or 'signature'. |
x, y, width, height | The widget box in user-space units (the origin is at the bottom-left corner of the page). |
Everything else is optional and falls back to defaults, including a generated unique fieldName. The available properties per kind are listed in useSetWidgetProps.
Behavior
- To make a radio group, create several
radiowidgets with the samefieldName. - The snapshots come in the order of the input. Their ids are session-scoped: don't store them across page reloads.
- The function is asynchronous because the target page may not be loaded yet.
- An unknown
pageIdthrowsInvalidInputError; with no document loaded, the call throwsDocumentNotLoadedError.
Example
import { useWidgetsCreate } from '@sdk-pdf/react'
function useAddNameField() {
const createWidgets = useWidgetsCreate()
return async (pageId: string) => {
const [widget] = await createWidgets({
pageId,
widgetType: 'text',
x: 72,
y: 700,
width: 180,
height: 24,
fieldName: 'customer.name',
})
return widget
}
}