import { ChangeDetectionStrategy, Component, computed, effect, inject, 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 { withInteraction } from '@embedpdf/angular/interaction'; import { EpdfSelection, EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection'; import { localEngine } from '@embedpdf/engine'; // On the cover: the characters of its first word, "Documents", and a point on its word "web", // in page coordinates. const FIRST_WORD = { start: 28, count: 9 }; const POINT = { x: 260, y: 242 }; 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()) }; }; // Every button selects on the cover, the first page: by its ref, or by its index, 0. @Component({ selector: 'demo-selection-toolbar', changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ summary() }}
`, }) export class SelectionToolbar { protected readonly selection = inject(EpdfSelection); private readonly document = inject(EpdfDocument); protected readonly cover = computed(() => this.document.pages()[0]?.ref); protected readonly point = POINT; protected readonly summary = computed(() => { const pages = this.selection.pages(); const range = this.selection.range(); if (pages.length > 1) return `On ${pages.length} pages`; if (range) return `${range.end.index - range.start.index} characters`; return 'Nothing selected'; }); constructor() { // The first word is selected on load. effect(() => { if (this.cover()) this.selectCharacters(); }); } protected selectCharacters() { const cover = this.cover(); if (cover) this.selection.select({ page: cover, ...FIRST_WORD }); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, SelectionToolbar, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withInteraction(), withSelection(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './programmatic.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}