import { ChangeDetectionStrategy, Component, computed, effect, inject, input, signal, 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 { withInteraction } from '@embedpdf/angular/interaction'; import { EpdfSelection, EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection'; import { withSearch } from '@embedpdf/angular/search'; import { EpdfAnnotation, EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { EpdfRedaction, withRedaction } from '@embedpdf/angular/redaction'; 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', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] /** Next to ``: ``. */ @Component({ selector: 'demo-mark-from-code', changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ marks() }}
`, }) export class MarkFromCode { readonly stage = input.required(); protected readonly redaction = inject(EpdfRedaction); protected readonly selection = inject(EpdfSelection); private readonly annotation = inject(EpdfAnnotation); protected readonly text = signal('John Warnock'); protected readonly marks = computed(() => { const count = this.redaction.pendingCount(); return `${count} ${count === 1 ? 'mark' : 'marks'}`; }); private started = false; constructor() { // On load: every "John Warnock" in the document. effect(() => { if (this.annotation.status() !== 'ready' || this.started) return; this.started = true; void this.redaction.markMatches({ text: 'John Warnock' }); }); } protected markMatches() { const text = this.text().trim(); if (text) void this.redaction.markMatches({ text }); } protected markThisPage() { void this.redaction.markPage(this.stage().currentPageIndex()); } } // Marking matches runs a search; marking the selected text needs a selection. @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfAnnotationLayer, MarkFromCode, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withInteraction(), withSelection(), withSearch(), withAnnotation(), withRedaction(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './from-code.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}