import { useEffect, useState } from 'react'; import { Viewer, DocumentGate, isPluginError } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { metadataPlugin, useMetadata, useMetadataState } from '@embedpdf/react/metadata'; import { localEngine } from '@embedpdf/engine'; import './custom-fields.css'; const engine = localEngine(); const plugins = [metadataPlugin()]; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // Your own fields, kept in the document: add one, change one, remove one. function CustomFields() { const metadata = useMetadata(); const { custom } = useMetadataState(); const [name, setName] = useState(''); const [value, setValue] = useState(''); const [error, setError] = useState(null); // Two fields of our own, written on load. Setting the same values again changes nothing. useEffect(() => { void metadata.custom.update({ contractId: 'C-2026-114', reviewedBy: 'dana' }); }, [metadata]); const canUpdate = metadata.canUpdate(); const add = async () => { try { await metadata.custom.update({ [name.trim()]: value }); setName(''); setValue(''); setError(null); } catch (failure) { // A name the PDF can't take, such as one of the standard fields. if (isPluginError(failure, 'invalid-input')) setError(failure.message); else throw failure; } }; return (
{Object.entries(custom ?? {}).map(([key, text]) => ( ))}
Name Value
{key} {text}
{ event.preventDefault(); void add(); }} > setName(event.target.value)} /> setValue(event.target.value)} />
{error &&

{error}

}
); } export default function App() { return ( Loading…

}>
); }