> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/ios/get-started/samples/elementreadertest.md).

# ElementReader

Sample code for using Apryse iOS SDK to traverse the page display list using ElementReader in Swift & Obj-C

Sample code in Swift and Obj-C for using Apryse iOS SDK to traverse the page display list using ElementReader available. Learn more about our full [PDF Data Extraction SDK Capabilities](https://apryse.com/capabilities/extraction).

To start your free trial, [get stated with iOS SDK](/ios/get-started/get-started.md).

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>

void ProcessReaderTestElements(PTElementReader *reader)
{
    PTElement *element;
    for (element=[reader Next]; element != NULL; element = [reader Next])     // Read page contents
    {
        switch ([element GetType])
        {
        case e_ptpath:                        // Process path data...
            {
                PTPathData *data = [element GetPathData];
                NSArray *points = [data GetPoints];
            }
             break;
        case e_pttext_obj:                 // Process text strings...
            {
                NSString* nsdata = [element GetTextString];
                unsigned char* data = (unsigned char *)[nsdata UTF8String];
                printf("%s\n", data);
            }
            break;
        case e_ptform:                // Process form XObjects
            {
                [reader FormBegin]; 
                ProcessReaderTestElements(reader);
                [reader End]; 
            }
            break;
        default:
                ;
        }
    }
}

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int ret = 0;
        [PTPDFNet Initialize: 0];
        
        @try    // Extract text data from all pages in the document
        {
            printf("-------------------------------------------------\n");
            printf("Sample 1 - Extract text data from all pages in the document.\n");
            printf("Opening the input pdf...\n");

            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [doc InitSecurityHandler];
            
            PTPageIterator *itr;
            PTElementReader *page_reader = [[PTElementReader alloc] init];

            for (itr = [doc GetPageIterator: 1]; [itr HasNext]; [itr Next])        //  Read every page
            {                
                [page_reader Begin: [itr Current]];
                ProcessReaderTestElements(page_reader);
                [page_reader End];
            }

            printf("Done.\n");
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }
        [PTPDFNet Terminate: 0];        
        return ret;
    }

}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
//---------------------------------------------------------------------------------------
// 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
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/ios/get-started/samples/elementreadertest.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
