介绍
Aspose.PDF FOSS for TypeScript 是一个用于在 Node.js 代码中处理 PDF 文档的新开源库。它在 MIT 许可证下发布,并以 @asposefoss/pdf 包的形式提供,运行时仅需要 Node.js (>=22) —— 没有其他运行时依赖。
该库围绕 Document 和 Page 类组织。Document.OpenFile() 和 Document.Open() 从磁盘或内存字节加载 PDF,Document.New() 则创建一个空白文档;doc.Pages 中的每一页都提供添加文本、图像、注释和 AcroForm 字段的方法,同时可以将自身转换为其他格式。此后,API 包括页面的拆分、合并和重新排序,将文档转换为 HTML、Markdown、DOCX、SVG 和栅格图像,添加和扁平化注释,构建交互式表单,以及对结果进行加密、签名或脱敏。
此版本面向需要在 Node.js 或 TypeScript 代码库中进行 PDF 处理且不具备商业许可证的开发者:文档组装流水线、将 PDF 渲染为 Web 可用格式的出版工具、表单填充自动化,以及在分发前需要对敏感 PDF 进行脱敏或加密的服务。该项目当前版本为 0.1.0,正处于积极的早期开发阶段,因此 API 和功能覆盖可能在正式版发布前发生变化。
包含内容
文档和页面管理
Document.OpenFile() / Document.Open() 从磁盘或内存中的 Uint8Array 加载 PDF;Document.New() 创建一个空白文档。页面可以使用 Document.Split() 拆分为独立文档,使用 Document.ExtractPages() 提取,使用 Document.Merge() 合并,或使用 Document.Append() 追加到已有文档。Document.WriteTo() 将结果写回,可选择压缩。
import { Document } from '@asposefoss/pdf';
// Open from disk (or Document.Open(uint8array) for in-memory data)
const doc = Document.OpenFile('input.pdf');
// Inspect and edit pages
const pages = doc.Pages;
console.log(pages.length);
pages[0].Rotate = 90;
doc.RemovePage(2); // 1-based page number
doc.Reorder([3, 1, 2]);
// Save
doc.WriteTo('output.pdf'); // or: const bytes = doc.Save();
格式转换
整个文档使用 Document.ToHtml() 渲染为 HTML,并使用 Document.ToMarkdown() 渲染为 GitHub 风格的 Markdown。单独的页面使用 Page.ToSvg() 渲染为独立的 SVG,或使用 Page.ToImage() 渲染为栅格 PNG 字节。Document.ToDocx() / Page.ToDocx() 生成可重排或固定布局的 .docx 输出。
import { Document } from '@asposefoss/pdf';
const doc = Document.OpenFile('in.pdf');
const png = doc.Pages[0].ToImage({ scale: 2 }); // Uint8Array of PNG bytes @144 DPI
const html = doc.ToHtml(); // standalone semantic HTML, all pages
const md = doc.ToMarkdown(); // GFM Markdown, all pages
const docx = doc.ToDocx(); // .docx bytes, reflowed, images in the package
注释
每个 Page 直接公开注释方法:Page.AddHighlight()、Page.AddUnderline()、Page.AddSquiggly() 和 Page.AddStrikeOut() 用于添加文本标记;Page.AddTextNote()、Page.AddFreeText() 和 Page.AddStamp() 用于添加便签、独立文本和橡皮图章注释。诸如 MarkupAnnotation 和 StampAnnotation 的类型子类公开一个 Flatten() 方法,以将注释烘焙到页面内容中。
AcroForms
Form 类直接构建交互式字段:AddTextField()、AddCheckbox()、AddRadioGroup()、AddComboBox()、AddListBox() 和 AddPushButton() 每个都返回一个特定类型的 Field 子类。对字段调用 SetStyle() 或 GenerateAppearance() 可刷新其渲染外观,而 Document.FlattenForm() 则将每个字段的当前值烘焙为静态页面内容。
function addFormFields(doc: Document, page: Page): void {
const form = doc.Form;
const pageNum = page.Number;
// Text field
form.AddTextField({
page: pageNum, rect: [200, 670, 450, 690], name: 'FullName', value: 'Alice Sample',
});
// Checkbox
form.AddCheckbox({
page: pageNum, rect: [200, 630, 218, 648], name: 'Subscribe',
checked: true,
});
}
安全与编辑
Document.WriteTo() 接受一个支持 AES-128、AES-256 或 RC4 的 encrypt 选项,并提供独立的用户密码和所有者密码以及细粒度的 Permissions。可以使用 Document.Redact()、Document.RedactText() 和 Page.ApplyRedactions() 永久删除敏感区域或匹配的文本,这些通过 RedactOptions 配置。
import { Document } from '@asposefoss/pdf';
const doc = Document.OpenFile('in.pdf');
doc.WriteTo('locked.pdf', {
encrypt: {
userPassword: 'open-me', // required to open (default '')
ownerPassword: 'full-rights', // default: same as userPassword
algorithm: 'aes256', // 'aes256' (default) | 'aes128' | 'rc4'
permissions: { copying: false, modifying: false },
encryptMetadata: true,
},
});
快速入门
安装软件包后,打开已有的 PDF,编辑其页面和元数据,然后保存结果:
git clone https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-TypeScript.git
cd Aspose.PDF-FOSS-for-TypeScript
npm install
npm run buildimport { Document } from '@asposefoss/pdf';
// Open from disk (or Document.Open(uint8array) for in-memory data)
const doc = Document.OpenFile('input.pdf');
// Inspect and edit pages
const pages = doc.Pages;
console.log(pages.length);
pages[0].Rotate = 90;
doc.RemovePage(2); // 1-based page number
doc.Reorder([3, 1, 2]);
// Metadata
doc.SetMetadata({ title: 'Report', author: 'Jane', custom: { Dept: 'R&D' } });
// Save
doc.WriteTo('output.pdf'); // or: const bytes = doc.Save();
doc.WriteTo('small.pdf', { compressed: true }); // xref stream + object streams
支持的格式
| 格式 | 扩展名 | 读取 | 写入 |
|---|---|---|---|
| ✓ | ✓ | ||
| Markdown | md | ✓ | ✓ |
| SVG | svg | ✓ | ✓ |
| TIFF | tiff | ✓ | ✓ |
| DOCX | docx | — | ✓ |
| HTML | html | — | ✓ |
| PNG | png | — | ✓ |
| EPUB | epub | — | ✓ |
开源与许可
Aspose.PDF FOSS for TypeScript 在 MIT 许可证下发布,源码已发布在 GitHub。没有评估水印、使用限制或需要管理的单独许可文件,且该库可在商业产品中免费使用,无需版税。
该软件包当前版本为 0.1.0,表明正处于活跃的早期开发阶段。Node.js (>=22) 是唯一的运行时要求,且该软件包没有其他第三方依赖。