# PDF 编辑器 — 开发者库

基于 PDFium（WebAssembly）的 PDF 编辑：在保持版式的前提下原地修改文字，添加文字、图片、高亮和手绘，遮盖内容，旋转、删除、重排和合并页面。

## 安装

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

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

## 用法

```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)`

从字节打开 PDF 并返回文档。

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

创建空白文档（默认 A4）。

### `PdfEditor.ready()`

提前加载 PDFium。

### `PdfEditor.setup(o)`

设置引擎和字体的地址。

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

从字节添加用于新文字的字体。

### `PdfEditor.pdfFamilies()`

可用于文字的字体家族。

### `PdfEditor.A4`

A4 页面尺寸（磅）。

### `PdfEditor.APP`

库的名称、版本和发布日期：{name, version, created}。

### `PdfEditor.PdfDocument()`

已打开文档的类（open 和 create 返回的对象）。

### `PdfEditor.SUBSTITUTE`

当文档自带字体无法编辑时代替使用的字体：sans, serif, mono → DejaVu。

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

按指定语言的颜色名称查找命名颜色（“深蓝色”→ #00008b）。

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

用于新文本的某字体族和样式的字体，已加载并可嵌入 PDF。

### `PdfEditor.listNamedColors(lang)`

列出命名颜色及其在指定语言中的名称。

### `PdfEditor.namedColorTexts(lang)`

指定语言的颜色字段标签（取色器的文本），缺少该语言时为英文。

### `PdfEditor.pdfTextFontData`

PDF 编辑器文本和字体区块的数据（来自 font-forge，仅限可嵌入 PDF 的字体）。

### `PdfEditor.textFontData`

文本和字体区块的数据：字体列表、加载字形样式、添加字体文件、某种语言的区块标签以及默认值。

### `doc.pageCount`

页数。

### `doc.pageSize(i)`

页面宽度和高度（磅）。

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

按比例绘制页面：{width, height, data}。

### `doc.paragraphs(i)`

页面上的文字段落及其位置和字体。

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

页面上某点下方的段落。

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

替换段落文字，保留其位置和换行。

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

在某点添加文字。

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

在矩形中插入图片。

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

高亮矩形区域。

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

添加手绘笔迹。

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

涂盖矩形并删除其下的内容。

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

按 90 度为单位旋转页面。

### `doc.deletePage(i)`

删除页面。

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

移动页面。

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

插入空白页。

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

添加另一个 PDF 的页面。

### `doc.save()`

以字节返回文档。

### `doc.close()`

释放内存。

### `doc.annotationCount(i)`

页面上注释（高亮、绘图）的数量。

### `doc.duplicatePage(i)`

在页面之后紧接着插入其副本。

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

将另一个已打开文档的页面（全部或指定索引）复制到某个位置。

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

页面空间中的矩形 {x0, y0, x1, y1} → 渲染页面上的矩形 {left, top, width, height}。

### `doc.textObjects(i)`

页面上的所有文本对象及其文本、位置、大小和字体。

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

渲染页面上的点 → 页面空间中的点（磅，y 向上）。

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

页面空间中的点 → 渲染页面上的点。

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

按字体的真实字形宽度把文本分成能放入指定宽度的行。

## 依赖

- @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
- 打开应用: https://openlibs.dev/zh-Hans/app/pdf-editor/
