Programmatically Go To PDF Outline in Swift for iOS

Programmatically go to a specific document outline item. Get additional resources by visiting our Outline API guide.


//
// Copyright © 2017-2025 PSPDFKit GmbH. All rights reserved.
//
// The Nutrient sample applications are licensed with a modified BSD license.
// Please see License for details. This notice may not be removed from this file.
//
import PSPDFKit
import PSPDFKitUI
class ProgrammaticallyGoToOutlineExample: Example {
override init() {
super.init()
title = "Programmatically Go to a Specific Outline."
category = .controllerCustomization
priority = .max
}
override func invoke(with delegate: ExampleRunnerDelegate) -> UIViewController {
let document = AssetLoader.document(for: .welcome)
let controller = PDFViewController(document: document)
// Check that the PDF has an outline.
guard let outline = document.outline else {
return controller
}
guard let matchingOutline = matchingOutlineByTitle(outline: outline, title: "Swift 4") else {
return controller
}
controller.setPageIndex(matchingOutline.pageIndex, animated: false)
return controller
}
// Recursive function to find the matching outline element by title.
private func matchingOutlineByTitle(outline: OutlineElement, title: String) -> OutlineElement! {
// Return the passed outline if the title matches
if outline.title == title {
return outline
}
if let children = outline.children {
// Loop through the outline's children to to find a matching outline
for child in children {
// Match found
if let match = matchingOutlineByTitle(outline: child, title: title) {
return match
}
}
}
return nil
}
}

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.