import { ChangeDetectionStrategy, Component, effect, inject, signal, 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 { copySelection, EpdfSelection, EpdfSelectionClipboard, EpdfSelectionLayer, withSelection, } from '@embedpdf/angular/selection'; import { cloudEngine } from '@cloudpdf/engine'; const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' }; @Component({ selector: 'demo-copy-button', changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ status() || 'Or press Ctrl+C' }}
`, }) export class CopyButton { protected readonly selection = inject(EpdfSelection); protected readonly status = signal(''); protected copy() { copySelection(this.selection).then( (text) => this.status.set(`Copied ${text.length} characters`), // The browser can refuse the clipboard, for example in a frame that doesn't allow it. () => this.status.set("The browser didn't allow copying"), ); } } // The selected text, read with readText() each time the selection settles. @Component({ selector: 'demo-selected-text', changeDetection: ChangeDetectionStrategy.OnPush, template: `

{{ text() || 'Select some text to read it here.' }}

`, }) export class SelectedText { private readonly selection = inject(EpdfSelection); protected readonly text = signal(''); constructor() { effect((onCleanup) => { this.selection.range(); if (this.selection.isSelecting() || !this.selection.canCopy()) return; // A newer selection cancels a read that hasn't finished. const controller = new AbortController(); this.selection.readText({ signal: controller.signal }).then( (text) => this.text.set(text), () => {}, ); onCleanup(() => controller.abort()); }); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfSelectionClipboard, CopyButton, SelectedText, ], providers: [ provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withInteraction(), withSelection(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './copy.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App { private readonly selection = inject(EpdfSelection); private readonly document = inject(EpdfDocument); constructor() { // Something selected on load: "Documents", the first word on the cover. effect(() => { const cover = this.document.pages()[0]?.ref; if (cover) this.selection.select({ page: cover, start: 36, count: 9 }); }); } }