PDF Table of Contents#

Overview#

Not all PDF documents will have a Table of Contents, but for those that do there will be a non-nil array associated against the MuPDFDKDoc document instance as follows:

let toc:[ARDKTocEntry]? = session.doc.toc

Note

If there is no Table of Contents for the PDF then the toc array will simply be nil.

Table of Contents is also known as “Bookmarks” or “Outline”.

To understand each ARDKTocEntry entry object inside the delivered array, an application developer can loop the array to strip out the entries into a flat array for listing purposes. This is because ARDKTocEntry items can contain sub-nested ARDKTocEntry items (items at lower depths representing a table of contents sub-listing from a parent item). An example of how to traverse the toc array is as follows:

func traverse(toc:[ARDKTocEntry]) {
    var tocEntries:[ARDKTocEntry] = []

    func add(entries:[ARDKTocEntry]) {
        for entry:ARDKTocEntry in entries {
            tocEntries.append(entry)
            if entry.children != nil {
                add(entries:entry.children! as! [ARDKTocEntry])
            }
        }
    }

    add(entries:toc)

    /// Items should now be in a flat array, check their label and depth
    for item:ARDKTocEntry in tocEntries {
        print("item.label=\(item.label)")
        print("item.depth=\(item.depth)")
    }
}

This software is provided AS-IS with no warranty, either express or implied. This software is distributed under license and may not be copied, modified or distributed except as expressly authorized under the terms of that license. Refer to licensing information at artifex.com or contact Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, CA 94129, USA, for further information.Discord logo