import { ChangeDetectionStrategy, Component, computed, effect, inject, untracked, viewChild, ViewEncapsulation, } from '@angular/core'; import { 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 { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection'; import { withInteraction } from '@embedpdf/angular/interaction'; import { EpdfAnnotation, EpdfAnnotationLayer, EpdfAnnotationMenu, withAnnotation, } from '@embedpdf/angular/annotation'; import { localEngine } from '@embedpdf/engine'; const notice = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/notice.pdf'); return { kind: 'bytes', id: 'notice', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // What's in the menu depends on what's selected: text boxes get Bold and Edit. @Component({ selector: 'demo-selection-actions', changeDetection: ChangeDetectionStrategy.OnPush, template: ` `, }) export class SelectionActions { protected readonly annotation = inject(EpdfAnnotation); protected readonly selected = this.annotation.selected; protected readonly allText = computed(() => this.selected().every((a) => a.subtype === 'free-text'), ); protected edit() { const [first] = this.selected(); if (first) this.annotation.text.begin(first.ref); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfAnnotationLayer, EpdfAnnotationMenu, SelectionActions, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: notice }] }, withStage(), withRender(), withInteraction(), withSelection(), withAnnotation(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './menu.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App { private readonly annotation = inject(EpdfAnnotation); private readonly stage = viewChild(EpdfStage); private done = false; constructor() { // On load: the document's text box, selected, so its menu shows. effect(() => { const stage = this.stage(); if (!stage || this.annotation.status() !== 'ready' || this.done) return; this.done = true; untracked(() => { const textBox = this.annotation.list().find((each) => each.nm === 'text-box'); if (!textBox) return; this.annotation.selection.select([textBox.ref]); stage.reveal(textBox.page, { rect: textBox.rect }); }); }); } }