import { ChangeDetectionStrategy, Component, computed, effect, inject, input, signal, untracked, viewChild, 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 { EpdfAnnotation, EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { cloudEngine } from '@cloudpdf/engine'; const layout: OpenInput = { kind: 'grant', grant: 'shr_d2E_LaTuddJbZR00d1CvkqLM' }; /** * duplicate() copies the selected annotation, here onto the next page. Next to * ``: ``. */ @Component({ selector: 'demo-copy-button', changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ status() || (first() ? 'Ready to copy' : 'Select an annotation to copy') }}
`, }) export class CopyButton { readonly stage = input.required(); protected readonly annotation = inject(EpdfAnnotation); private readonly pages = inject(EpdfDocument).pages; protected readonly status = signal(''); protected readonly first = computed(() => this.annotation.selected()[0]); protected readonly next = computed(() => { const first = this.first(); if (!first) return undefined; const pages = this.pages(); const index = pages.findIndex((page) => page.ref.objectNumber === first.page.objectNumber); return pages[index + 1]; }); protected async copyToNextPage() { const first = this.first(); const next = this.next(); if (!first || !next) return; const { annotations } = await this.annotation.duplicate([first.ref], { page: next.ref, select: true, // so the next click copies the copy, a page further }); const [made] = annotations; if (made) this.stage().reveal(next.ref, { rect: made.rect }); this.status.set(`Copied to page ${next.index + 1}`); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfAnnotationLayer, CopyButton, ], providers: [ provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: layout }] }, withStage(), withRender(), withInteraction(), withAnnotation(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './copy.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App { private readonly annotation = inject(EpdfAnnotation); private readonly stage = viewChild(EpdfStage); private done = false; constructor() { // On load: the document's square, selected, ready to copy. effect(() => { const stage = this.stage(); if (!stage || this.annotation.status() !== 'ready' || this.done) return; this.done = true; untracked(() => { const square = this.annotation.list().find((each) => each.nm === 'square'); if (!square) return; this.annotation.selection.select([square.ref]); stage.reveal(square.page, { rect: square.rect }); }); }); } }