Rich text
A text box (a free text annotation) can mix styles: a bold word, a red sentence, a bigger heading.
Double-click the box below, select a word, and use the buttons, or press Ctrl/Cmd with B, I
or U while you type:
Loading live preview…
import {
ChangeDetectionStrategy,
Component,
computed,
effect,
inject,
untracked,
viewChild,
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 { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection';
import { withInteraction } from '@embedpdf/angular/interaction';
import { EpdfAnnotation, EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation';
import { localEngine } from '@embedpdf/engine';
const notice = async (): Promise<OpenInput> => {
const response = await fetch('/pdfs/notice.pdf');
return { kind: 'bytes', id: 'notice', bytes: new Uint8Array(await response.arrayBuffer()) };
};
type Format = 'bold' | 'italic' | 'underline';
const FORMATS: readonly Format[] = ['bold', 'italic', 'underline'];
// The same calls style the selected words while typing, and the whole box otherwise.
@Component({
selector: 'demo-rich-text-toolbar',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="toolbar">
@for (format of formats; track format) {
<button
type="button"
[class]="'button ' + format"
[attr.aria-pressed]="isOn(format)"
[disabled]="!hasText()"
(click)="annotation.text.toggleFormat(format)"
>
{{ format[0]!.toUpperCase() }}
</button>
}
<button
type="button"
class="button"
[disabled]="!hasText()"
(click)="annotation.selection.update({ fontColor: '#c00000' })"
>
Red
</button>
<button
type="button"
class="button"
[disabled]="!hasText()"
(click)="annotation.selection.update({ fontSize: large() ? 16 : 24 })"
>
{{ large() ? '16 pt' : '24 pt' }}
</button>
<p class="hint">Ctrl or Cmd with B, I or U works while you type</p>
</div>
`,
})
export class RichTextToolbar {
protected readonly annotation = inject(EpdfAnnotation);
protected readonly formats = FORMATS;
private readonly panel = this.annotation.selection.properties;
protected readonly hasText = computed(() =>
this.panel().properties.some((property) => property.control === 'textFormat'),
);
protected readonly large = computed(() => this.panel().values.fontSize === 24);
// On when every selected word has it; `mixed` lists what the words disagree on.
protected isOn(format: Format) {
const { values, mixed } = this.panel();
return values[format] === true && !mixed.includes(format);
}
}
@Component({
selector: 'demo-root',
imports: [
EpdfDocumentGate,
EpdfStage,
EpdfPageTemplate,
EpdfRenderLayer,
EpdfSelectionLayer,
EpdfAnnotationLayer,
RichTextToolbar,
],
providers: [
provideEmbedPdf(
{ engine: () => localEngine(), initialDocuments: [{ source: notice }] },
withStage(),
withRender(),
withInteraction(),
withSelection(),
withAnnotation(),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './rich-text.css',
encapsulation: ViewEncapsulation.None,
template: `
<ng-container *epdfDocumentGate="let document; fallback: loading">
<demo-rich-text-toolbar />
<epdf-stage class="stage">
<ng-template epdfPage>
<epdf-render-layer />
<epdf-selection-layer />
<epdf-annotation-layer />
</ng-template>
</epdf-stage>
</ng-container>
<ng-template #loading><p class="loading">Loading…</p></ng-template>
`,
})
export class App {
private readonly annotation = inject(EpdfAnnotation);
private readonly stage = viewChild(EpdfStage);
private done = false;
constructor() {
// On load: the document's rich text box, selected.
effect(() => {
const stage = this.stage();
if (!stage || this.annotation.status() !== 'ready' || this.done) return;
this.done = true;
untracked(() => {
const richText = this.annotation.list().find((each) => each.nm === 'rich-text');
if (!richText) return;
this.annotation.selection.set([richText.ref]);
stage.reveal(richText.page, { rect: richText.rect });
});
});
}
}How it’s stored#
A text box has a body style, which is how its text looks by default, and paragraphs of
runs. A run is a piece of text with the styles it changes, and nothing else: a bold word in a
regular box is one run with { weight: 700 }.
annotation.richText;
// {
// body: { family: 'Helvetica', size: 12, color: '#000000' },
// paragraphs: [{ runs: [{ text: 'Paid ' }, { text: 'in full', style: { weight: 700 } }] }],
// }The engine writes it the way Acrobat does, so the formatting is the same there. The engine docs have the full model: Free text.
Format from your toolbar#
The same calls work while someone types in a box and when boxes are only selected:
await annotation.selection.update({ bold: true });
await annotation.selection.update({ fontSize: 24, fontColor: '#c00000' });
await annotation.text.toggleFormat('italic'); // italic on, or off if all of it already isWhere a change lands depends on what’s selected:
- Words selected while typing: the change styles those words.
- A caret while typing: the change is for the text typed next, as in a word processor. Nothing changes until someone types, and moving the caret drops it.
- Otherwise: it styles all the text of every selected box. A word with its own style follows the change: turn the box red, and a blue word turns red too.
A property that isn’t about text, such as opacity or the border, always applies to the whole box.
To show the state in your toolbar, annotation.selection.properties()
reports what the next change would style:
- Words selected:
values.boldis true when every selected word is bold, andmixedlists what the words disagree on. - A caret: the style of the text before it (at the start of a line, the text after it), with the changes made at the caret since.
- A selected box: all of its text, the way selected words read.
Text boxes also give you textFormat properties, so a style panel shows the bold, italic and
underline buttons without extra code.
The engine’s own update keeps the PDF’s rule: fontColor, fontSize and fontFamily change the
box’s body style, and a word with its own style keeps it. See
Free text.
When it’s saved#
Typing is written after a short pause, at every format change, and when editing ends, so the engine
never gets half a word. documents.download() waits for the last write, so the file you download has
everything the user typed.
Show your own fonts#
The engine puts a font into the PDF, and the browser draws the text while people type, in the same
font: the fonts plugin (withFonts(), from the fonts entry) hands the browser the file the
engine writes with. Register the font with the engine, in your engine function,
which runs only in the browser:
import { localEngine } from '@embedpdf/engine';
/** `provideEmbedPdf({ engine }, …)`: runs in the browser, before the first document opens. */
export const engine = async () => {
const pdfEngine = localEngine();
const data = await fetch('/fonts/brand-sans.ttf').then((response) => response.arrayBuffer());
await pdfEngine.fonts.register({ key: 'brand-sans', data });
return pdfEngine;
};The key is the font’s name: fontFamily: 'brand-sans' on a text box. The standard PDF fonts need
nothing: they show in the engine’s own faces. Without the fonts plugin, a box shows in its standard
font, or in Helvetica. More in Fonts.
Draw your own text box#
The annotation layer’s text box already edits rich text. If you draw text boxes
yourself, [epdfRichTextEditor] makes your element the editor:
it draws the runs, turns typing back into runs, keeps the caret when the style changes, and handles
pasting, input methods and the format shortcuts:
import { Component } from '@angular/core';
import {
EpdfAnnotationLayer,
EpdfAnnotationTemplate,
EpdfRichTextEditor,
type Annotation,
} from '@embedpdf/angular/annotation';
@Component({
selector: 'app-annotations',
imports: [EpdfAnnotationLayer, EpdfAnnotationTemplate, EpdfRichTextEditor],
template: `
<epdf-annotation-layer>
<ng-template [epdfAnnotation]="isTextBox" let-annotation>
<!-- The box fills its frame (width and height 100% in your CSS) and draws it; the
editor inside fills the box. -->
<div class="text-box" [class.editing]="editor.editing()">
<div [epdfRichTextEditor]="annotation" #editor="epdfRichTextEditor"></div>
</div>
</ng-template>
</epdf-annotation-layer>
`,
})
export class Annotations {
protected readonly isTextBox = (annotation: Annotation) => annotation.subtype === 'free-text';
}Loading live preview…
import {
ChangeDetectionStrategy,
Component,
computed,
effect,
inject,
untracked,
viewChild,
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 { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection';
import { withInteraction } from '@embedpdf/angular/interaction';
import {
EpdfAnnotation,
EpdfAnnotationLayer,
EpdfAnnotationTemplate,
EpdfRichTextEditor,
withAnnotation,
type Annotation,
} from '@embedpdf/angular/annotation';
import { localEngine } from '@embedpdf/engine';
const notice = async (): Promise<OpenInput> => {
const response = await fetch('/pdfs/notice.pdf');
return { kind: 'bytes', id: 'notice', bytes: new Uint8Array(await response.arrayBuffer()) };
};
const FORMATS = ['bold', 'italic', 'underline'] as const;
// The formatting calls work on your element as they do on the built-in one.
@Component({
selector: 'demo-format-buttons',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="toolbar">
@for (format of formats; track format) {
<button
type="button"
[class]="'button ' + format"
[disabled]="!hasText()"
(click)="annotation.text.toggleFormat(format)"
>
{{ format[0]!.toUpperCase() }}
</button>
}
</div>
`,
})
export class FormatButtons {
protected readonly annotation = inject(EpdfAnnotation);
protected readonly formats = FORMATS;
protected readonly hasText = computed(() =>
this.annotation.selected().some((selected) => selected.subtype === 'free-text'),
);
}
@Component({
selector: 'demo-root',
imports: [
EpdfDocumentGate,
EpdfStage,
EpdfPageTemplate,
EpdfRenderLayer,
EpdfSelectionLayer,
EpdfAnnotationLayer,
EpdfAnnotationTemplate,
EpdfRichTextEditor,
FormatButtons,
],
providers: [
provideEmbedPdf(
{ engine: () => localEngine(), initialDocuments: [{ source: notice }] },
withStage(),
withRender(),
withInteraction(),
withSelection(),
withAnnotation(),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './branded-text-box.css',
encapsulation: ViewEncapsulation.None,
template: `
<ng-container *epdfDocumentGate="let document; fallback: loading">
<demo-format-buttons />
<epdf-stage class="stage">
<ng-template epdfPage>
<epdf-render-layer />
<epdf-selection-layer />
<epdf-annotation-layer>
<!-- Your own text box, in your app's look: it fills its frame, which the layer
places and turns like the text box. The element inside is the editor and
fills the box, so the box draws the fill, border and padding. -->
<ng-template [epdfAnnotation]="isTextBox" let-annotation>
<div class="text-box" [class.editing]="editor.editing()">
<div [epdfRichTextEditor]="annotation" #editor="epdfRichTextEditor"></div>
</div>
</ng-template>
</epdf-annotation-layer>
</ng-template>
</epdf-stage>
</ng-container>
<ng-template #loading><p class="loading">Loading…</p></ng-template>
`,
})
export class App {
private readonly annotation = inject(EpdfAnnotation);
private readonly stage = viewChild(EpdfStage);
private done = false;
// Kept in a field, the same function on every page: the layer registers the look once.
protected readonly isTextBox = (annotation: Annotation) => annotation.subtype === 'free-text';
constructor() {
// On load: the document's text box, selected, drawn by your component.
effect(() => {
const stage = this.stage();
if (!stage || this.annotation.status() !== 'ready' || this.done) return;
this.done = true;
untracked(() => {
const textBox = this.annotation.list().find((each) => each.nm === 'text-box');
if (!textBox) return;
this.annotation.selection.set([textBox.ref]);
stage.reveal(textBox.page, { rect: textBox.rect });
});
});
}
}Try it: double-click the box and type. The buttons format the selected words, as on the built-in box.
The directive gives your element the box’s body style: font, size, color and alignment. Its
editing() is true while someone types in the box. It also makes the element fill its parent: the
text is laid out at 16 px or more and drawn back at the box’s size, so iOS doesn’t zoom the page in
when the box takes focus. Draw the box’s fill, border and padding on the parent, as above; anything
on the element itself would scale with the text. The formatting calls above work on your element as
they do on the built-in one.
Your feedback goes directly to the documentation team.