import { ChangeDetectionStrategy, Component, computed, effect, inject, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, EpdfDocuments, provideEmbedPdf, saveFile, } 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 { EpdfRedaction, withRedaction } from '@embedpdf/angular/redaction'; import { localEngine } from '@embedpdf/engine'; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // The cover's title, in page coordinates: the shapes drawn over it go with it. const TITLE = { x: 100, y: 212, width: 392, height: 156 }; @Component({ selector: 'demo-apply-bar', changeDetection: ChangeDetectionStrategy.OnPush, template: ` @if (asking()) {
{{ question() }}
} @else {
@if (redaction.lastResult(); as result) { Gone for good, with {{ result.removedAnnotationCount }} other annotations } @else { {{ marks() }} waiting }
} `, }) export class ApplyBar { protected readonly redaction = inject(EpdfRedaction); private readonly documents = inject(EpdfDocuments); private readonly annotation = inject(EpdfAnnotation); protected readonly asking = signal(false); protected readonly collateral = signal(0); protected readonly marks = computed(() => { const count = this.redaction.pendingCount(); return `${count} ${count === 1 ? 'mark' : 'marks'}`; }); protected readonly question = computed(() => { const count = this.collateral(); const others = count > 0 ? `${count} ${count === 1 ? 'annotation' : 'annotations'} under them go too. ` : ''; return `Redact ${this.marks()}? ${others}This can’t be undone.`; }); private started = false; constructor() { // On load: the title marked. effect(() => { if (this.annotation.status() !== 'ready' || this.started) return; this.started = true; void this.redaction.markArea(0, TITLE); }); } // The other annotations applying removes too: they could show what was there. protected ask() { this.collateral.set(this.redaction.estimateCollateral().count); this.asking.set(true); } protected redactForGood() { void this.redaction.applyAll().finally(() => this.asking.set(false)); } protected download() { void this.documents .download(undefined, { mode: 'rewrite' }) .then((bytes) => saveFile(bytes, 'redacted.pdf', 'application/pdf')); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfAnnotationLayer, ApplyBar, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withInteraction(), withAnnotation(), withRedaction(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './apply.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}