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, EpdfAnnotationTemplate, EpdfRichTextEditor, withAnnotation, type Annotation, } from '@embedpdf/angular/annotation'; import { localEngine } from '@embedpdf/engine'; // [!doc-source notice] 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()) }; }; // [!/doc-source] const FORMATS = ['bold', 'italic', 'underline'] as const; // The formatting calls work on your element as they do on the built-in one. @Component({ selector: 'demo-format-buttons', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (format of formats; track format) { }
`, }) export class FormatButtons { protected readonly annotation = inject(EpdfAnnotation); protected readonly formats = FORMATS; protected readonly hasText = computed(() => this.annotation.selected().some((selected) => selected.subtype === 'free-text'), ); } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfAnnotationLayer, EpdfAnnotationTemplate, EpdfRichTextEditor, FormatButtons, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: notice }] }, withStage(), withRender(), withInteraction(), withSelection(), withAnnotation(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './branded-text-box.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App { private readonly annotation = inject(EpdfAnnotation); private readonly stage = viewChild(EpdfStage); private done = false; // Kept in a field, the same function on every page: the layer registers the look once. protected readonly isTextBox = (annotation: Annotation) => annotation.subtype === 'free-text'; constructor() { // On load: the document's text box, selected, drawn by your component. 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 }); }); }); } }