mirror of
https://github.com/foliojs/pdfkit.git
synced 2025-12-08 20:15:54 +00:00
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
class PDFOutline {
|
|
constructor(document, parent, title, dest, options = { expanded: false }) {
|
|
this.document = document;
|
|
this.options = options;
|
|
this.outlineData = {};
|
|
|
|
if (dest !== null) {
|
|
this.outlineData['Dest'] = [dest.dictionary, 'Fit'];
|
|
}
|
|
|
|
if (parent !== null) {
|
|
this.outlineData['Parent'] = parent;
|
|
}
|
|
|
|
if (title !== null) {
|
|
this.outlineData['Title'] = new String(title);
|
|
}
|
|
|
|
this.dictionary = this.document.ref(this.outlineData);
|
|
this.children = [];
|
|
}
|
|
|
|
addItem(title, options = { expanded: false }) {
|
|
const result = new PDFOutline(
|
|
this.document,
|
|
this.dictionary,
|
|
title,
|
|
this.document.page,
|
|
options,
|
|
);
|
|
this.children.push(result);
|
|
|
|
return result;
|
|
}
|
|
|
|
endOutline() {
|
|
if (this.children.length > 0) {
|
|
if (this.options.expanded) {
|
|
this.outlineData.Count = this.children.length;
|
|
}
|
|
|
|
const first = this.children[0],
|
|
last = this.children[this.children.length - 1];
|
|
this.outlineData.First = first.dictionary;
|
|
this.outlineData.Last = last.dictionary;
|
|
|
|
for (let i = 0, len = this.children.length; i < len; i++) {
|
|
const child = this.children[i];
|
|
if (i > 0) {
|
|
child.outlineData.Prev = this.children[i - 1].dictionary;
|
|
}
|
|
if (i < this.children.length - 1) {
|
|
child.outlineData.Next = this.children[i + 1].dictionary;
|
|
}
|
|
child.endOutline();
|
|
}
|
|
}
|
|
|
|
return this.dictionary.end();
|
|
}
|
|
}
|
|
|
|
export default PDFOutline;
|