import { ChangeDetectionStrategy, Component, computed, effect, inject, input, signal, untracked, 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 { EpdfAnnotation, EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { localEngine } from '@embedpdf/engine'; // [!doc-source article] const article = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/article.pdf'); return { kind: 'bytes', id: 'article', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // On page 1: the characters of the title's first line, and where it is. // [!doc-range article firstLine] const SENTENCE = { start: 18, count: 33 }; // [!/doc-range] const SENTENCE_BOX = { x: 60, y: 102, width: 363, height: 31 }; const MARKUP = [ { tool: 'highlight', label: 'Highlight' }, { tool: 'underline', label: 'Underline' }, { tool: 'strikeout', label: 'Strike out' }, { tool: 'squiggly', label: 'Squiggly' }, ]; /** * The selected text becomes a mark, one per page; the selection is cleared afterwards. Next to * ``: ``. */ @Component({ selector: 'demo-markup-toolbar', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (markup of markups; track markup.tool) { } {{ readout() }}
`, }) export class MarkupToolbar { readonly stage = input.required(); private readonly annotation = inject(EpdfAnnotation); protected readonly selection = inject(EpdfSelection); private readonly document = inject(EpdfDocument); protected readonly markups = MARKUP; private readonly status = signal(''); protected readonly readout = computed(() => this.selection.hasSelection() ? 'Text selected' : this.status() || 'Select text', ); constructor() { // On load: the sentence is selected, ready to mark, and in view. effect(() => { const page = this.document.pages()[0]?.ref; const stage = this.stage(); if (!page) return; untracked(() => { this.selection.select({ page, ...SENTENCE }); stage.reveal(page, { rect: SENTENCE_BOX }); }); }); } protected async mark(tool: string, label: string) { const { annotations } = await this.annotation.createFromSelection(tool); this.status.set(`${label}: ${annotations.length} made`); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfAnnotationLayer, MarkupToolbar, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: article }] }, withStage(), withRender(), withInteraction(), withSelection(), withAnnotation(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './from-selection.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}