import { ChangeDetectionStrategy, Component, computed, inject, model, 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 { EpdfInteraction, withInteraction } from '@embedpdf/angular/interaction'; import { EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { localEngine } from '@embedpdf/engine'; // [!doc-source sketch] const sketch = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/sketch.pdf'); return { kind: 'bytes', id: 'sketch', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] const TOOLS = [ { id: 'note', label: 'Note' }, { id: 'square', label: 'Rectangle' }, ]; @Component({ selector: 'demo-toolbar', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (tool of tools; track tool.id) { }
`, }) export class Toolbar { /** The ghost opacity from CSS, or `null` for each tool's own. */ readonly opacity = model.required(); protected readonly interaction = inject(EpdfInteraction); protected readonly tools = TOOLS; protected readonly readout = computed(() => { const opacity = this.opacity(); return opacity === null ? "each tool's own" : `${Math.round(opacity * 100)}%`; }); constructor() { // The note tool is active on load: move the pointer over the page. this.interaction.activateTool('note'); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfAnnotationLayer, Toolbar, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: sketch }] }, withStage(), withRender(), withInteraction(), // A preview for the rectangle (off by default for tools you drag), and a fainter one for notes. withAnnotation({ tools: [ { id: 'square', ghost: true }, { id: 'note', ghost: { opacity: 0.3 } }, ], }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './ghost.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App { // `--epdf-ghost-opacity` from CSS wins over each tool's own opacity, for every tool. protected readonly opacity = signal(null); }