How can I help you?
Working with Form Recognition
25 May 20269 minutes to read
Recognize Forms as JSON
To recognize form data from a PDF or image and get the output as a JSON string using the RecognizeFormAsJson (synchronous) and RecognizeFormAsJsonAsync (asynchronous) methods of the FormRecognizer class, refer to the following code examples.
Example (synchronous):
using Syncfusion.SmartFormRecognizer;
// Open the input PDF file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite))
{
// Initialize the Form Recognizer.
FormRecognizer recognizer = new FormRecognizer();
// Recognize the form and get the output as a JSON string.
string outputJson = recognizer.RecognizeFormAsJson(inputStream);
// Save the extracted JSON data into an output file.
File.WriteAllText("result.json", outputJson);
}You can download a complete working sample from GitHub.
Example (asynchronous):
using Syncfusion.SmartFormRecognizer;
// Open the input PDF file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite))
{
// Initialize the Form Recognizer.
FormRecognizer recognizer = new FormRecognizer();
// Recognize the form asynchronously and get the output as a JSON string.
string outputJson = await recognizer.RecognizeFormAsJsonAsync(inputStream);
// Save the extracted JSON data into an output file.
File.WriteAllText("result.json", outputJson);
}You can download a complete working sample from GitHub.
Recognize Forms from PDF or Image
Recognize forms as PDF
To recognize form data from a PDF or image and get the output as a PdfLoadedDocument using the RecognizeFormAsPdfDocument (synchronous) and RecognizeFormAsPdfDocumentAsync (asynchronous) methods of the FormRecognizer class, refer to the following code examples.
Example (synchronous):
using Syncfusion.SmartFormRecognizer;
using Syncfusion.Pdf.Parsing;
// Open the input PDF file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite))
{
// Initialize the Form Recognizer.
FormRecognizer recognizer = new FormRecognizer();
// Recognize the form and get the output as a PDF document.
PdfLoadedDocument document = recognizer.RecognizeFormAsPdfDocument(inputStream);
// Save the recognized document.
document.Save("Output.pdf");
// Close the document.
document.Close();
}You can download a complete working sample from GitHub.
Example (asynchronous):
using Syncfusion.Pdf.Parsing;
using Syncfusion.SmartFormRecognizer;
// Open the input PDF file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite))
{
// Initialize the Form Recognizer.
FormRecognizer recognizer = new FormRecognizer();
// Recognize the form asynchronously and get the output as a PDF document.
PdfLoadedDocument document = await recognizer.RecognizeFormAsPdfDocumentAsync(inputStream);
// Save the recognized document.
document.Save("Output.pdf");
// Close the document.
document.Close();
}You can download a complete working sample from GitHub.
Recognize forms as Stream
To recognize form data from a PDF or image and get the output as a Stream using the RecognizeFormAsPdfStream (synchronous) and RecognizeFormAsPdfStreamAsync (asynchronous) methods of the FormRecognizer class, refer to the following code examples.
Example (synchronous):
using Syncfusion.SmartFormRecognizer;
// Open the input PDF file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite))
{
// Initialize the Form Recognizer.
FormRecognizer recognizer = new FormRecognizer();
// Recognize the form and get the output as a PDF stream.
using (Stream outputStream = recognizer.RecognizeFormAsPdfStream(inputStream))
{
// Save the output PDF stream to a file.
using (FileStream fileStream = File.Create("Output.pdf"))
{
outputStream.Seek(0, SeekOrigin.Begin);
outputStream.CopyTo(fileStream);
}
}
}You can download a complete working sample from GitHub.
Example (asynchronous):
using Syncfusion.SmartFormRecognizer;
// Open the input PDF file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite))
{
// Initialize the Form Recognizer.
FormRecognizer recognizer = new FormRecognizer();
// Recognize the form asynchronously and get the output as a PDF stream.
using (Stream outputStream = await recognizer.RecognizeFormAsPdfStreamAsync(inputStream))
{
// Save the output PDF stream to a file.
using (FileStream fileStream = File.Create("Output.pdf"))
{
outputStream.Seek(0, SeekOrigin.Begin);
outputStream.CopyTo(fileStream);
}
}
}You can download a complete working sample from GitHub.
Async variants with CancellationToken
To recognize form data asynchronously with cancellation support using the RecognizeFormAsPdfStreamAsync method of the FormRecognizer class, refer to the following code example.
Example with cancellation token (PDF stream):
using Syncfusion.SmartFormRecognizer;
// Open the input PDF file as a stream.
using FileStream inputStream = new FileStream(Path.GetFullPath("Input.pdf"), FileMode.Open, FileAccess.Read);
// Initialize the Form Recognizer.
FormRecognizer recognizer = new FormRecognizer();
// Create a cancellation token that cancels after 5 seconds.
using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
CancellationToken token = cts.Token;
// Recognize the form asynchronously and get the output as a PDF stream.
using Stream resultStream = await recognizer.RecognizeFormAsPdfStreamAsync(inputStream, token);
// Save the output PDF stream to a file.
using FileStream fileStream = File.Create(Path.GetFullPath("Output.pdf"));
await resultStream.CopyToAsync(fileStream, token);You can download a complete working sample from GitHub.
Working with Form Recognize Options
FormRecognizeOptions provides configurable settings that control how the SmartFormRecognizer detects elements from a document. It allows you to enable or disable the detection of specific form controls such as checkboxes, radio buttons, textboxes, and signatures—while also letting you fine-tune the recognition results using a confidence threshold.
Additionally, it supports restricting processing to specific pages through an optional 1‑based inclusive PageRange. By adjusting these options, developers can optimize performance, reduce noise in results, and tailor form extraction precisely to the needs of their application.
Disable Textbox Detection
To disable textbox field detection in the FormRecognizeOptions of the FormRecognizer class, refer to the following code example.
FormRecognizer recognizer = new FormRecognizer();
// Disable textbox detection
recognizer.FormRecognizeOptions.DetectTextboxes = false;Disable Checkbox Detection
To disable checkbox detection in the FormRecognizeOptions of the FormRecognizer class, refer to the following code example.
FormRecognizer recognizer = new FormRecognizer();
// Disable checkbox detection
recognizer.FormRecognizeOptions.DetectCheckboxes = false;Disable RadioButtons Detection
To disable radio button detection in the FormRecognizeOptions of the FormRecognizer class, refer to the following code example.
FormRecognizer recognizer = new FormRecognizer();
// Disable radio button detection
recognizer.FormRecognizeOptions.DetectRadioButtons = false;Disable Signature Detection
To disable signature field detection in the FormRecognizeOptions of the FormRecognizer class, refer to the following code example.
FormRecognizer recognizer = new FormRecognizer();
// Disable signature detection
recognizer.FormRecognizeOptions.DetectSignatures = false;Set Confidence Threshold
To set a minimum confidence score for detected form elements using the ConfidenceThreshold in the FormRecognizeOptions of the FormRecognizer class, refer to the following code example.
FormRecognizer recognizer = new FormRecognizer();
// Set a ConfidenceThreshold
recognizer.FormRecognizeOptions.ConfidenceThreshold = 0.9;Set Page Range
To specify which pages to process using the PageRange in the FormRecognizeOptions of the FormRecognizer class, refer to the following code example.
FormRecognizer recognizer = new FormRecognizer();
// Set a single page range – detects only the specified page
recognizer.FormRecognizeOptions.PageRange = new int[,] { { 3 }, { 8 } };
// Set a page range – detects content between the specified start and end page
recognizer.FormRecognizeOptions.PageRange = new int[,] { { 3, 8 } };