Aspose.PDF FOSS for TypeScript 是一个开源库,可在 Node.js (>=22) 应用程序中创建、编辑、转换和保护 PDF 文档。Document 和 Page 类是核心入口点:Document.OpenFile() / Document.Open() 用于加载已有文件或内存字节,Document.New() 用于创建空白文档,WriteTo() / Save() 将结果写回,可选使用 xref-stream 压缩。该项目是零依赖包(Node.js 是唯一的运行时要求),并处于积极的早期开发阶段(当前版本 0.1.0)。
除了核心文档和页面编辑——使用 Document.Split()、Document.Merge()、Document.ExtractPages() 和 Document.Append() 进行页面的拆分、合并、提取、重新排序和追加——库还可以通过 ToHtml()、ToMarkdown()、ToDocx()、ToSvg()、ToImage() 和 ToEpub() 将页面和整个文档转换为其他格式。注释支持高亮、下划线、波浪线、删除线、链接、文本注释、自由文本和印章标记,可通过 Page.AddHighlight()、Page.AddStamp() 等方法实现。AcroForm 字段(文本、复选框、单选框、下拉框、列表框和按钮)通过 Form 类及其类型化的 Field 子类构建,文档可使用 AES-128、AES-256 或 RC4 加密,使用 Document.Sign() 进行数字签名,使用 Document.Redact() / Page.ApplyRedactions() 进行编辑隐私处理。
Aspose.PDF FOSS for TypeScript 在 MIT 许可证下发布,无运行时费用或使用限制(请参阅下文的安装章节)。有关企业产品系列,请参阅 Aspose.PDF — Enterprise Product Family。
Document.OpenFile() / Document.Open(),或使用 Document.New() 创建空文档Document.Split()、Document.Merge()、Document.ExtractPages()、Document.Reorder() 和 Document.Append() 对页面进行拆分、合并、提取、重新排序和追加Document.SetMetadata() / Document.GetMetadata() 读取和更新文档元数据Document.WriteTo() 写出输出,可通过 xref 流和对象流进行可选压缩Document.ToHtml() 将整个文档渲染为 HTML,并使用 Document.ToMarkdown() 将其渲染为 GitHub 风格的 MarkdownDocument.ToDocx() / Page.ToDocx() 生成可重新流式或固定布局的 .docx 输出Page.ToSvg() 将单个页面渲染为独立的 SVG,或使用 Page.ToImage() 获取栅格 PNG/TIFF 字节Document.ToEpub() 导出为 EPUB,以供电子阅读器分发Page.AddHighlight()、Page.AddUnderline()、Page.AddSquiggly() 和 Page.AddStrikeOut()Page.AddTextNote() 和 Page.AddFreeText() 附加便签和独立文本Page.AddStamp() 放置橡皮图章注释,并使用 Page.AddLink() 添加可点击链接Annotation.Flatten() 方法(在 MarkupAnnotation、StampAnnotation 等具体子类型上公开)将任何注释展平为页面内容Form 类添加文本、复选框、单选框、组合框、列表框和推送按钮字段(AddTextField(), AddCheckbox(), AddRadioGroup(), AddComboBox(), AddListBox(), AddPushButton())TextField、ChoiceField 和 ButtonField 等类型字段类上使用 SetStyle() 和 GenerateAppearance() 设置样式并刷新外观Field 上使用 SetActions() 绑定字段行为Document.FlattenForm() 将完成的表单展平为静态页面内容Document.WriteTo() 的 encrypt 选项,并通过 AES-128、AES-256 或 RC4 对输出进行密码保护和加密,具备细粒度的 PermissionsDocument.Sign() 对文档进行数字签名,并使用 Document.VerifySignatures() 验证签名Document.AddDocumentTimestamp() 添加受信任的时间戳Document.Redact()、Document.RedactText() 和 Page.ApplyRedactions() 永久编辑敏感区域或匹配的文本打开 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';
const doc = Document.OpenFile('in.pdf');
const svg = doc.Pages[0].ToSvg(); // standalone <svg> string
// fs.writeFileSync('page1.svg', svg);
const png = doc.Pages[0].ToImage({ scale: 2 }); // Uint8Array of PNG bytes @144 DPI
// fs.writeFileSync('page1.png', png);
const html = doc.ToHtml(); // standalone semantic HTML, all pages
// fs.writeFileSync('out.html', html);
const md = doc.ToMarkdown(); // GFM Markdown, all pages
// fs.writeFileSync('out.md', md);
const docx = doc.ToDocx(); // .docx bytes, reflowed, images in the package
// fs.writeFileSync('out.docx', docx);
const fixed = doc.ToDocx({ mode: 'textbox' }); // .docx keeping each page's own geometry
// fs.writeFileSync('fixed.docx', fixed);
加载 PDF,编辑其页面和元数据,并使用压缩写回。
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
console.log(doc.Pages.length);
doc.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
创建一个空白文档,添加定位文本,然后追加另一页。
import { Document, PageFormat } from '@asposefoss/pdf';
const doc = Document.New(PageFormat.A4); // one blank A4 page
doc.Pages[0].AddText('Hello', 72, 720, { fontSize: 14 });
doc.AddPage(PageFormat.A4.landscape()); // append more as you go
doc.WriteTo('scratch.pdf');
使用用户/所有者密码对现有 PDF 进行 AES-256 加密,并限制复制和修改。
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,
},
});
Aspose.PDF FOSS for TypeScript 在 MIT 许可证下发布,允许在商业产品中免费使用且无需运行时费用。源代码已在 GitHub 上公开提供。
该软件包面向 Node.js 22 及更高版本,并且没有其他运行时依赖项。有关当前安装命令,请参见上面的安装部分。
该库可读取和写入 PDF、Markdown、SVG 和 TIFF,并且能够生成 DOCX、HTML、PNG 和 EPUB 输出。
使用 Document.New(PageFormat.A4) 生成一个空白文档,然后添加页面并使用 Document.WriteTo() 保存。
无需许可证密钥或激活步骤。该库在 MIT 许可证下免费完全无许可运行。