import { useState } from 'react'; import { Viewer, DocumentGate, usePageList } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin, useStage } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { interactionPlugin } from '@embedpdf/react/interaction'; import { FormLayer, formPlugin, useForm } from '@embedpdf/react/form'; import type { FormCapability, FormField } from '@embedpdf/react/form'; import type { PageRef } from '@embedpdf/react/runtime'; import { localEngine } from '@embedpdf/engine'; import './create.css'; const engine = localEngine(); const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), 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()) }; }; // The printed boxes are the fields' look: a new field only sets its text size. const look = { fontSize: 11 }; // The printed table of group tickets: a row every 36 points from the top at 470, with a country // box at x 96 and a circle for each ticket at x 330 and 430. const rowTop = (row: number) => 470 + row * 36; /** A dropdown in a row's country box. */ function addDropdown(form: FormCapability, page: PageRef, row: number) { return form.create({ kind: 'combobox', name: `country_${row + 1}`, options: [ { label: 'Netherlands', value: 'NL' }, { label: 'Belgium', value: 'BE' }, { label: 'Germany', value: 'DE' }, ], widgets: [{ page, rect: { x: 96, y: rowTop(row), width: 160, height: 24 }, ...look }], }); } /** A radio group in a row's two circles: one field, a widget per circle, each with its value. */ function addRadioGroup(form: FormCapability, page: PageRef, row: number) { const y = rowTop(row) + 4; return form.create({ kind: 'radio', name: `ticket_${row + 1}`, widgets: [ { page, rect: { x: 330, y, width: 16, height: 16 }, exportValue: 'one-day', ...look }, { page, rect: { x: 430, y, width: 16, height: 16 }, exportValue: 'both-days', ...look }, ], }); } function BuildToolbar() { const form = useForm(); const stage = useStage(); const page = usePageList()[0]?.ref; // the printed form const [added, setAdded] = useState([]); // Each kind of field fills its column from the top: the next row that hasn't got one. const rowFor = (kind: FormField['kind']) => added.filter((field) => field.kind === kind).length; const add = async (addField: typeof addDropdown, kind: FormField['kind']) => { if (!page) return; const { field } = await addField(form, page, rowFor(kind)); setAdded((fields) => [...fields, field]); stage.reveal(page, { rect: form.getWidget(field.widgets[0])!.rect }); }; const last = added.at(-1); return (
{added.map((field) => field.name).join(', ') || 'No fields in the table yet'}
); } export default function App() { return ( Loading…

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