import { ChangeDetectionStrategy, Component, computed, inject, input, signal, ViewEncapsulation, } from '@angular/core'; import { 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 { EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { EpdfStamp, withStamp, type StampAsset } from '@embedpdf/angular/stamp'; import { localEngine } from '@embedpdf/engine'; const engine = localEngine(); // [!asset-engine] const assetEngine = engine; // stamp libraries are PDFs; they open here too // [!/asset-engine] // [!doc-source approvals] const approvals = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/approvals.pdf'); return { kind: 'bytes', id: 'approvals', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // A signature as a pen would draw it: two strokes, in points. const loop = Array.from({ length: 48 }, (_, i) => ({ x: i * 4, y: 30 - Math.sin(i / 3) * 16 - i * 0.2, })); const underline = [ { x: 10, y: 52 }, { x: 180, y: 46 }, ]; @Component({ selector: 'button[demoMarkButton]', changeDetection: ChangeDetectionStrategy.OnPush, host: { type: 'button', class: 'button', '[title]': "'Place “' + asset().label + '”'", '[attr.aria-pressed]': 'armed()', '(click)': 'toggle()', }, template: ` @if (url(); as url) { } @else { {{ asset().label }} } `, }) export class MarkButton { readonly asset = input.required(); readonly armed = input(false); private readonly stamp = inject(EpdfStamp); protected readonly url = this.stamp.previewUrlOf(() => this.asset().id); protected toggle() { if (this.armed()) this.stamp.disarm(); else void this.stamp.armAsset(this.asset().id); } } @Component({ selector: 'demo-marks', imports: [MarkButton], changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (asset of marks(); track asset.id) { }
`, }) export class Marks { private readonly stamp = inject(EpdfStamp); protected readonly library = computed(() => this.stamp.libraries()[0]); protected readonly marks = this.stamp.assetsOf(() => ({ libraryId: this.library()?.id })); protected readonly armedAsset = this.stamp.armedAsset; protected readonly text = signal('Ada L.'); constructor() { // On load: a drawn signature and typed initials, in a library of their own. void this.stamp.createLibrary('Ada Lovelace').then(async ({ library }) => { await this.stamp.createAsset({ libraryId: library.id, label: 'Signature', mark: { kind: 'ink', strokes: [loop, underline], strokeWidth: 2.5, color: '#1d2b53' }, }); await this.stamp.createAsset({ libraryId: library.id, label: 'Initials', mark: { kind: 'text', text: 'AL', fontFamily: 'times-italic', color: '#1d2b53' }, }); }); } protected async typeMark() { const library = this.library(); const text = this.text().trim(); if (!library || !text) return; const { asset } = await this.stamp.createAsset({ libraryId: library.id, label: text, mark: { kind: 'text', text, fontFamily: 'times-italic', color: '#1d2b53' }, }); await this.stamp.armAsset(asset.id); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfAnnotationLayer, Marks, ], providers: [ provideEmbedPdf( { engine, initialDocuments: [{ source: approvals }] }, withStage(), withRender(), withInteraction(), withSelection(), withAnnotation(), withStamp({ assetEngine }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './marks.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}