GetTableColumnCount Method (GdPictureOCR)
                                 
                                
                                    
                                        In This Topic
                                    
                                
                                
            Returns the number of columns in a specified table.
            
            
            
            Syntax
            
            
            
            
            'Declaration
 
Public Function GetTableColumnCount( _
   ByVal  As String, _
   ByVal  As Integer _
) As Integer
             
        
            
            public int GetTableColumnCount( 
   string ,
   int 
)
             
        
            
            public function GetTableColumnCount( 
    : String;
    : Integer
): Integer; 
             
        
            
            public function GetTableColumnCount( 
    : String,
    : int
) : int;
             
        
            
            public: int GetTableColumnCount( 
   string* ,
   int 
) 
             
        
            
            public:
int GetTableColumnCount( 
   String^ ,
   int 
) 
             
        
             
        
            Parameters
- OCRResultID
- The unique result identifier of the executed OCR process obtained by the RunOCR method.
- TableIdx
- The 0-based index of the detected table within the specified OCR result. It must be a value between 0 and GetTableCount(OCRResultID) - 1.
Return Value
The number of columns.
 
            
            
            
            
            
            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()}");
}
	 
	
 
 
            
            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