import { ChangeDetectionStrategy, Component, effect, signal, untracked, viewChild, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageChrome, EpdfPageTemplate, EpdfStage, withStage, type RevealZoom, } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; 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()) }; }; // Spots to jump to: a page index and a box in page coordinates (points from // the page's top-left), like a search hit or a comment would give you. const SPOTS = [ { label: 'Top of page 2', page: 1, rect: { x: 72, y: 72, width: 468, height: 96 } }, { label: 'Middle of page 3', page: 2, rect: { x: 72, y: 340, width: 468, height: 120 } }, { label: 'Corner of page 4', page: 3, rect: { x: 330, y: 620, width: 210, height: 110 } }, ]; const ZOOMS: { label: string; zoom: RevealZoom }[] = [ { label: 'Keep zoom', zoom: 'keep' }, { label: 'Fit width', zoom: 'fit-width' }, { label: 'Fit the box', zoom: 'fit' }, ]; @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfPageChrome, EpdfRenderLayer], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './reveal.css', encapsulation: ViewEncapsulation.None, template: `
@for (spot of spots; track spot.label) { }
@for (option of zooms; track option.label) { }
@for (spot of spots; track spot.label) { @if (spot.page === page.pageIndex()) { @let box = page.transform().pageToViewRect(spot.rect);
} }

Loading…

`, }) export class App { protected readonly spots = SPOTS; protected readonly zooms = ZOOMS; protected readonly active = signal(0); protected readonly zoom = signal('fit-width'); private readonly stage = viewChild(EpdfStage); constructor() { // Open on the first spot, once the document is there. effect(() => { const stage = this.stage(); if (!stage || stage.pageCount() === 0) return; untracked(() => stage.reveal(SPOTS[0].page, { rect: SPOTS[0].rect, zoom: 'fit-width', align: { y: 0.35 }, }), ); }); } protected reveal(stage: EpdfStage, index: number, zoom: RevealZoom) { const spot = SPOTS[index]; // The box lands about a third from the top, like a browser's find bar. stage.reveal(spot.page, { rect: spot.rect, zoom, align: { y: 0.35 } }); this.active.set(index); } }