Sample Obj-C code for using Apryse SDK to traverse the page display list using ElementReader. Learn more about our iOS SDK and PDF Data Extraction SDK Capabilities.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9void ProcessReaderTestElements(PTElementReader *reader)
10{
11 PTElement *element;
12 for (element=[reader Next]; element != NULL; element = [reader Next]) // Read page contents
13 {
14 switch ([element GetType])
15 {
16 case e_ptpath: // Process path data...
17 {
18 PTPathData *data = [element GetPathData];
19 NSArray *points = [data GetPoints];
20 }
21 break;
22 case e_pttext_obj: // Process text strings...
23 {
24 NSString* nsdata = [element GetTextString];
25 unsigned char* data = (unsigned char *)[nsdata UTF8String];
26 printf("%s\n", data);
27 }
28 break;
29 case e_ptform: // Process form XObjects
30 {
31 [reader FormBegin];
32 ProcessReaderTestElements(reader);
33 [reader End];
34 }
35 break;
36 default:
37 ;
38 }
39 }
40}
41
42int main(int argc, char *argv[])
43{
44 @autoreleasepool {
45 int ret = 0;
46 [PTPDFNet Initialize: 0];
47
48 @try // Extract text data from all pages in the document
49 {
50 printf("-------------------------------------------------\n");
51 printf("Sample 1 - Extract text data from all pages in the document.\n");
52 printf("Opening the input pdf...\n");
53
54 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
55 [doc InitSecurityHandler];
56
57 PTPageIterator *itr;
58 PTElementReader *page_reader = [[PTElementReader alloc] init];
59
60 for (itr = [doc GetPageIterator: 1]; [itr HasNext]; [itr Next]) // Read every page
61 {
62 [page_reader Begin: [itr Current]];
63 ProcessReaderTestElements(page_reader);
64 [page_reader End];
65 }
66
67 printf("Done.\n");
68 }
69 @catch(NSException *e)
70 {
71 NSLog(@"%@", e.reason);
72 ret = 1;
73 }
74 [PTPDFNet Terminate: 0];
75 return ret;
76 }
77
78}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
8
9func ProcessReaderTestElements(reader: PTElementReader) {
10 while let element = reader.next() {
11 switch element.getType() {
12 case e_ptpath: // Process path data...
13 let data: PTPathData = element.getPathData()
14 let _ = data.getPoints() // points
15 case e_pttext_obj: // Process text strings...
16 let textData: Data = element.getTextData()
17 let text = String(data: textData, encoding: .utf8)
18 print("\(text ?? "")")
19 case e_ptform: // Process form XObjects
20 reader.formBegin()
21 ProcessReaderTestElements(reader: reader)
22 reader.end()
23 default:
24 break
25 }
26 }
27}
28
29func runElementReaderTest() -> Int {
30 return autoreleasepool {
31 var ret: Int = 0
32
33
34 do { // Extract text data from all pages in the document
35 try PTPDFNet.catchException {
36 print("__________________________________________________")
37 print("Sample 1 - Extract text data from all pages in the document.")
38 print("Opening the input pdf...")
39
40 let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
41 doc.initSecurityHandler()
42
43 let page_reader: PTElementReader = PTElementReader()
44
45 let itr: PTPageIterator = doc.getPageIterator(1)
46 while itr.hasNext() { // Read every page
47 page_reader.begin(itr.current())
48 ProcessReaderTestElements(reader: page_reader)
49 page_reader.end()
50 itr.next()
51
52 }
53 print("Done.")
54 }
55 } catch let e as NSError {
56 print("Caught PDFNet exception: \(e)")
57 ret = 1
58 }
59
60 return ret
61 }
62}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales