import { ChangeDetectionStrategy, Component, effect, inject, signal, untracked, ViewEncapsulation, } from '@angular/core'; import { EpdfDocument, EpdfDocumentGate, EpdfDocuments, EpdfViewer, 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 { copySelection, EpdfSelection, EpdfSelectionLayer, withSelection, } from '@embedpdf/angular/selection'; import { EpdfSearch, EpdfSearchLayer, withSearch } from '@embedpdf/angular/search'; import { localEngine } from '@embedpdf/engine'; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // What each role may do, as permissions. const roles = { viewer: ['doc.open', 'doc.render', 'doc.text.select'], reviewer: ['doc.open', 'doc.render', 'doc.text.select', 'doc.text.copy', 'doc.text.search'], owner: [ 'doc.open', 'doc.render', 'doc.text.select', 'doc.text.copy', 'doc.text.search', 'doc.download', ], }; type Role = keyof typeof roles; // Each control shows only when its check says yes. @Component({ selector: 'demo-controls', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@if (search.canSearch()) { } @if (selection.canCopy()) { } @if (documents.canDownload()) { }

{{ copied() ? 'Copied: “' + copied() + '”' : ' ' }}

`, }) export class Controls { protected readonly documents = inject(EpdfDocuments); protected readonly search = inject(EpdfSearch); protected readonly selection = inject(EpdfSelection); private readonly document = inject(EpdfDocument); protected readonly text = signal('PDF'); protected readonly copied = signal(''); constructor() { // On load: a search, and some text selected, so every check has something to act on. effect(() => { const text = this.text(); untracked(() => { if (this.search.canSearch()) void this.search.search({ text }); }); }); effect(() => { const cover = this.document.pages()[0]?.ref; if (cover) untracked(() => this.selection.select({ page: cover, start: 28, count: 9 })); }); } protected async copy() { await copySelection(this.selection).catch(() => {}); // the browser may refuse the clipboard this.copied.set(await this.selection.readText()); } protected async download() { saveFile(await this.documents.download(), 'welcome.pdf'); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSearchLayer, EpdfSelectionLayer, Controls, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), scope: roles.reviewer, initialDocuments: [{ source: welcome, name: 'welcome.pdf' }], }, withStage(), withRender(), withInteraction(), withSelection(), withSearch(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './checks.css', encapsulation: ViewEncapsulation.None, template: `

Opening…

`, }) export class App { private readonly viewer = inject(EpdfViewer); private readonly documents = inject(EpdfDocuments); private readonly document = inject(EpdfDocument); protected readonly role = signal('reviewer'); // A document keeps the permissions it opened with, so a new role opens it again. protected async pickRole(value: string) { const role = value as Role; this.role.set(role); this.viewer.updateSettings({ scope: roles[role] }); const id = this.document.id(); if (!id) return; await this.documents.close(id); await this.documents.open(welcome, { name: 'welcome.pdf' }); } }