Customize Annotation Link Protocol in Swift for iOS

Use a custom link protocol with a link annotation. Get additional resources by visiting our guide on adding link annotations to PDFs in iOS.


//
// 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 CustomLinkProtocolExample: Example {
override init() {
super.init()
title = "Custom Link Protocol"
contentDescription = "Uses a custom pspdfcatalog:// link protocol."
category = .annotations
priority = 800
}
override func invoke(with delegate: ExampleRunnerDelegate) -> UIViewController? {
let document = AssetLoader.temporaryDocument(with: "Test PDF for custom Protocols")
document.annotationSaveMode = .disabled
// Add link
// By default, Nutrient would ask if you want to leave the app when an external URL is detected.
// We skip this question if the protocol is defined within our own app.
let link = PSPDFLinkAnnotation(url: URL(string: "pspdfcatalog://this-is-a-test-link")!)
let pageSize = document.pageInfoForPage(at: 0)!.size
let size = CGSize(width: 400, height: 300)
link.boundingBox = CGRect(x: (pageSize.width - size.width) / 2, y: (pageSize.height - size.height) / 2, width: size.width, height: size.height)
document.add([link])
let pdfController = PDFViewController(document: document)
return pdfController
}
}

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