|
| 1 | +--- |
| 2 | +title: Working with Recognize methods in SmartFormRecognizer| Syncfusion® |
| 3 | +description: Learn how to use Recognize methods in Syncfusion® SmartFormRecognizer library. |
| 4 | +platform: document-processing |
| 5 | +control: SmartFormRecognizer |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# Recognize Forms |
| 10 | + |
| 11 | +The `FormRecognizer` exposes several convenience methods to recognize forms from a `Stream` input. Each method accepts an input `Stream` (PDF or image) and returns recognized output either as a `PdfLoadedDocument`, a `Stream` containing PDF data, or as a JSON string. |
| 12 | + |
| 13 | +Below each method signature you'll find a explanation and corrected example usage (both synchronous and asynchronous where applicable). |
| 14 | + |
| 15 | +## Recognize forms using PdfLoadedDocument |
| 16 | +Using `PdfLoadedDocument` this operation analyzes the form content supplied through the inputStream whether it contains a PDF or an image and produces a fully enriched PdfLoadedDocument that includes recognized form elements such as checkboxes, radio buttons, textboxes, and signatures, according to the options defined in `FormRecognizeOptions`. This recognition process supports both execution patterns: the synchronous `RecognizeFormAsPdfDocument` method for immediate, blocking processing, and the asynchronous `RecognizeFormAsPdfDocumentAsync` method for non‑blocking, awaitable processing ideal for responsive UI applications or scalable server‑side workflows. |
| 17 | + |
| 18 | +Example (synchronous): |
| 19 | + |
| 20 | +{% tabs %} |
| 21 | +{% highlight c# tabtitle="C#" %} |
| 22 | +public void Button_Click(object sender, RoutedEventArgs e) |
| 23 | + { |
| 24 | + //Initialize the Form Recognizer |
| 25 | + FormRecognizer smartFormRecognizer = new FormRecognizer(); |
| 26 | + //Read the input PDF file as stream |
| 27 | + FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); |
| 28 | + //Recognize the form and get the output as PDF stream |
| 29 | + PdfLoadedDocument pdfLoadedDocument =recognizer.RecognizeFormAsPdfDocument(inputStream); |
| 30 | + //Save the loadeddocument |
| 31 | + pdfLoadedDocument.Save(Output.pdf); |
| 32 | + } |
| 33 | +{% endhighlight %} |
| 34 | +{% endtabs %} |
| 35 | + |
| 36 | + |
| 37 | +Example (asynchronous): |
| 38 | + |
| 39 | +{% tabs %} |
| 40 | +{% highlight c# tabtitle="C#" %} |
| 41 | +public async void Button_Click(object sender, RoutedEventArgs e) |
| 42 | + { |
| 43 | + //Initialize the Form Recognizer |
| 44 | + FormRecognizer smartFormRecognizer = new FormRecognizer(); |
| 45 | + //Read the input PDF file as stream |
| 46 | + FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); |
| 47 | + //Recognize the form and get the output as PDF stream |
| 48 | + PdfLoadedDocument pdfLoadedDocument = await recognizer.RecognizeFormAsPdfDocumentAsync(inputStream); |
| 49 | + //Save the loadeddocument |
| 50 | + pdfLoadedDocument.Save(Output.pdf); |
| 51 | +} |
| 52 | + |
| 53 | +{% endhighlight %} |
| 54 | +{% endtabs %} |
| 55 | + |
| 56 | + |
| 57 | +## Recognize forms using Stream |
| 58 | +Using `Stream`this operation processes the form content provided through the inputStream—whether it contains a PDF or an image—and returns the fully recognized PDF as a Stream.This functionality is available through both the synchronous `RecognizeFormAsPdfStream` method for immediate, blocking execution and the asynchronous `RecognizeFormAsPdfStreamAsync` method for non‑blocking, awaitable processing suitable for responsive UI applications, background services, and scalable server‑side workflows. |
| 59 | + |
| 60 | +Example (synchronous): |
| 61 | + |
| 62 | +{% tabs %} |
| 63 | +{% highlight c# tabtitle="C#" %} |
| 64 | +public void Button_Click(object sender, RoutedEventArgs e) |
| 65 | +{ |
| 66 | + //Initialize the Form Recognizer |
| 67 | + FormRecognizer smartFormRecognizer = new FormRecognizer(); |
| 68 | + //Read the input PDF file as stream |
| 69 | + FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); |
| 70 | + //Recognize the form and get the output as PDF stream |
| 71 | + Stream outputStream = smartFormRecognizer.RecognizeFormAsPdfStream(inputStream); |
| 72 | + //Save the output PDF stream to file |
| 73 | + using (FileStream fileStream = File.Create("Output.pdf")) |
| 74 | + { |
| 75 | + outputStream.Seek(0, SeekOrigin.Begin); |
| 76 | + outputStream.CopyTo(fileStream); |
| 77 | + } |
| 78 | +} |
| 79 | +{% endhighlight %} |
| 80 | +{% endtabs %} |
| 81 | + |
| 82 | + |
| 83 | +Example (asynchronous): |
| 84 | + |
| 85 | +{% tabs %} |
| 86 | +{% highlight c# tabtitle="C#" %} |
| 87 | +public async void Button_Click(object sender, RoutedEventArgs e) |
| 88 | +{ |
| 89 | + //Initialize the Form Recognizer |
| 90 | + FormRecognizer smartFormRecognizer = new FormRecognizer(); |
| 91 | + //Read the input PDF file as stream |
| 92 | + FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); |
| 93 | + //Recognize the form and get the output as PDF stream |
| 94 | + Stream outputStream = await smartFormRecognizer.RecognizeFormAsPdfStreamAsync(inputStream); |
| 95 | + //Save the output PDF stream to file |
| 96 | + using (FileStream fileStream = File.Create("Output.pdf")) |
| 97 | + { |
| 98 | + outputStream.Seek(0, SeekOrigin.Begin); |
| 99 | + outputStream.CopyTo(fileStream); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +{% endhighlight %} |
| 104 | +{% endtabs %} |
| 105 | + |
| 106 | +## Recognize forms using JSON |
| 107 | + |
| 108 | +Using `JSON`this operation recognizes the form contained in the inputStream whether the source document is a PDF or an image—and returns the complete recognition output serialized as a JSON string. This functionality is accessible through both the synchronous `RecognizeFormAsJson` method, which performs immediate, blocking processing, and the asynchronous `RecognizeFormAsJsonAsync` method, which provides non‑blocking, awaitable execution suitable for UI applications, background workers, and scalable cloud or server‑side workflows. |
| 109 | + |
| 110 | +Example (synchronous): |
| 111 | + |
| 112 | +{% tabs %} |
| 113 | +{% highlight c# tabtitle="C#" %} |
| 114 | +public void Button_Click(object sender, RoutedEventArgs e) |
| 115 | +{ |
| 116 | + //Initialize the Form Recognizer |
| 117 | + FormRecognizer smartFormRecognizer = new FormRecognizer(); |
| 118 | + //Read the input PDF file as stream |
| 119 | + FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); |
| 120 | + //Recognize the form and get the output as PDF stream |
| 121 | + String outputJson = smartFormRecognizer.RecognizeFormAsJson(inputStream); |
| 122 | + //Save the outputJson |
| 123 | + File.Create("D:\\result.json").Close(); |
| 124 | + File.WriteAllText("D:\\result.json", outputJson); |
| 125 | +} |
| 126 | +{% endhighlight %} |
| 127 | +{% endtabs %} |
| 128 | + |
| 129 | + |
| 130 | +Example (asynchronous): |
| 131 | + |
| 132 | +{% tabs %} |
| 133 | +{% highlight c# tabtitle="C#" %} |
| 134 | +public async void Button_Click(object sender, RoutedEventArgs e) |
| 135 | +{ |
| 136 | + //Initialize the Form Recognizer |
| 137 | + FormRecognizer smartFormRecognizer = new FormRecognizer(); |
| 138 | + //Read the input PDF file as stream |
| 139 | + FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); |
| 140 | + //Recognize the form and get the output as PDF stream |
| 141 | + String outputJson = await smartFormRecognizer.RecognizeFormAsJsonAsync(inputStream); |
| 142 | + //Save the outputJson |
| 143 | + File.Create("D:\\result.json").Close(); |
| 144 | + File.WriteAllText("D:\\result.json", outputJson); |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +{% endhighlight %} |
| 149 | +{% endtabs %} |
| 150 | + |
| 151 | +## Async variants with CancellationToken |
| 152 | + |
| 153 | +The async overloads accept an optional `CancellationToken` to cancel long-running operations: |
| 154 | + |
| 155 | +- `Task<PdfLoadedDocument> RecognizeFormAsPdfDocumentAsync(Stream inputStream, CancellationToken cancellationToken = default)` |
| 156 | +- `Task<Stream> RecognizeFormAsPdfStreamAsync(Stream inputStream, CancellationToken cancellationToken = default)` |
| 157 | +- `Task<string> RecognizeFormAsJsonAsync(Stream inputStream, CancellationToken cancellationToken = default)` |
| 158 | + |
| 159 | +Example with cancellation token (PDF stream): |
| 160 | + |
| 161 | +```csharp |
| 162 | +public async Task RecognizeWithCancellationAsync(CancellationToken token) |
| 163 | +{ |
| 164 | + FormRecognizer recognizer = new FormRecognizer(); |
| 165 | + |
| 166 | + using FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read); |
| 167 | + |
| 168 | + using Stream resultStream = await recognizer.RecognizeFormAsPdfStreamAsync(inputStream, token); |
| 169 | + |
| 170 | + using FileStream fileStream = File.Create("Output.pdf"); |
| 171 | + await resultStream.CopyToAsync(fileStream, token); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +Notes: |
| 176 | +- Always forward cancellation tokens to I/O methods when available. |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## Common corrections and best practices shown in the examples |
| 181 | + |
| 182 | +- Use `using` (or `await using` for IAsyncDisposable) to ensure streams are disposed. |
| 183 | +- Use `FileAccess.Read` when only reading input files. |
| 184 | +- Do not reuse mismatched variable names (e.g., `smartFormRecognizer` vs `recognizer`) — pick one consistent name. |
| 185 | +- Prefer `async`/`await` in UI event handlers but avoid `async void` except for top-level event handlers; prefer `async Task` where possible for testability. |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +If you want, I can also: |
| 190 | +- Add a short snippet showing how to handle `FormRecognizeOptions` with these calls. |
| 191 | +- Add a unit test or a small console app sample demonstrating each method. |
0 commit comments