介绍
在 Aspose.PDF FOSS 中针对 TypeScript 的每项功能都通过两个类实现:Document 和 Page。Document 管理页面集合以及所有文档范围的内容——元数据、书签、页面标签以及保存/加载——而 doc.Pages 中的每个 Page 则负责该页范围内的所有内容:内容、批注和表单字段。一旦熟悉了这两个类,库的其余功能——转换、批注、表单、安全——实际上只是这两个对象上更多的方法。
本文更深入地审视了 介绍性文章 仅略作提及的核心功能的几个部分:从其他文档组装文档、从零创建页面、添加用于导航的书签树以及为可访问性标记内容。这四项都是普通的方法调用——无需单独导入模块,也不需要除 @asposefoss/pdf 包本身之外的额外依赖。
这些操作在 PDF 流程超出“打开一个文件、修改、保存”之后会出现:将来自多个来源的报告合并为一个文档、以编程方式生成页面而不是从模板开始,以及使输出既可导航又可访问,而不仅仅是视觉上正确。
包含内容
组装与重新组织文档
Document.Split() 将文档按页顺序拆分为每页一个全新的单页 Document。Document.ExtractPages() 按给定的 1 起始页码集合复制到一个新的独立 Document,顺序保持不变——同一页可以重复。也可以将一个文档的页面复制到另一个文档:Document.Append() 将源文档的所有页面复制到目标文档的末尾,Document.InsertPage() 在指定的 1 起始位置跨文档复制单页。
import { Document } from '@asposefoss/pdf';
const report = Document.OpenFile('report.pdf');
const parts = report.Split(); // one Document per page
const chapter = report.ExtractPages([3, 4, 5]); // subset (1-based, repeats allowed) as a new Document
const cover = Document.OpenFile('cover.pdf');
report.InsertPage(1, cover.Pages[0]); // copy a single page across documents
report.Append(chapter); // copy chapter's pages onto report
report.WriteTo('assembled.pdf');
也可以通过一次调用使用 Document.Merge() 将多个文档合并为一个,该调用会按照顺序从每个传入文档的每页副本构建一个新的 Document。
从头创建页面
Document.New() 从空白创建文档:如果省略格式则为零页,传入格式时则创建一页给定 PageFormat 的空白页。额外的空白页来源于 Document.AddPage(),它也接受一个 PageFormat(或现有的 Page 以复制其尺寸)。文本使用 Page.AddText() 绘制到页面上,定位于 PDF 用户空间中的 (x, y) 点。
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 阅读器在页面旁显示的书签面板)使用 Document.GetOutlines() 读取,并整体替换为 Document.SetOutlines(),两者都使用 OutlineItem 值的树结构。每个 OutlineItem 包含一个 Title、一个 Dest 目标,并可选地包含用于嵌套条目的 Children,以及诸如 Open(默认展开)和 Bold 等显示提示。
import { Document, OutlineItem } from '@asposefoss/pdf';
const items: OutlineItem[] = [
{ Title: 'Introduction', Dest: { name: 'intro' } },
{
Title: 'Chapters',
Open: true,
Children: [
{ Title: 'Chapter 1', Dest: { name: 'ch1' } },
{ Title: 'Chapter 2', Dest: { name: 'ch2' } },
],
},
];
doc.SetOutlines(items);
以这种方式命名的目标 ({ name: 'intro' }) 通过文档的命名目标表解析,当书签需要指向未绑定到页面内容的目标时,该表由 Document.GetNamedDestinations() 和 Document.SetNamedDestination() 直接管理。
结构化文本与可访问性标记
Document.GetStructTree() 返回文档的逻辑结构树(当文档未标记时返回 null),Document.CreateStructTree() 构建该树,并将文档标记为 Tagged。结构树存在后,Page.GetStructuredText() 将页面文本以 TextBlock 值的形式返回——这些已定位的片段已按行和段落式块分组——这就是手动标记过程的输入:遍历这些块,判断哪些是标题而哪些是正文,并将相应的元素追加到树中。
function handTagPage(doc: Document, page: Page, heading: string): void {
const root = doc.GetStructTree();
if (!root) throw new Error('handTagPage: the document is not tagged yet');
for (const block of page.GetStructuredText()) {
const text = block.text.trim();
if (text.length === 0) continue;
const el = root.Append(text.startsWith(heading) ? 'H2' : 'P');
el.MarkContent(page, block.quad);
}
}
标记结构树是下游功能构建的基础:语义 HTML 导出会从中重新流式布局,而不是回退到普通的启发式布局,且 Document.ValidatePdfUa() 会根据依赖标签存在的 PDF/UA 可访问性标准的子集检查文档。
快速开始
安装软件包后,打开文档,提取并重新组合其部分页面,然后保存结果:
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('report.pdf');
const chapter = doc.ExtractPages([3, 4, 5]); // subset (1-based, repeats allowed) as a new Document
const cover = Document.OpenFile('cover.pdf');
doc.InsertPage(1, cover.Pages[0]); // copy a single page across documents
doc.Append(chapter); // copy chapter's pages onto doc
doc.WriteTo('assembled.pdf');
支持的格式
| 格式 | 扩展名 | 读取 | 写入 |
|---|---|---|---|
| ✓ | ✓ | ||
| 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) 是唯一的运行时要求,且该软件包没有其他第三方依赖。