Skip to content

Form Recognizer

With the Form Recognizer tool of ComPDFKit Document AI, you can recognize all the forms in Images. The following examples show how to upload a JPG file and run the Form Recognizer feature using Java, PHP, C#, Python, and Swift programming languages. Then, output a JSON file.

Java
// Create a client
CPDFClient client = new CPDFClient(publicKey,secretKey);

// Create a task
// Create an example of a DocumentAI Form Recognizer task
CPDFCreateTaskResult result = client.createTask(CPDFDocumentAIEnum.TABLEREC);

// Get a task id
String taskId = result.getTaskId();

// File handling parameter settings
CPDFFormRecognizerParameter fileParameter = new CPDFFormRecognizerParameter();
fileParameter.setLang("auto");

// Upload files
client.uploadFile(new File("test.jpg"), taskId, fileParameter);

// Execute task
client.executeTask(taskId);

// Query TaskInfo
CPDFTaskInfoResult taskInfo = client.getTaskInfo(taskId);
PHP
// Create a client
$client = new CPDFClient('public_key', 'secret_key');

// Create a task
// Create an example of a DocumentAI Form Recognizer task
$taskInfo = $client->createTask(CPDFDocumentAI::TABLEREC);

// File handling parameter settings
$file = $client->addFile('test.jpg')
    ->setLang('auto');

// Upload files
$fileInfo = $file->uploadFile($taskInfo['taskId']);

// Execute task
$client->executeTask($taskInfo['taskId']);

// Query TaskInfo
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
C#
// Create a client
CPDFClient client = new CPDFClient(publicKey,secretKey);

// Create a task
// Create an example of a DocumentAI Form Recognizer task
CPDFCreateTaskResult result = client.CreateTask(CPDFDocumentAIEnum.TABLEREC);

// Get a task id
string taskId = result.TaskId;

// File handling parameter settings
CPDFFormRecognizerParameter fileParameter = new CPDFFormRecognizerParameter();

// Upload files
client.UploadFile(new FileInfo("test.jpg"), taskId, fileParameter);

// Execute task
client.ExecuteTask(taskId);

// Query TaskInfo
CPDFTaskInfoResult taskInfo = client.GetTaskInfo(taskId);
Python
# Create a client
client = CPDFClient(public_key, secret_key)

# Create a task
# Create an example of a DocumentAI Form Recognizer task
create_task_result = client.create_task(CPDFDocumentAIEnum.TABLEREC)

# Get a task id
task_id = create_task_result.task_id

# File handling parameter settings
file_parameter = CPDFFormRecognizerParameter()

# Upload files
client.upload_file('test.jpg', task_id, file_parameter)

# Execute task
client.execute_task(task_id)

# Query TaskInfo
task_info = client.get_task_info(task_id)
Swift
// Create a client
let client: CPDFClient = CPDFClient(publicKey: public_key, secretKey: secret_key)

Task { @MainActor in
    // Create a task
    // Create an example of a DocumentAI Form Recognizer task
    let taskModel = await client.createTask(url: CPDFDocumentAI.TABLEREC, language: .english)
    
    // Get a task id
    let taskId = taskModel?.taskId ?? ""

    // Upload files
    let path = Bundle.main.path(forResource: "IMG_00001(2)", ofType: "jpg")
    let uploadFileModel = await client.uploadFile(filepath: path ?? "", language: .english, params: [:], taskId: taskId)
    
    // Execute task
    let _ = await client.processFiles(taskId: taskId, language: .english)
    
    // Query TaskInfo
    let taskInfoModel = await client.getTaskInfo(taskId: taskId, language: .english)
}

Needed Parameters:

  • lang: Supported types and definitions.
    • auto - Automatic classification language.

    • english - English.

    • chinese - Simplified Chinese.

    • chinese_tra - Traditional Chinese.

    • korean - Korean.

    • japanese - Japanese.

    • latin - Latin.

    • devanagari - Sanskrit alphabet.

Result:

File TypeDescription
.jsonForm Recognition results.

Content:

ParameterDescription
costTime spent on form identification.
typeTypes of form.
angleThe angle at which the form is rotated.
widthWidth of the form.
heightHeight of the form.
rowsNumber of rows in the form.
colsNumber of columns in the form.
positionThe rectangular box position of the form.
height_of_rowsHeight of each row of the form.
width_of_colsWidth of each column of the form.
table_cellsInformation about all cells in the form.
table_cells: start_rowThe start row of a cell.
table_cells: end_rowThe end row of a cell.
table_cells: start_colThe start column of a cell.
table_cells: end_colThe end column of a cell.
table_cells: textText in cells.
table_cells: positionRectangular box position information for cells.
table_cells: linesThe text lines included in the cell.
table_cells: lines: textThe text line.
table_cells: lines: scoreThe score is identified by the text line.
table_cells: lines: positionText line position information.