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()}");
}