GdPicture.NET.14.API
GdPicture14 Namespace / GdPictureOCR Class / GetTableCount Method
The unique result identifier of the executed OCR process obtained by the GdPictureOCR.RunOCR method.
Example





In This Topic
GetTableCount Method (GdPictureOCR)
In This Topic
Returns the number of detected tables within a specified OCR result.
Syntax
'Declaration
 
Public Function GetTableCount( _
   ByVal OCRResultID As String _
) As Integer
public int GetTableCount( 
   string OCRResultID
)
public function GetTableCount( 
    OCRResultID: String
): Integer; 
public function GetTableCount( 
   OCRResultID : String
) : int;
public: int GetTableCount( 
   string* OCRResultID
) 
public:
int GetTableCount( 
   String^ OCRResultID
) 

Parameters

OCRResultID
The unique result identifier of the executed OCR process obtained by the GdPictureOCR.RunOCR method.

Return Value

The number of detected tables.
Remarks

This method requires the KVP and Table Processing - Intelligent Redaction component to run.

Example
using GdPictureImaging img = new GdPictureImaging();
int invoiceId = img.CreateGdPictureImageFromHTTP("https://passportpdfapi.com/", "test/invoice.png", 443);
if (img.GetStat() != GdPictureStatus.OK)
{
    Console.WriteLine($"An error occurred during image loading ({img.GetStat()})");
    return;
}
 
using GdPictureOCR ocr = new GdPictureOCR();
ocr.ResourcesFolder = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\GdPicture.Net 14\\redist\\OCR" : "/mnt/c/GdPicture.Net 14/redist/OCR";
ocr.OCRMode = OCRMode.FavorAccuracy;
ocr.AddLanguage(OCRLanguage.French);
ocr.SetImage(invoiceId);
 
string ocrResultId = ocr.RunOCR();
 
if (ocr.GetStat() == GdPictureStatus.OK)
{
    Console.WriteLine();
    for (int tableIdx = 0; tableIdx < ocr.GetTableCount(ocrResultId); tableIdx++)
    {
        int colCount = ocr.GetTableColumnCount(ocrResultId, tableIdx);
        int rowCount = ocr.GetTableRowCount(ocrResultId, tableIdx);
        string[,] tableContent = new string[colCount, rowCount];
        int[] maxLinesInRows = new int[rowCount];
 
        for (int rowIdx = 0; rowIdx < rowCount; rowIdx++)
        {
            for (int colIdx = 0; colIdx < colCount; colIdx++)
            {
                tableContent[colIdx, rowIdx] = ocr.GetTableCellText(ocrResultId, tableIdx, colIdx, rowIdx);
            }
        }
 
        // Compute table formatting
        int[] widestCellInColumn = new int[colCount];
        for (int colIdx = 0; colIdx < colCount; colIdx++)
        for (int rowIdx = 0; rowIdx < rowCount; rowIdx++)
        {
            if (tableContent[colIdx, rowIdx].Length > widestCellInColumn[colIdx])
            {
                widestCellInColumn[colIdx] = tableContent[colIdx, rowIdx].Length;
            }
        }
        int rowLength = widestCellInColumn.Sum() + (3 * colCount);
        string separator = new string('=', (int)Math.Truncate((double)rowLength / 2) - 4);
 
        // Print table
        Console.WriteLine($"{separator} Table {tableIdx} {separator}");
        //if first two column is consired as header, consider the entire row as header
        bool isHorizontalHeaderRow = ocr.IsHeaderCell(ocrResultId, tableIdx, 0, 0) && ocr.IsHeaderCell(ocrResultId, tableIdx, 1, 0);
        for (int rowIdx = 0; rowIdx < rowCount; rowIdx++)
        {
            bool isCurrentRowHeader = ocr.IsHeaderCell(ocrResultId, tableIdx, 0, rowIdx) && ocr.IsHeaderCell(ocrResultId, tableIdx, 1, rowIdx);
            for (int colIdx = 0; colIdx < colCount; colIdx++)
            {
                string prettifiedCell = tableContent[colIdx, rowIdx].Replace(Environment.NewLine, "").PadRight(widestCellInColumn[colIdx]);
                Console.Write($" {prettifiedCell} |{(!isHorizontalHeaderRow ? "|" : null)}");
            }
            Console.WriteLine();
            if (isCurrentRowHeader)
            {
                Console.WriteLine(new string('=', rowLength)); //If header row, write a header separator
            }
        }
        Console.WriteLine($"{separator} Table {tableIdx} {separator}");
        Console.WriteLine();
 
    }
    ocr.ReleaseOCRResult(ocrResultId);
    img.ReleaseGdPictureImage(invoiceId);
}
else
{
    Console.WriteLine($"OCR result : {ocr.GetStat()}");
}
See Also