EmbedPDF

Your own form controls

The form layer draws each field as an HTML control: a text box, a checkbox, a list. You can put your own UI on a field, or draw its control yourself: a tag on the fields still to fill in, a hint next to a field, a switch instead of a checkbox.

This changes the screen only. What people enter is still the field’s value in the PDF, so other PDF apps, printing and downloads show the field as the PDF describes it.

Its control, or UI on it#

Ask whether it should zoom and turn with the PDF:

You wantUseIt
A hint, a menu or a picker next to a fieldAnchored UIFloats over the pages, and moves to stay in view
A tag that stays on a field: “Required”, a signer’s name, a ✓Pinned anchored UIFloats over the pages, upright and the same size at every zoom, exactly where you put it
A different control: a switch for a checkbox, a button inside a text boxA controlIs part of the page: it zooms and turns with the field

It’s the same choice as for an annotation. Most of what an app adds to a form is the first two, and they need nothing from the form layer.

Pin a tag to a field#

A tag on each field that still has to be filled in, a signer’s name on each of their fields, a ✓ on a field that’s done: UI that belongs to one field and stays on it. form.widgetAnchorOf(widget) gives you the anchor of one box of a field, and follows it when the field is moved. pinned keeps the tag exactly where you put it:

field-tag.ts
import { Component, inject, input } from '@angular/core';
import { EpdfAnchored } from '@embedpdf/angular/anchored';
import { EpdfForm, type FormField } from '@embedpdf/angular/form';

// A tag beside the first box of a field.
@Component({
  selector: 'app-field-tag',
  imports: [EpdfAnchored],
  template: `
    <epdf-anchored [anchor]="anchor()" placement="left" [gap]="4" pinned>
      <span class="tag"><ng-content /></span>
    </epdf-anchored>
  `,
})
export class FieldTag {
  readonly field = input.required<FormField>();

  private readonly form = inject(EpdfForm);
  protected readonly anchor = this.form.widgetAnchorOf(
    () => this.form.getWidgets(this.field().ref)[0]?.ref ?? null,
  );
}

Pass the box as a function, and the anchor follows field when it changes.

Draw one for each field that needs it: form.validate().missing lists the required fields this user still has to fill in. Put them inside <epdf-stage>, like a menu.

Loading live preview…

import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
  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 { withInteraction } from '@embedpdf/angular/interaction';
import { EpdfAnchored } from '@embedpdf/angular/anchored';
import { EpdfForm, EpdfFormLayer, withForm } from '@embedpdf/angular/form';
import type { FormField } from '@embedpdf/angular/form';
import { localEngine } from '@embedpdf/engine';

const forms = async (): Promise<OpenInput> => {
  const response = await fetch('https://www.embedpdf.com/pdfs/forms.pdf');
  return { kind: 'bytes', id: 'forms', bytes: new Uint8Array(await response.arrayBuffer()) };
};

