Some test text!

menu

Read elements across all PDF pages in Swift

More languages

chevron_right
More languages
JavaScript
Java (Android)
C++
C#
C# (.NET Core)
Java
Kotlin
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
C# (UWP)
VB
C# (Xamarin)

Sample Swift code for using PDFTron SDK to traverse the page display list using ElementReader. Learn more about our Swift PDF Library and PDF Parsing & Content Extraction Library.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

func ProcessReaderTestElements(reader: PTElementReader) {
    while let element = reader.next() {
        switch element.getType() {
        case e_ptpath:  // Process path data...
            let data: PTPathData = element.getPathData()
            let _ = data.getPoints()    // points
        case e_pttext_obj:  // Process text strings...
            let textData: Data = element.getTextData()
            let text = String(data: textData, encoding: .utf8)
            print("\(text ?? "")")
        case e_ptform:  // Process form XObjects
            reader.formBegin()
            ProcessReaderTestElements(reader: reader)
            reader.end()
        default:
            break
        }
    }
}

func runElementReaderTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0
        
        
        do {    // Extract text data from all pages in the document
            try PTPDFNet.catchException {
                print("__________________________________________________")
                print("Sample 1 - Extract text data from all pages in the document.")
                print("Opening the input pdf...")
                
                let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                doc.initSecurityHandler()
                
                let page_reader: PTElementReader = PTElementReader()
                
                let itr: PTPageIterator = doc.getPageIterator(1)
                while itr.hasNext() {  // Read every page
                    page_reader.begin(itr.current())
                    ProcessReaderTestElements(reader: page_reader)
                    page_reader.end()
                    itr.next()
                
                }
                print("Done.")
            }
        } catch let e as NSError {
            print("Caught PDFNet exception: \(e)")
            ret = 1
        }
        
        return ret
    }
}
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.