Some test text!

platform
search
< iOS samples

Read Elements Across All PDF Pages in Swift

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

Step 1: Get your free trial license key, or sign in

Start Trial
Sign in

Step 2: Add the code:

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2017 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
    }
}