import { ChangeDetectionStrategy, Component, computed, inject, input, OnInit, signal, ViewEncapsulation, } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { DocumentMetadata, OpenInput } from '@embedpdf/angular/runtime'; import { EpdfMetadata, withMetadata } from '@embedpdf/angular/metadata'; import { localEngine } from '@embedpdf/engine'; 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()) }; }; const EDITABLE = ['title', 'author', 'subject', 'keywords'] as const; type Draft = Record<(typeof EDITABLE)[number], string>; // A properties form: the inputs start with what the file says, and Save writes them back. @Component({ selector: 'demo-properties-form', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (key of editable; track key) { }

Changes

Last changed: {{ modifiedAt() }}

@if (changes().length === 0) {

Edit a field and save.

} @else {
    @for (change of changes(); track changes().length - $index) {
  • {{ change }}
  • }
}
`, }) export class PropertiesForm implements OnInit { /** The fields as the file has them when the form starts. */ readonly initial = input.required(); protected readonly metadata = inject(EpdfMetadata); protected readonly editable = EDITABLE; protected readonly draft = signal({ title: '', author: '', subject: '', keywords: '' }); protected readonly changes = signal([]); protected readonly modifiedAt = computed(() => { const modifiedAt = this.metadata.fields()?.modifiedAt ?? null; return modifiedAt ? new Date(modifiedAt).toLocaleString() : 'not set'; }); constructor() { // Every change to the standard fields, by you or anyone else. this.metadata.updated$ .pipe(takeUntilDestroyed()) .subscribe(({ changedKeys, origin }) => this.changes.update((list) => [ `${changedKeys.join(', ')} changed ${origin.kind === 'local' ? 'here' : 'elsewhere'}`, ...list, ]), ); } ngOnInit() { const initial = this.initial(); this.draft.set({ title: initial.title ?? '', author: initial.author ?? '', subject: initial.subject ?? '', keywords: initial.keywords ?? '', }); } protected edit(key: keyof Draft, value: string) { this.draft.update((draft) => ({ ...draft, [key]: value })); } // An empty field removes it: null, not ''. protected save() { const draft = this.draft(); void this.metadata.update({ title: draft.title.trim() || null, author: draft.author.trim() || null, subject: draft.subject.trim() || null, keywords: draft.keywords.trim() || null, }); } protected modifiedNow() { void this.metadata.update({ modifiedAt: new Date() }); } } // The form starts once the properties have been read from the file. @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, PropertiesForm], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withMetadata(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './edit.css', encapsulation: ViewEncapsulation.None, template: ` @if (metadata.fields(); as fields) { } @else {

Reading…

}

Loading…

`, }) export class App { protected readonly metadata = inject(EpdfMetadata); }