import { ChangeDetectionStrategy, Component, computed, inject, input, 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 { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection'; import { withInteraction } from '@embedpdf/angular/interaction'; import { EpdfAnnotation, EpdfAnnotationLayer, withAnnotation, type AnnotationRef, } from '@embedpdf/angular/annotation'; import { localEngine } from '@embedpdf/engine'; // [!doc-source report] const report = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/report.pdf'); return { kind: 'bytes', id: 'report', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] /** * Create a rectangle, then change, move and delete it, all from code. Next to * ``: ``. */ @Component({ selector: 'demo-square-controls', changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ status() }}
`, }) export class SquareControls { readonly stage = input.required(); private readonly annotation = inject(EpdfAnnotation); private readonly document = inject(EpdfDocument); private readonly squareRef = signal(null); private readonly annotations = this.annotation.watch(); // follows the annotations, for the reads below protected readonly square = computed(() => { this.annotations(); // read again whenever the annotations change const ref = this.squareRef(); return ref ? this.annotation.get(ref) : null; }); // Checks are signals too: they follow the annotation, the document and its permissions. protected readonly canUpdate = computed(() => { const square = this.square(); return square !== null && this.annotation.canUpdate(square.ref); }); protected readonly canDelete = computed(() => { const square = this.square(); return square !== null && this.annotation.canDelete(square.ref); }); protected readonly status = computed(() => { const square = this.square(); if (!square) return 'No rectangle'; return this.annotation.isPending(square.ref) ? 'Saving…' : 'Saved'; }); protected async add() { const page = this.document.pages()[1]?.ref; // page 2, which has room at the bottom if (!page) return; const { annotation: square } = await this.annotation.create(page, { subtype: 'square', box: { x: 72, y: 592, width: 200, height: 100 }, color: '#0078ff', strokeWidth: 3, }); this.squareRef.set(square.ref); this.stage().reveal(page, { rect: square.rect }); } protected makeRed() { const square = this.square(); if (square) void this.annotation.update(square.ref, { color: '#dc143c' }); } protected moveRight() { const square = this.square(); if (!square) return; const { rect } = square; void this.annotation.update(square.ref, { rect: { ...rect, x: rect.x + 40 } }); // 40 points right } protected turn() { const square = this.square(); if (square) void this.annotation.update(square.ref, { rotation: 90 }); } protected remove() { const square = this.square(); if (!square) return; void this.annotation.delete(square.ref); this.squareRef.set(null); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfAnnotationLayer, SquareControls, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: report }] }, withStage(), withRender(), withInteraction(), withSelection(), withAnnotation(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './create-update.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}