# PDF editor — library for developers

PDF editing on PDFium (WebAssembly): change text in place keeping the layout, add text, pictures, highlights and drawings, hide fragments, rotate, delete, reorder and merge pages.

## Installation

```sh
npm install @openlibsdev/pdf-editor
```

```js
import PdfEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/pdf-editor@1.0.0/+esm";
```

## Usage

```js
import PdfEditor from "@openlibsdev/pdf-editor";

const doc = await PdfEditor.create();               // or await PdfEditor.open(bytes)
await doc.addText(0, 72, 770, "Hello, PDF", { family: "DejaVu Sans", size: 18 });
const page = doc.render(0, 1.5);                    // { width, height, data: RGBA }
const bytes = doc.save();                           // Uint8Array
doc.close();
```

## API

### `PdfEditor.open(bytes)`

Opens a PDF from bytes and returns a document.

### `PdfEditor.create(width, height)`

Creates an empty document (A4 by default).

### `PdfEditor.ready()`

Loads PDFium in advance.

### `PdfEditor.setup(o)`

Sets the addresses of the engine and fonts.

### `PdfEditor.addFontBytes(name, bytes)`

Adds a font from bytes for new text.

### `PdfEditor.pdfFamilies()`

Font families available for text.

### `PdfEditor.A4`

A4 page size in points.

### `PdfEditor.APP`

Name, version and release date of the library: {name, version, created}.

### `PdfEditor.PdfDocument()`

The class of an open document (what open and create return).

### `PdfEditor.SUBSTITUTE`

Fonts used instead of the document's own when they cannot be edited: sans, serif, mono → DejaVu.

### `PdfEditor.findNamedColor(text, lang)`

Finds a named color by its name in the given language ("dark blue" → #00008b).

### `PdfEditor.fontFor(family, bold, italic)`

Font for new text in a family and style, loaded and ready to embed in the PDF.

### `PdfEditor.listNamedColors(lang)`

Lists the named colors with their names in the given language.

### `PdfEditor.namedColorTexts(lang)`

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

### `PdfEditor.pdfTextFontData`

Data for the text and font block of the PDF editor (font-forge's, limited to fonts that can be embedded in a PDF).

### `PdfEditor.textFontData`

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

### `doc.pageCount`

Number of pages.

### `doc.pageSize(i)`

Page width and height in points.

### `doc.render(i, scale)`

Draws a page at a scale: {width, height, data}.

### `doc.paragraphs(i)`

Paragraphs of text on a page with their position and font.

### `doc.paragraphAt(i, x, y)`

Paragraph under a point of the page.

### `doc.editParagraph(i, para, newText, style)`

Replaces the text of a paragraph, keeping its place and line breaks.

### `doc.addText(i, x, yTop, text, style)`

Adds text at a point.

### `doc.insertImage(i, img, r)`

Inserts a picture into a rectangle.

### `doc.highlight(i, r, color)`

Highlights a rectangle.

### `doc.addInk(i, strokes, color, width)`

Adds a freehand drawing.

### `doc.redact(i, r, fill)`

Paints over a rectangle and removes what was under it.

### `doc.rotatePage(i, quarterTurns)`

Rotates a page by quarter turns.

### `doc.deletePage(i)`

Deletes a page.

### `doc.movePage(from, to)`

Moves a page.

### `doc.insertBlankPage(at, width, height)`

Inserts an empty page.

### `doc.merge(bytes, at)`

Adds the pages of another PDF.

### `doc.save()`

Returns the document as bytes.

### `doc.close()`

Frees the memory.

### `doc.annotationCount(i)`

Number of annotations (highlights, drawings) on a page.

### `doc.duplicatePage(i)`

Inserts a copy of a page right after it.

### `doc.importPages(src, at, indexes)`

Copies pages of another open document (all, or the given indexes) to a position.

### `doc.rectToView(i, view, r)`

Page-space rectangle {x0, y0, x1, y1} → rectangle on the rendered page {left, top, width, height}.

### `doc.textObjects(i)`

Every text object of a page with its text, position, size and font.

### `doc.toPage(i, view, x, y)`

Point on the rendered page → point in page space (points, y up).

### `doc.toView(i, view, x, y)`

Point in page space → point on the rendered page.

### `doc.wrap(font, size, text, maxWidth)`

Splits text into lines that fit a width, using the real glyph widths of the font.

## Dependencies

- @embedpdf/pdfium 2.15.1 (MIT)
- @embedpdf/pdfium 2.15.1 (MIT)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- imagetracerjs 1.2.6 (Unlicense)
- jspdf 2.5.2 (MIT)

- GitHub: https://github.com/OpenLibsDev/pdf-editor
- npm: https://www.npmjs.com/package/@openlibsdev/pdf-editor
- Open the app: https://openlibs.dev/en/app/pdf-editor/
