|
| 1 | +--- |
| 2 | +title: Extract tables from PDF and image documents in .NET | Syncfusion |
| 3 | +description: Syncfusion® Smart Table Extractor is a .NET library that extracts tabular data from PDF documents. It detects table regions, header rows, columns, cell spans (merged cells), and provides structured JSON. |
| 4 | +platform: SmartTableExtractor |
| 5 | +control: PDF |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# SmartTableExtractor Features |
| 10 | + |
| 11 | +## Extract Tables from a PDF Documents |
| 12 | + |
| 13 | +To extract structured table data from a PDF document using the **ExtractTableAsJson** method of the **TableExtractor** class, refer to the following code |
| 14 | + |
| 15 | +{% tabs %} |
| 16 | + |
| 17 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 18 | + |
| 19 | +using System.IO; |
| 20 | +using System.Text; |
| 21 | +using Syncfusion.SmartTableExtractor; |
| 22 | + |
| 23 | +//Open the input PDF file as a stream. |
| 24 | +using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read)) |
| 25 | +{ |
| 26 | + // Initialize the Smart Table Extractor |
| 27 | + TableExtractor extractor = new TableExtractor(); |
| 28 | + |
| 29 | + // Set all three options together |
| 30 | + TableExtractionOptions options = new TableExtractionOptions(); |
| 31 | + options.DetectBorderlessTables = true; |
| 32 | + options.PageRange = new int[,] { { 1, 5 } }; |
| 33 | + options.ConfidenceThreshold = 0.75; |
| 34 | + |
| 35 | + extractor.TableExtractionOptions = options; |
| 36 | + |
| 37 | + // Extract and save |
| 38 | + string data = extractor.ExtractTableAsJson(stream); |
| 39 | + File.WriteAllText("TableOutput_AllOptions.json", data, Encoding.UTF8); |
| 40 | +} |
| 41 | + |
| 42 | +{% endhighlight %} |
| 43 | + |
| 44 | +{% endtabs %} |
| 45 | + |
| 46 | +## Extract Tables with detect borderless tables |
| 47 | + |
| 48 | +To extract structured table data from a PDF document that contains tables without visible borders using the **ExtractTableAsJson** method of the **TableExtractor** class, refer to the following code examples. |
| 49 | + |
| 50 | +{% tabs %} |
| 51 | + |
| 52 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 53 | + |
| 54 | +using System.IO; |
| 55 | +using System.Text; |
| 56 | +using Syncfusion.SmartTableExtractor; |
| 57 | + |
| 58 | +using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read)) |
| 59 | +{ |
| 60 | + // Initialize the Smart Table Extractor |
| 61 | + TableExtractor extractor = new TableExtractor(); |
| 62 | + |
| 63 | + // Set DetectBorderlessTables |
| 64 | + TableExtractionOptions options = new TableExtractionOptions(); |
| 65 | + options.DetectBorderlessTables = true; |
| 66 | + |
| 67 | + extractor.TableExtractionOptions = options; |
| 68 | + |
| 69 | + // Extract and save |
| 70 | + string data = extractor.ExtractTableAsJson(stream); |
| 71 | + File.WriteAllText("Output.json", data, Encoding.UTF8); |
| 72 | +} |
| 73 | + |
| 74 | +{% endhighlight %} |
| 75 | + |
| 76 | +{% endtabs %} |
| 77 | + |
| 78 | +## Extract Tables Within a Specific Page Range |
| 79 | + |
| 80 | +To extract structured table data from a specific range of pages in a PDF document using the **ExtractTableAsJson** method of the **TableExtractor** class, refer to the following code example: |
| 81 | + |
| 82 | +{% tabs %} |
| 83 | + |
| 84 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 85 | + |
| 86 | +using System.IO; |
| 87 | +using System.Text; |
| 88 | +using Syncfusion.SmartTableExtractor; |
| 89 | + |
| 90 | +using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read)) |
| 91 | +{ |
| 92 | + // Initialize the Smart Table Extractor |
| 93 | + TableExtractor extractor = new TableExtractor(); |
| 94 | + |
| 95 | + // Set only PageRange |
| 96 | + TableExtractionOptions options = new TableExtractionOptions(); |
| 97 | + options.PageRange = new int[,] { { 2, 4 } }; |
| 98 | + |
| 99 | + extractor.TableExtractionOptions = options; |
| 100 | + |
| 101 | + // Extract and save |
| 102 | + string data = extractor.ExtractTableAsJson(stream); |
| 103 | + File.WriteAllText("Output.json", data, Encoding.UTF8); |
| 104 | +} |
| 105 | + |
| 106 | +{% endhighlight %} |
| 107 | + |
| 108 | +{% endtabs %} |
| 109 | + |
| 110 | +## Apply confidence threshold to extract the Table |
| 111 | + |
| 112 | +To apply confidence thresholding when extracting table data from a PDF document using the **ExtractTableAsJson** method of the **TableExtractor** class, refer to the following code example: |
| 113 | + |
| 114 | +{% tabs %} |
| 115 | + |
| 116 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 117 | + |
| 118 | +using System.IO; |
| 119 | +using System.Text; |
| 120 | +using Syncfusion.SmartTableExtractor; |
| 121 | + |
| 122 | +using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read)) |
| 123 | +{ |
| 124 | + // Initialize the Smart Table Extractor |
| 125 | + TableExtractor extractor = new TableExtractor(); |
| 126 | + |
| 127 | + // Set ConfidenceThreshold |
| 128 | + TableExtractionOptions options = new TableExtractionOptions(); |
| 129 | + options.ConfidenceThreshold = 0.6; |
| 130 | + |
| 131 | + extractor.TableExtractionOptions = options; |
| 132 | + |
| 133 | + // Extract and save |
| 134 | + string data = extractor.ExtractTableAsJson(stream); |
| 135 | + File.WriteAllText("Output.json", data, Encoding.UTF8); |
| 136 | +} |
| 137 | + |
| 138 | +{% endtabs %} |
| 139 | + |
| 140 | +## Extract table data asynchronously from a PDF document |
| 141 | + |
| 142 | +To extract table data asynchronously with cancellation support using the **ExtractTableAsJsonAsync** method of the **TableExtractor** class, refer to the following code example: |
| 143 | + |
| 144 | +{% tabs %} |
| 145 | + |
| 146 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 147 | + |
| 148 | +using System.IO; |
| 149 | +using System.Text; |
| 150 | +using System.Threading; |
| 151 | +using Syncfusion.SmartTableExtractor; |
| 152 | + |
| 153 | +using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read)) |
| 154 | +{ |
| 155 | + // Declare and configure the extractor and options |
| 156 | + TableExtractionOptions extractionOptions = new TableExtractionOptions(); |
| 157 | + extractionOptions.DetectBorderlessTables = true; |
| 158 | + extractionOptions.ConfidenceThreshold = 0.6; |
| 159 | + |
| 160 | + TableExtractor tableExtractor = new TableExtractor(); |
| 161 | + tableExtractor.TableExtractionOptions = extractionOptions; |
| 162 | + |
| 163 | + // Create cancellation token with timeout |
| 164 | + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); |
| 165 | + |
| 166 | + // Call the async extraction API |
| 167 | + string data = await tableExtractor.ExtractTableAsJsonAsync(stream, cts.Token); |
| 168 | + |
| 169 | + // Save the extracted data as JSON |
| 170 | + File.WriteAllText("TableOutput.json", data, Encoding.UTF8); |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +{% endhighlight %} |
| 175 | + |
| 176 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 177 | + |
| 178 | +using System.IO; |
| 179 | +using System.Text; |
| 180 | +using System.Threading; |
| 181 | +using Syncfusion.SmartTableExtractor; |
| 182 | + |
| 183 | +using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read)) |
| 184 | +{ |
| 185 | + // Declare and configure the extractor and options |
| 186 | + TableExtractionOptions extractionOptions = new TableExtractionOptions(); |
| 187 | + extractionOptions.DetectBorderlessTables = true; |
| 188 | + extractionOptions.ConfidenceThreshold = 0.6; |
| 189 | + |
| 190 | + TableExtractor tableExtractor = new TableExtractor(); |
| 191 | + tableExtractor.TableExtractionOptions = extractionOptions; |
| 192 | + |
| 193 | + // Create cancellation token with timeout |
| 194 | + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); |
| 195 | + |
| 196 | + // Call the async extraction API |
| 197 | + string data = await tableExtractor.ExtractTableAsJsonAsync(stream, cts.Token); |
| 198 | + |
| 199 | + // Save the extracted data as JSON |
| 200 | + File.WriteAllText("TableOutput.json", data, Encoding.UTF8); |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +{% endhighlight %} |
| 205 | + |
| 206 | +{% endtabs %} |
| 207 | + |
0 commit comments