GdPicture.NET.14.API
GdPicture14 Namespace / GdPictureOCR Class / GetTableColumnCount Method
The unique result identifier of the executed OCR process obtained by the GdPictureOCR.RunOCR method.
The 0-based index of the detected table within the specified OCR result. It must be a value between 0 and GdPictureOCR.GetTableCount(OCRResultID) - 1.
Example





In This Topic
GetTableColumnCount Method (GdPictureOCR)
In This Topic
Returns the number of columns in a specified table.
Syntax
'Declaration
 
Public Function GetTableColumnCount( _
   ByVal OCRResultID As String, _
   ByVal TableIdx As Integer _
) As Integer
public int GetTableColumnCount( 
   string OCRResultID,
   int TableIdx
)
public function GetTableColumnCount( 
    OCRResultID: String;
    TableIdx: Integer
): Integer; 
public function GetTableColumnCount( 
   OCRResultID : String,
   TableIdx : int
) : int;
public: int GetTableColumnCount( 
   string* OCRResultID,
   int TableIdx
) 
public:
int GetTableColumnCount( 
   String^ OCRResultID,
   int TableIdx
) 

Parameters

OCRResultID
The unique result identifier of the executed OCR process obtained by the GdPictureOCR.RunOCR method.
TableIdx
The 0-based index of the detected table within the specified OCR result. It must be a value between 0 and GdPictureOCR.GetTableCount(OCRResultID) - 1.

Return Value

The number of columns.
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