import { ChangeDetectionStrategy, Component, computed, inject, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, isPluginError, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfMetadata, withMetadata } from '@embedpdf/angular/metadata'; import { localEngine } from '@embedpdf/engine'; // [!doc-source welcome] 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()) }; }; // [!/doc-source] // Your own fields, kept in the document: add one, change one, remove one. @Component({ selector: 'demo-custom-fields', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (field of fields(); track field[0]) { }
Name Value
{{ field[0] }} {{ field[1] }}
@if (error(); as error) {

{{ error }}

}
`, }) export class CustomFields { protected readonly metadata = inject(EpdfMetadata); protected readonly fields = computed(() => Object.entries(this.metadata.custom.fields() ?? {})); protected readonly name = signal(''); protected readonly value = signal(''); protected readonly error = signal(null); constructor() { // Two fields of our own, written on load. Setting the same values again changes nothing. void this.metadata.custom.update({ contractId: 'C-2026-114', reviewedBy: 'dana' }); } protected async add() { try { await this.metadata.custom.update({ [this.name().trim()]: this.value() }); this.name.set(''); this.value.set(''); this.error.set(null); } catch (failure) { // A name the PDF can't take, such as one of the standard fields. if (isPluginError(failure, 'invalid-input')) this.error.set(failure.message); else throw failure; } } protected remove(key: string) { void this.metadata.custom.update({ [key]: null }); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, CustomFields], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withMetadata(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './custom-fields.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}