// A tag beside the first box of a field: pinned, so it stays on the field. A pinned tag keeps
// its size at every zoom, so this one is small: it fits in the page's margin on a phone too.
@Component({
  selector: 'demo-tag',
  imports: [EpdfAnchored],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <epdf-anchored [anchor]="anchor()" placement="left" [gap]="6" pinned>
      <button
        type="button"
        class="tag"
        title="Required"
        [attr.aria-label]="'Required: ' + (field().alternateName ?? field().name)"
        (click)="form.focus(field().ref)"
      >
        !
      </button>
    </epdf-anchored>
  `,
})
export class Tag {
  readonly field = input.required<FormField>();

  protected readonly form = inject(EpdfForm);
  protected readonly anchor = this.form.widgetAnchorOf(
    () => this.form.getWidgets(this.field().ref)[0]?.ref ?? null,
  );
}

@Component({
  selector: 'demo-root',
  imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfFormLayer, Tag],
  providers: [
    provideEmbedPdf(
      { engine: () => localEngine(), initialDocuments: [{ source: forms }] },
      withStage(),
      withRender(),
      withInteraction(),
      withForm(),
    ),
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  styleUrl: './tags.css',
  encapsulation: ViewEncapsulation.None,
  template: `
    <ng-container *epdfDocumentGate="let document; fallback: loading">
      <!-- The view's own controls: the tags follow the view when it zooms and turns. -->
      <div class="toolbar">
        <button type="button" class="button" (click)="stage.zoomOut()">Zoom out</button>
        <button type="button" class="button" (click)="stage.zoomIn()">Zoom in</button>
        <button type="button" class="button" (click)="stage.rotateViewBy(90)">Turn the view</button>
      </div>
      <epdf-stage #stage="epdfStage" class="stage">
        <ng-template epdfPage>
          <epdf-render-layer />
          <epdf-form-layer />
        </ng-template>
        <!-- A tag on every required field that is still empty. -->
        @for (field of missing(); track field.name) {
          <demo-tag [field]="field" />
        }
      </epdf-stage>
    </ng-container>

    <ng-template #loading><p class="loading">Loading…</p></ng-template>
  `,
})
export class App {
  private readonly form = inject(EpdfForm);

  protected readonly missing = computed(() => {
    this.form.fields(); // again when a field is filled in
    return this.form.validate().missing;
  });
}

Try it: each required field that is still empty has an orange tag. Fill one in: its tag goes. Scroll, zoom and turn the view, and the tags stay on their fields, at the same size.

A field can show in several boxes, so an anchor is for one box: form.getWidgets(field.ref) lists a field’s boxes, each with its ref. Anchored UI on a page that isn’t on screen isn’t drawn, so a form with hundreds of fields costs only the ones in view. See Where it goes for placement and gap.

A pinned tag keeps its size and its side at every zoom, and isn’t moved to where there is room. The room beside a field shrinks with the page: next to a field at the edge of a page shown small, such as on a phone, there are a few dozen pixels. So keep a tag there small, as the example’s is, or put it on a side of the field that has room.

Draw a control your way#

Put a control in the layer for each kind of box you draw: an <ng-template epdfFieldControl> that says which boxes it draws, and draws them:
controls.ts
import { Component } from '@angular/core';
import {
  EpdfFieldControlTemplate,
  EpdfFormLayer,
  type FormWidgetItem,
} from '@embedpdf/angular/form';

@Component({
  selector: 'app-fields',
  imports: [EpdfFormLayer, EpdfFieldControlTemplate],
  template: `
    <epdf-form-layer>
      <ng-template [epdfFieldControl]="isCheckbox" let-widget>
        <!-- how this box is drawn: see below -->
      </ng-template>
    </epdf-form-layer>
  `,
})
export class Fields {
  protected readonly isCheckbox = (widget: FormWidgetItem) =>
    widget.control === 'toggle' && widget.kind === 'checkbox';
}

The function you give epdfFieldControl gets the box, with what it tells you, so you can pick by control, by the field’s group or anything else. The first control that matches wins; a box no control matches is drawn as usual.

Your template draws into a frame: a box the layer places, turns and scales exactly like the field. Fill it, with width and height at 100%, and draw at the field’s 100% size: the layer scales your drawing with the page. A field that is turned turns its frame, so your control reads the way the field reads with no code of yours.

switch.ts
import { Component } from '@angular/core';
import {
  EpdfFieldControlTemplate,
  EpdfFormLayer,
  type FormWidgetItem,
} from '@embedpdf/angular/form';

@Component({
  selector: 'app-fields',
  imports: [EpdfFormLayer, EpdfFieldControlTemplate],
  template: `
    <epdf-form-layer>
      <ng-template [epdfFieldControl]="isCheckbox" let-widget let-setValue="setValue">
        <!-- It fills its frame (width and height 100% in your CSS), and turns with the field. -->
        @if (widget.control === 'toggle') {
          <button
            role="switch"
            class="switch"
            [attr.aria-checked]="widget.checked"
            [attr.aria-label]="widget.label"
            [disabled]="widget.disabled"
            (click)="setValue({ checked: !widget.checked })"
          ></button>
        }
      </ng-template>
    </epdf-form-layer>
  `,
})
export class Fields {
  protected readonly isCheckbox = (widget: FormWidgetItem) =>
    widget.control === 'toggle' && widget.kind === 'checkbox';
}
Your template gets these: the box is let-widget, and the rest are named, such as let-frame="frame".
PropWhat it is
widgetThe box, as listWidgets() gives it: its field (fieldRef), its control, its value, and what it tells you.
frameThe box you draw into: its width and height in pixels at the field’s 100% size, its rotation on screen in degrees clockwise, and the scale the layer draws it at.
nativeThe layer’s own control for the box with the field’s picture, filling the frame. Render it to keep the control and add to it.
setValueFills in the field: setValue({ value }), ({ checked }) or ({ selectedValues }), with the same checks, scripts and undo step as form.setValue().
focusedTrue while the cursor is in this box.

The frame is the same one an annotation renderer gets. Your control is drawn in place of the field’s picture, so a switch shows no check mark under it.

Loading live preview…

import { ChangeDetectionStrategy, Component, 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 { withInteraction } from '@embedpdf/angular/interaction';
import { EpdfFieldControlTemplate, EpdfFormLayer, withForm } from '@embedpdf/angular/form';
import type { FormWidgetItem } from '@embedpdf/angular/form';
import { localEngine } from '@embedpdf/engine';

const forms = async (): Promise<OpenInput> => {
  const response = await fetch('https://www.embedpdf.com/pdfs/forms.pdf');
  return { kind: 'bytes', id: 'forms', bytes: new Uint8Array(await response.arrayBuffer()) };
};

@Component({
  selector: 'demo-root',
  imports: [
    EpdfDocumentGate,
    EpdfStage,
    EpdfPageTemplate,
    EpdfRenderLayer,
    EpdfFormLayer,
    EpdfFieldControlTemplate,
  ],
  providers: [
    provideEmbedPdf(
      { engine: () => localEngine(), initialDocuments: [{ source: forms }] },
      withStage(),
      withRender(),
      withInteraction(),
      withForm(),
    ),
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  styleUrl: './controls.css',
  encapsulation: ViewEncapsulation.None,
  template: `
    <ng-container *epdfDocumentGate="let document; fallback: loading">
      <!-- The view's own controls: the switches follow the view when it zooms and turns. -->
      <div class="toolbar">
        <button type="button" class="button" (click)="stage.zoomOut()">Zoom out</button>
        <button type="button" class="button" (click)="stage.zoomIn()">Zoom in</button>
        <button type="button" class="button" (click)="stage.rotateViewBy(90)">Turn the view</button>
      </div>
      <epdf-stage #stage="epdfStage" class="stage">
        <ng-template epdfPage>
          <epdf-render-layer />
          <epdf-form-layer>
            <!-- The first control that matches a box draws it; the layer draws every other
                 box. A checkbox as a switch: a click fills the field in, as a click on the
                 checkbox would. -->
            <ng-template [epdfFieldControl]="isCheckbox" let-widget let-setValue="setValue">
              @if (widget.control === 'toggle') {
                <button
                  type="button"
                  role="switch"
                  class="switch"
                  [attr.aria-checked]="widget.checked"
                  [attr.aria-label]="widget.label"
                  [disabled]="widget.disabled"
                  (click)="setValue({ checked: !widget.checked })"
                ></button>
              }
            </ng-template>
          </epdf-form-layer>
        </ng-template>
      </epdf-stage>
    </ng-container>

    <ng-template #loading><p class="loading">Loading…</p></ng-template>
  `,
})
export class App {
  protected readonly isCheckbox = (widget: FormWidgetItem) =>
    widget.control === 'toggle' && widget.kind === 'checkbox';
}

Try it: flip a switch, and the checkbox it stands for is filled in. Press Tab to move through the fields, the switches among them. Zoom and turn the view: the switches zoom and turn with the page.

A click on your control also runs the field’s own action, when the PDF gives it one, after the value you wrote: as a click on the layer’s control does.

Add to the layer’s control#

native is what the layer would have drawn: the field’s picture, and its control. Render it and put your own parts next to it, such as a button at the end of a text box that empties it:

clear-button.ts
import { NgTemplateOutlet } from '@angular/common';
import { Component, inject } from '@angular/core';
import {
  EpdfFieldControlTemplate,
  EpdfForm,
  EpdfFormLayer,
  type EpdfFieldControlTemplateContext,
  type FormWidgetItem,
} from '@embedpdf/angular/form';

@Component({
  selector: 'app-fields',
  imports: [EpdfFormLayer, EpdfFieldControlTemplate, NgTemplateOutlet],
  template: `
    <epdf-form-layer>
      <ng-template
        [epdfFieldControl]="isTextBox"
        let-widget
        let-native="native"
        let-setValue="setValue"
      >
        <!-- The layer's own text box, and a button at its end that empties it. -->
        <ng-container [ngTemplateOutlet]="native" />
        @if (widget.control === 'text' && !widget.disabled && widget.value !== '') {
          <!-- Tab goes to the text box. -->
          <button
            class="clear-button"
            [attr.aria-label]="'Clear ' + widget.label"
            tabindex="-1"
            (click)="clear(widget, setValue)"
          ></button>
        }
      </ng-template>
    </epdf-form-layer>
  `,
})
export class Fields {
  private readonly form = inject(EpdfForm);

  protected readonly isTextBox = (widget: FormWidgetItem) =>
    widget.control === 'text' && !widget.multiline;

  // Empty the box, then put the cursor back in it, ready for the new value.
  protected clear(widget: FormWidgetItem, setValue: EpdfFieldControlTemplateContext['setValue']) {
    void setValue({ value: '' }).then(() => this.form.focus(widget.fieldRef));
  }
}

Loading live preview…

import { NgTemplateOutlet } from '@angular/common';
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 { withInteraction } from '@embedpdf/angular/interaction';
import {
  EpdfFieldControlTemplate,
  EpdfForm,
  EpdfFormLayer,
  withForm,
} from '@embedpdf/angular/form';
import type { EpdfFieldControlTemplateContext, FormWidgetItem } from '@embedpdf/angular/form';
import { localEngine } from '@embedpdf/engine';

const forms = async (): Promise<OpenInput> => {
  const response = await fetch('https://www.embedpdf.com/pdfs/forms.pdf');
  return { kind: 'bytes', id: 'forms', bytes: new Uint8Array(await response.arrayBuffer()) };
};

@Component({
  selector: 'demo-root',
  imports: [
    NgTemplateOutlet,
    EpdfDocumentGate,
    EpdfStage,
    EpdfPageTemplate,
    EpdfRenderLayer,
    EpdfFormLayer,
    EpdfFieldControlTemplate,
  ],
  providers: [
    provideEmbedPdf(
      { engine: () => localEngine(), initialDocuments: [{ source: forms }] },
      withStage(),
      withRender(),
      withInteraction(),
      withForm(),
    ),
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  styleUrl: './clear-button.css',
  encapsulation: ViewEncapsulation.None,
  template: `
    <ng-container *epdfDocumentGate="let document; fallback: loading">
      <div class="toolbar">
        <button type="button" class="button" (click)="fillIn()">Fill in</button>
        <button type="button" class="button" (click)="stage.zoomIn()">Zoom in</button>
        <button type="button" class="button" (click)="stage.rotateViewBy(90)">Turn the view</button>
      </div>
      <epdf-stage #stage="epdfStage" class="stage">
        <ng-template epdfPage>
          <epdf-render-layer />
          <epdf-form-layer>
            <!-- The one-line text boxes get a button at their end that empties them; the
                 layer draws every other box. The outlet draws the layer's own text box: the
                 field as the PDF draws it, and the editor people type in. The button is
                 yours. -->
            <ng-template
              [epdfFieldControl]="isTextBox"
              let-widget
              let-native="native"
              let-setValue="setValue"
            >
              <ng-container [ngTemplateOutlet]="native" />
              @if (widget.control === 'text' && !widget.disabled && widget.value !== '') {
                <!-- Tab goes to the text box. -->
                <button
                  type="button"
                  class="clear-button"
                  [attr.aria-label]="'Clear ' + widget.label"
                  tabindex="-1"
                  (click)="clear(widget, setValue)"
                ></button>
              }
            </ng-template>
          </epdf-form-layer>
        </ng-template>
      </epdf-stage>
    </ng-container>

    <ng-template #loading><p class="loading">Loading…</p></ng-template>
  `,
})
export class App {
  private readonly form = inject(EpdfForm);

  protected readonly isTextBox = (widget: FormWidgetItem) =>
    widget.control === 'text' && !widget.multiline;

  /** Empty the box, then put the cursor back in it, ready for the new value. */
  protected clear(widget: FormWidgetItem, setValue: EpdfFieldControlTemplateContext['setValue']) {
    void setValue({ value: '' }).then(() => this.form.focus(widget.fieldRef));
  }

  protected fillIn() {
    void this.form.importValues({
      name: 'Ada Lovelace',
      email: 'ada@example.com',
      company: 'Lanternfield',
    });
  }
}

Try it: press Fill in, or type in a text box and press Tab: a button at the end of the box empties it, and leaves the cursor in the box. The box is still the layer’s own: it shows the field as the PDF draws it, you type in it, and Tab moves on to the next field, past the button.

What you draw in the frame zooms with the page. Open a menu or a picker from it as a card, with anchored UI or your own popover.

The cursor and Tab#

Tab, focus() and focusNext() put the cursor in the first element of your control that takes focus: an <input>, a <button>, or an element with tabindex="0". Give it one, and the field takes part in the form’s order like any other. While the cursor is anywhere inside your control, the field is the focused field.

A box that is disabled takes no input: draw it, and leave out what people would click.

What a box tells you#

Each box says where it is, what it holds, and whose it is. listWidgets(page) gives the boxes of a page, and a control gets its box as widget:

ValueWhat it is
controlWhich control it is: 'text', 'toggle', 'choice', 'button' or 'signature'
fieldRefThe field it belongs to: what setValue() and get() take
refThe box’s own ref: what form.widgetAnchorOf() and getWidget() take
labelIts name for screen readers: the field’s tooltip, else its name
groupIdThe group the field is in, or null
canFillWhether this user may fill it in, or sign it for a signature field: their permissions alone
disabledDraw it, take no input: a read-only field, one this user may not fill in, one fillable leaves out, or a value on its way
requiredThe form asks for a value
emptyIt holds nothing yet: no text, nothing ticked or picked, no signature. What validate() counts as missing
box, rotation, rectWhere it is; see Turned fields

Then each control has its value: a text box its value, a checkbox or radio button checked and its exportValue, a dropdown or list its options and selected, a signature box signed.

Good to know#

Design mode#

Controls fill forms in. They show while the active tool fills forms, as the layer’s own do. In design mode fields are boxes people select and move, drawn by the annotation layer, and your controls stand down with the layer’s own.

Signature fields#

An empty signature field’s control is a button. A click makes the field the target, where the next mark goes. Your own control for it calls signature.setTarget(widget.fieldRef), or opens your own signing UI.

The list a dropdown opens is the browser’s own, so it opens unturned and at the browser’s size. For a list in your app’s look, draw the dropdown as a control and open your list as anchored UI.

Drawing every field yourself#

A control replaces one box inside the form layer, which still places it, turns it, and moves the cursor between fields. To draw a form without the layer, listWidgets(page) gives you every box with these same values.

Was this page helpful?

Your feedback goes directly to the documentation team.