import { useEffect, useRef } from 'react'; import { Viewer, DocumentGate } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { interactionPlugin, useInteraction, useInteractionState } from '@embedpdf/react/interaction'; import { AnnotationLayer, annotationPlugin } from '@embedpdf/react/annotation'; import { FormLayer, formPlugin, useForm, useFormState } from '@embedpdf/react/form'; import { localEngine } from '@embedpdf/engine'; import './designer.css'; const engine = localEngine(); // Building forms needs the annotation plugin: in design mode, fields are boxes like any annotation. const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin(), formPlugin(), ]; const registration = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/registration.pdf'); return { kind: 'bytes', id: 'registration', bytes: new Uint8Array(await response.arrayBuffer()) }; }; const FIELD_TOOLS = [ ['form-text', 'Text'], ['form-checkbox', 'Checkbox'], ['form-radio', 'Radio button'], ['form-combobox', 'Dropdown'], ['form-listbox', 'List'], ['form-signature', 'Signature'], ] as const; /** Start with the text tool picked: the printed form has boxes, but no fields yet. */ function useStartDesigning() { const interaction = useInteraction(); const started = useRef(false); useEffect(() => { if (started.current) return; started.current = true; interaction.activateTool('form-text'); }, [interaction]); } function FieldPalette() { useStartDesigning(); const interaction = useInteraction(); const form = useForm(); const { activeToolId } = useInteractionState(); const { fields } = useFormState(); // The pointer fills the form in; every other tool here designs it. const filling = activeToolId === 'pointer'; if (!form.canDesign()) return

This document’s form can’t be changed.

; return (
{FIELD_TOOLS.map(([id, label]) => ( ))} {fields.length} fields: click the page to place another
); } export default function App() { return ( Loading…

}> {() => ( <> )}
); }