import { ChangeDetectionStrategy, Component, computed, effect, inject, untracked, ViewEncapsulation, } from '@angular/core'; import { EpdfDocument, EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { EpdfInteraction, withInteraction } from '@embedpdf/angular/interaction'; import { EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { EpdfForm, EpdfFormLayer, withForm } from '@embedpdf/angular/form'; import { localEngine } from '@embedpdf/engine'; 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; @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfAnnotationLayer, EpdfFormLayer, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: registration }] }, withStage(), withRender(), withInteraction(), // Building forms needs the annotation plugin: in design mode, fields are boxes like any annotation. withAnnotation(), withForm(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './designer.css', encapsulation: ViewEncapsulation.None, template: ` @if (!form.canDesign()) {

This document's form can't be changed.

} @else {
@for (tool of tools; track tool[0]) { } {{ form.fields().length }} fields: click the page to place another
}

Loading…

`, }) export class App { protected readonly form = inject(EpdfForm); protected readonly interaction = inject(EpdfInteraction); protected readonly tools = FIELD_TOOLS; private readonly document = inject(EpdfDocument); private started = false; /** The pointer fills the form in; every other tool here designs it. */ protected readonly filling = computed(() => this.interaction.activeToolId() === 'pointer'); constructor() { // Start with the text tool picked: the printed form has boxes, but no fields yet. effect(() => { if (this.document.status() !== 'ready' || this.started) return; this.started = true; untracked(() => this.interaction.activateTool('form-text')); }); } }