2024-11-07 23:05:19 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2024-11-08 09:03:47 +03:00
|
|
|
|
using SnippingTool;
|
2024-11-09 22:34:58 +03:00
|
|
|
|
using OcrClient.Services;
|
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)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<object[,]> tables = await client.ProcessImage(shot, xFolderId, apiKey);
|
|
|
|
|
if (tables != null)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
excelCell.Value2 = table[row,column];
|
|
|
|
|
excelCell.EntireColumn.AutoFit();
|
|
|
|
|
excelCell.EntireRow.AutoFit();
|
|
|
|
|
}
|
|
|
|
|
app.ActiveSheet.Cells(currentCell.Row + rowCount + 1, currentCell.Column).Activate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|