import { ChangeDetectionStrategy, Component, effect, inject, signal, ViewEncapsulation, } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; 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 { EpdfSearch, EpdfSearchLayer, withSearch } from '@embedpdf/angular/search'; 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', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] const colors = ['#ffd500', '#7dd3fc', '#86efac']; @Component({ selector: 'demo-search-box', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@if (search.hitCount() > 0) { {{ search.activeHitIndex() + 1 }} of {{ search.hitCount() }} } @else { No matches } @for (swatch of colors; track swatch) { }

{{ announcement() }}

`, }) export class SearchBox { // One service for everything: the API, the data, the events and the settings. protected readonly search = inject(EpdfSearch); protected readonly colors = colors; protected readonly text = signal('PDF'); protected readonly announcement = signal(''); constructor() { // A call to your function when something happens: an RxJS stream. this.search.completed$ .pipe(takeUntilDestroyed()) .subscribe(({ hitCount }) => this.announcement.set(`The search finished with ${hitCount} matches.`), ); effect(() => { const text = this.text(); void this.search.search({ text }); }); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSearchLayer, SearchBox, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withSearch(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './hooks.css', encapsulation: ViewEncapsulation.None, template: `

Opening…

`, }) export class App {}