ElementReader

Sample C# code for using Apryse SDK to traverse the page display list using ElementReader. Learn more about our Xamarin SDK and PDF Data Extraction SDK Capabilities.

1//
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11
12using NUnit.Framework;
13
14namespace MiscellaneousSamples
15{
16 /// <summary>
17 /// Summary description for Class1.
18 /// </summary>
19 [TestFixture]
20 public class ElementReaderTest
21 {
22
23 static void ProcessElements(ElementReader reader)
24 {
25 Element element;
26 while ((element = reader.Next()) != null) // Read page contents
27 {
28 switch (element.GetType())
29 {
30
31 case Element.Type.e_path: // Process path data...
32 {
33 PathData data = element.GetPathData();
34 double[] points = data.points;
35 break;
36 }
37
38 case Element.Type.e_text: // Process text strings...
39 {
40 String str = element.GetTextString();
41 Console.WriteLine(str);
42 break;
43 }
44
45 case Element.Type.e_form: // Process form XObjects
46 {
47 Console.WriteLine("Process Element.Type.e_form");
48 reader.FormBegin();
49 ProcessElements(reader);
50 reader.End();
51 break;
52 }
53 }
54 }
55 }
56
57 /// <summary>
58 /// The main entry point for the application.
59 /// </summary>
60 [Test]
61 public static void Sample()
62 {
63
64 // Relative path to the folder containing test files.
65 const string input_path = "TestFiles/";
66
67 try
68 {
69 Console.WriteLine("-------------------------------------------------");
70 Console.WriteLine("Sample 1 - Extract text data from all pages in the document.");
71
72 // Open the test file
73 Console.WriteLine("Opening the input pdf...");
74 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
75 using (ElementReader page_reader = new ElementReader())
76 {
77 doc.InitSecurityHandler();
78
79 PageIterator itr;
80 for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) // Read every page
81 {
82 page_reader.Begin(itr.Current());
83 ProcessElements(page_reader);
84 page_reader.End();
85 }
86 Console.WriteLine("Done.");
87 }
88
89 }
90 catch (PDFNetException e)
91 {
92 Console.WriteLine(e.Message);
93 Assert.True(false);
94 }
95 }
96 }
97}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales