2024-11-07 23:05:19 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2024-11-08 09:03:47 +03:00
|
|
|
|
using SnippingTool;
|
2024-11-13 23:42:28 +03:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Application = Microsoft.Office.Interop.Excel.Application;
|
2024-11-07 23:05:19 +03:00
|
|
|
|
|
2024-11-06 23:43:00 +03:00
|
|
|
|
namespace RhSolutions.Tools;
|
|
|
|
|
|
2024-11-09 22:59:46 +03:00
|
|
|
|
internal class OcrTool : ITool
|
2024-11-06 23:43:00 +03:00
|
|
|
|
{
|
2024-11-09 22:34:58 +03:00
|
|
|
|
private IOcrClient client = RhSolutionsAddIn.ServiceProvider.GetService<IOcrClient>();
|
2024-11-13 23:42:28 +03:00
|
|
|
|
private Application app = RhSolutionsAddIn.Excel;
|
|
|
|
|
private string xFolderId = RhSolutionsAddIn.Configuration["x-folder-id"];
|
|
|
|
|
private string apiKey = RhSolutionsAddIn.Configuration["apiKey"];
|
2024-11-09 22:34:58 +03:00
|
|
|
|
|
2024-11-13 23:42:28 +03:00
|
|
|
|
public async void Execute()
|
2024-11-07 23:05:19 +03:00
|
|
|
|
{
|
2024-11-09 22:34:58 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
RhSolutionsAddIn.Excel.Visible = false;
|
|
|
|
|
Task.Run(async delegate
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(250);
|
|
|
|
|
}).Wait();
|
|
|
|
|
|
|
|
|
|
string shot = Snipper.SnipBase64();
|
2024-11-13 23:42:28 +03:00
|
|
|
|
RhSolutionsAddIn.Excel.Visible = true;
|
|
|
|
|
|
|
|
|
|
if (shot != null)
|
|
|
|
|
{
|
2024-11-14 00:23:38 +03:00
|
|
|
|
IEnumerable<object[,]> tables = await client.ProcessImage(shot);
|
2024-12-16 18:05:43 +03:00
|
|
|
|
if (tables.Count() != 0)
|
2024-11-13 23:42:28 +03:00
|
|
|
|
{
|
|
|
|
|
foreach (var table in tables)
|
|
|
|
|
{
|
|
|
|
|
int rowCount = table.GetLength(0);
|
|
|
|
|
int columnCount = table.GetLength(1);
|
|
|
|
|
|
|
|
|
|
Range currentCell = app.ActiveCell;
|
|
|
|
|
Range tableRange = app.ActiveSheet.Range(currentCell,
|
|
|
|
|
app.ActiveSheet.Cells(currentCell.Row + rowCount - 1, currentCell.Column + columnCount - 1));
|
|
|
|
|
|
|
|
|
|
if (app.WorksheetFunction.CountA(tableRange) > 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(@"На листе отсустствует диапазон для вставки распознанной таблицы.Попробуйте в другом месте или на пустом листе.",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Error);
|
|
|
|
|
RhSolutionsAddIn.Excel.Visible = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
tableRange.Borders.LineStyle = XlLineStyle.xlContinuous;
|
|
|
|
|
|
|
|
|
|
for (int row = 0; row < rowCount; row++)
|
|
|
|
|
for (int column = 0; column < columnCount; column++)
|
|
|
|
|
{
|
|
|
|
|
Range excelCell = app.ActiveSheet.Cells(currentCell.Row + row,
|
|
|
|
|
currentCell.Column + column);
|
2024-11-14 22:44:22 +03:00
|
|
|
|
excelCell.Value2 = table[row, column];
|
2024-11-13 23:42:28 +03:00
|
|
|
|
}
|
2024-11-14 22:44:22 +03:00
|
|
|
|
|
|
|
|
|
foreach (Range row in tableRange.Rows)
|
|
|
|
|
{
|
|
|
|
|
row.EntireRow.AutoFit();
|
|
|
|
|
}
|
|
|
|
|
foreach (Range column in tableRange.Columns)
|
|
|
|
|
{
|
|
|
|
|
column.EntireColumn.AutoFit();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 23:42:28 +03:00
|
|
|
|
app.ActiveSheet.Cells(currentCell.Row + rowCount + 1, currentCell.Column).Activate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-16 18:05:43 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(@"В выделенном фрагменте не распознано ни одной таблицы.",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Information);
|
|
|
|
|
RhSolutionsAddIn.Excel.Visible = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-13 23:42:28 +03:00
|
|
|
|
}
|
2024-11-09 22:34:58 +03:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
2024-11-07 23:05:19 +03:00
|
|
|
|
{
|
2024-11-09 22:34:58 +03:00
|
|
|
|
RhSolutionsAddIn.Excel.Visible = true;
|
|
|
|
|
}
|
2024-11-06 23:43:00 +03:00
|
|
|
|
}
|
2024-11-09 22:59:46 +03:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-11-06 23:43:00 +03:00
|
|
|
|
}
|