import { ChangeDetectionStrategy, Component, computed, inject, input, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocument, EpdfDocumentGate, EpdfDocuments, EpdfViewer, 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 { cloudEngine } from '@cloudpdf/engine'; const engine = cloudEngine(); const loan: OpenInput = { kind: 'grant', grant: 'shr_WGj1goAtlNN_fQ5OswPrbJQM' }; // Who the document's groups are: your app's own list. The PDF holds only each field's group id. const signers = [ { id: 'borrower', label: 'Dana Smith', color: '#2563eb' }, { id: 'approver', label: 'Ada Lovelace' }, // a color is picked for it ]; // The sender places fields for both signers. Each signer fills in and signs their own, and your // app knows who each of them is. const see = ['doc.open', 'doc.render', 'doc.download', 'doc.forms.read']; const people = { sender: { name: 'The sender', scope: [ ...see, 'doc.forms.modify', 'fields:set-group:group=borrower', 'fields:set-group:group=approver', ], identity: null, }, borrower: { name: 'Dana Smith', scope: [...see, 'fields:fill:group=borrower', 'fields:sign:group=borrower'], identity: { userId: 'u_381', displayName: 'Dana Smith', email: 'dana@fernquill.example', organization: 'Fernquill Ltd', title: 'Operations', }, }, approver: { name: 'Ada Lovelace', scope: [...see, 'fields:fill:group=approver', 'fields:sign:group=approver'], identity: { userId: 'u_412', displayName: 'Ada Lovelace', email: 'ada@fernquill.example', organization: 'Fernquill Ltd', title: 'Finance', }, }, }; type Person = keyof typeof people; // A signing tool for each kind of value: a text box that says what it is for. const VALUE_TOOLS = [ ['signer-name', 'Name'], ['signer-email', 'Email'], ['signer-organization', 'Organization'], ['signer-organization-title', 'Title'], ['signer-date-signed', 'Date signed'], ] as const; /** The sender picks who the next field is for and what it is for, and clicks the page. */ @Component({ selector: 'demo-sender-toolbar', changeDetection: ChangeDetectionStrategy.OnPush, template: `
Next field is for @for (group of form.groups(); track group.id) { } @for (tool of tools; track tool[0]) { }
`, }) export class SenderToolbar { protected readonly form = inject(EpdfForm); protected readonly interaction = inject(EpdfInteraction); protected readonly tools = VALUE_TOOLS; constructor() { // Start by placing name fields for the first signer. this.form.setDesignGroup('borrower'); this.interaction.activateTool('signer-name'); } } /** A signer starts: the app fills in what it knows about them, in their own fields, as one change. */ @Component({ selector: 'demo-signer-bar', changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ message() }}
`, }) export class SignerBar { readonly person = input.required(); private readonly filled = signal(null); protected readonly message = computed(() => { const filled = this.filled(); return filled === null ? 'Filling in what the app knows…' : `Filled in for ${people[this.person()].name}: ${filled} ${filled === 1 ? 'field' : 'fields'}`; }); constructor() { // Another signer's field is left for them, and so is a field that has a value. void inject(EpdfForm) .prefill() .then(({ applied }) => this.filled.set(applied.length)); } } /** Names the open document's groups, and shows the sender's toolbar or a signer's bar. */ @Component({ selector: 'demo-toolbar', imports: [SenderToolbar, SignerBar], changeDetection: ChangeDetectionStrategy.OnPush, template: ` @if (form.canDesign()) { } @else { } `, }) export class Toolbar { readonly person = input.required(); protected readonly form = inject(EpdfForm); constructor() { // Tell the open document who its groups are. this.form.setGroups(signers); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfAnnotationLayer, EpdfFormLayer, Toolbar, ], providers: [ provideEmbedPdf( { engine, scope: people.sender.scope, identity: people.sender.identity, initialDocuments: [{ source: loan, name: 'equipment-loan.pdf' }], }, withStage(), withRender(), withInteraction(), withAnnotation(), withForm(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './purposes.css', encapsulation: ViewEncapsulation.None, template: `
Open the form as
@for (each of everyone; track each) { }

Opening…

`, }) export class App { private readonly viewer = inject(EpdfViewer); private readonly documents = inject(EpdfDocuments); private readonly document = inject(EpdfDocument); protected readonly people = people; protected readonly everyone = Object.keys(people) as Person[]; protected readonly person = signal('sender'); // A document keeps who it opened for, so the next person opens the saved form again. protected async handOver(person: Person) { if (person === this.person()) return; this.person.set(person); const { scope, identity } = people[person]; this.viewer.updateSettings({ scope, identity }); const id = this.document.id(); if (!id) return; const bytes = await this.documents.download(); await this.documents.close(id); await this.documents.open({ kind: 'bytes', bytes }, { name: 'equipment-loan.pdf' }); } }