Sample C# code for using Apryse SDK to traverse the page display list using ElementReader. Learn more about our UWP SDK and PDF Data Extraction SDK Capabilities.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14
15namespace PDFNetSamples
16{
17 public sealed class ElementReaderTest : Sample
18 {
19 public ElementReaderTest() :
20 base("ElementReader", "Illustrates how to traverse page display list using ElementReader.")
21 {
22 }
23
24 public override IAsyncAction RunAsync()
25 {
26 return Task.Run(new System.Action(() => {
27 WriteLine("--------------------------------");
28 WriteLine("Starting ElementReader Test...");
29 WriteLine("--------------------------------\n");
30 try {
31 WriteLine("-------------------------------------------------");
32 WriteLine("Extract text data from all pages in the document.");
33
34 // Open the test file
35 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
36 WriteLine("Opening input file: " + input_file_path);
37 PDFDoc doc = new PDFDoc(input_file_path);
38 doc.InitSecurityHandler();
39
40 PageIterator itr;
41 pdftron.PDF.ElementReader page_reader = new pdftron.PDF.ElementReader();
42
43 //int i = 0;
44 for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) // Read every page
45 {
46 int pageNo = itr.GetPageNumber();
47 WriteLine(String.Format("Page {0:d} ----------------------------------------", pageNo));
48
49 page_reader.Begin(itr.Current());
50 String result = ProcessElements(page_reader);
51 WriteLine(result);
52 page_reader.End();
53 }
54 WriteLine("Done.");
55 doc.Destroy();
56 }
57 catch (Exception e) {
58 WriteLine(GetExceptionMessage(e));
59 }
60
61 WriteLine("\n--------------------------------");
62 WriteLine("Done ElementReader Test.");
63 WriteLine("--------------------------------\n");
64 })).AsAsyncAction();
65 }
66
67 String ProcessElements(ElementReader reader)
68 {
69 String result = "";
70 Element element;
71 //int i = 0;
72 while ((element = reader.Next()) != null) // Read page contents
73 {
74 switch (element.GetType())
75 {
76 case ElementType.e_path: // Process path data...
77 {
78 result += "Process Element.Type.e_path\n";
79 //PathData data = element.GetPathData();
80 //double[] points = data.get_pts();// points;
81 break;
82 }
83 case ElementType.e_image: // Process images...
84 case ElementType.e_inline_image:
85 {
86 result += "Process Element.Type.e_image\n";
87 break;
88 }
89 case ElementType.e_text: // Process text strings...
90 {
91 result += "Process Element.Type.e_text\n";
92 //String txt = element.GetTextString();
93 // Message+=(txt);
94 break;
95 }
96 case ElementType.e_form: // Process form XObjects
97 {
98 result += "Process Element.Type.e_form\n";
99 reader.FormBegin();
100 result += ProcessElements(reader);
101 reader.End();
102 break;
103 }
104 }
105 }
106 return result;
107 }
108 }
109}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales