Labeling form fields with a vision language model
Form-field detection locates the fillable regions on a page, but a bounding box alone doesn’t tell you what each field means. AI labeling adds a human-readable semantic label to each detected field, such as “First name” or “Date of birth”, by sending the page to a vision language model (VLM).
This sample builds on offline form-field detection. For the detection basics — and a fully offline workflow with no model contacted — refer to the extract form fields from an image guide. Here you use the built-in Local provider to label fields with Nutrient .NET SDK.
Download sampleHow Nutrient helps
Nutrient .NET SDK runs detection and labeling behind a single method call. With labeling enabled, it also:
- Draws numbered marks over each detected field on a rendered copy of the page
- Processes the annotated page with the built-in VLM and reads each field’s semantic label back
- Optionally drops detections the model judges to be false positives
- Records each field’s type, bounding box, confidence, and assigned label in JSON
The result is structured data you can index, validate, or feed into a downstream workflow.
Prerequisites
The SDK’s built-in Local provider runs a vision model in the application process. No API key, endpoint, or separately managed model server is required.
- Allow internet access on first use so the SDK can download its model files from the Nutrient CDN.
- Ensure the application can write to the persistent SDK resource cache. Later runs reuse the downloaded files.
- Allocate enough CPU/GPU memory for the local model. The SDK uses GPU acceleration when available.
Use the built-in vision model
Local is the default provider. The SDK manages the model download, cache, and loading for you. Turn on AI labeling to process form fields with that model.
Complete implementation
Start by importing the Nutrient namespace:
using Nutrient;Load the document
Open the document with a using statement(opens in a new tab) so the handle is released when the block ends:
try{ using Document document = Document.Open("input_forms_detection.pdf");Configure AI labeling
Use the default Local provider and turn on labeling with EnableAiLabeling = true:
DocumentSettings settings = document.Settings;
// Turn on labeling and drop detections the model judges to be false positives FormLabelingSettings formLabeling = settings.FormLabelingSettings; formLabeling.EnableAiLabeling = true; formLabeling.EnableAiRemoveFalsePositives = true;
// Optional: constrain labels to a known vocabulary formLabeling.CandidateLabels = "First name, Last name, Date of birth, Signature";Detect and label form fields
Create a vision instance from the document with Vision.Set(document), then call DetectForms(). The same call covers both modes; it includes labels because AI labeling is enabled:
Vision vision = Vision.Set(document); string formsJson = vision.DetectForms();Write the JSON result to a file for downstream processing:
File.WriteAllText("output.json", formsJson);}catch (NutrientException e){ Console.Error.WriteLine($"Error: {e.Message}"); Environment.Exit(1);}Match labels to a vocabulary
Free-form labels can vary between runs (“First name” vs. “Given name”), which makes them hard to map to a database or template. Supply a vocabulary of preferred labels with the CandidateLabels property, as shown above, and the model maps each field to one when it fits. If no label fits, it invents a concise new label.
Pass the labels as newline- or comma-separated text. A matched label uses the casing you supplied, and each field’s labelSource records whether the label was matched or invented. Leave the candidate labels empty, which is the default, for free-form labeling.
Understand the output
DetectForms() returns structured JSON. The elements array holds one form element per page. Each form element includes its pageNumber and a fields list, so fields from a multi-page document stay grouped by the page they came from. Each field includes:
fieldType— The detected type:Text,Checkbox, orSignature.bounds— The bounding box of the field on the page.confidence— The detection confidence for the field.label— The AI-assigned semantic label (for example, “First name”). Present only when AI labeling is enabled.labelSource—matchedorinvented, present only when a candidate vocabulary was supplied.id— A unique identifier for the field.
Handle errors
Vision API throws a NutrientException when detection or labeling fails.
Common failure scenarios include:
- The document can’t be read due to path or permission issues
- The page produces no renderable image
- The form detection model is missing or inaccessible, or the feature isn’t licensed
- Required local model files cannot be downloaded or read from the SDK resource cache
- Available CPU/GPU memory is insufficient for the local model
In production code:
- Catch
NutrientException. - Return a clear error message.
- Log failure details for debugging.
- Consider running detection only, with labeling disabled, as a fallback when the local model is unavailable.
Conclusion
The workflow for labeling form fields with a vision model is:
- Open the source document with a
usingstatement for automatic resource cleanup. - Use the built-in Local provider and set
EnableAiLabelingtotrueon the form labeling settings. - Create a vision instance with
Vision.Set(). - Call
DetectForms()to detect every field, assign a semantic label, and export the result as JSON. - Write the JSON to a file for indexing, validation, or downstream processing.
- Handle
NutrientExceptionfailures for robust error recovery.
Labeling adds semantic meaning when a vision model is available. For offline detection with no model contacted, refer to the extract form fields from an image guide. To produce a fillable PDF instead of data, refer to the detect and add form fields guide.
For related image extraction workflows, refer to the .NET SDK guides.
Download the sample package to explore form-field labeling.
For an external OpenAI-compatible vision model server, refer to the custom endpoint version of this sample.