();
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-make-stamp',
imports: [MyStamp],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (asset of mine(); track asset.id) {
}
`,
})
export class MakeStamp {
private readonly stamp = inject(EpdfStamp);
protected readonly selected = inject(EpdfAnnotation).selected;
protected readonly mine = this.stamp.assetsOf({ libraryId: MY_STAMPS });
protected readonly armedAsset = this.stamp.armedAsset;
// A stamp is one page of artwork: the selection must be on one page.
private readonly page = computed(() => this.selected()[0]?.page);
protected readonly canMake = computed(() => {
const page = this.page();
const onePage = this.selected().every(
(annotation) => annotation.page.objectNumber === page?.objectNumber,
);
return !!page && onePage && this.stamp.canCreateFromAnnotations();
});
protected async make() {
const page = this.page();
if (!page) return;
// The library is made on first use.
if (!this.stamp.getLibrary(MY_STAMPS)) {
await this.stamp.createLibrary('My stamps', { id: MY_STAMPS });
}
const { asset } = await this.stamp.createAssetFromAnnotations(
page,
this.selected().map((annotation) => annotation.ref),
{ libraryId: MY_STAMPS, label: `My stamp ${this.mine().length + 1}` },
);
await this.stamp.armAsset(asset.id);
}
}
@Component({
selector: 'demo-root',
imports: [
EpdfDocumentGate,
EpdfStage,
EpdfPageTemplate,
EpdfRenderLayer,
EpdfSelectionLayer,
EpdfAnnotationLayer,
MakeStamp,
],
providers: [
provideEmbedPdf(
{ engine, initialDocuments: [{ source: approvals }] },
withStage(),
withRender(),
withInteraction(),
withSelection(),
withAnnotation(),
withStamp({ assetEngine }),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './from-selection.css',
encapsulation: ViewEncapsulation.None,
template: `
Loading…
`,
})
export class App {
private readonly annotation = inject(EpdfAnnotation);
private readonly stage = viewChild(EpdfStage);
private done = false;
constructor() {
// On load: the document's frame and word, both selected, scrolled into view.
effect(() => {
const stage = this.stage();
if (!stage || this.annotation.status() !== 'ready' || this.done) return;
this.done = true;
untracked(() => {
const found = this.annotation
.list()
.filter((each) => each.nm === 'checked-frame' || each.nm === 'checked-text');
if (found.length === 0) return;
this.annotation.selection.select(found.map((each) => each.ref));
stage.reveal(found[0].page, { rect: found[0].rect });
});
});
}
}