import { ChangeDetectionStrategy, Component, inject, 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 { EpdfAnnotation, EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import type { AnnotationTool } from '@embedpdf/angular/annotation'; import { localEngine } from '@embedpdf/engine'; 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()) }; }; // A toolbar that builds itself from the tools: every tool with a label gets a button. @Component({ selector: 'demo-toolbar', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (tool of labelled; track tool.id) { }
`, }) export class Toolbar { private readonly annotation = inject(EpdfAnnotation); protected readonly interaction = inject(EpdfInteraction); protected readonly labelled = this.annotation.tools .list() .filter((tool) => typeof tool.meta?.label === 'string'); constructor() { // On load: the arrow tool is active. this.interaction.activateTool('arrow'); } protected label(tool: AnnotationTool) { return String(tool.meta?.label); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfAnnotationLayer, Toolbar, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: sketch }] }, withStage(), withRender(), withInteraction(), // A blue pen, an arrow, and three tools of your own. `meta` is yours: the toolbar reads its label. withAnnotation({ tools: [ { id: 'ink', defaults: { color: '#1e90ff', strokeWidth: 3 }, meta: { label: 'Blue pen' }, }, { id: 'arrow', extends: 'line', defaults: { lineEndings: { start: 'none', end: 'closed-arrow' } }, meta: { label: 'Arrow' }, }, { id: 'red-pen', extends: 'ink', defaults: { color: '#ff0000', strokeWidth: 2 }, meta: { label: 'Red pen' }, }, { id: 'marker', extends: 'ink-highlight', defaults: { color: '#ffa500' }, meta: { label: 'Marker' }, }, { id: 'todo', extends: 'note', defaults: { icon: 'key', contents: 'TODO' }, meta: { label: 'To do' }, }, ], }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './custom-tools.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}