Justerat lite i gränssnittet

This commit is contained in:
2026-04-26 13:38:54 +02:00
parent 1e8098996c
commit 3ec543ed3d
4 changed files with 338 additions and 202 deletions

View File

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
namespace StockHistory;
public static class PdfPigLetterExtractor
{
public static List<string> ExtractLines(List<PdfSida> pages, double lineTolerance = 3.0, double wordSpacingFactor = 0.5)
{
var result = new List<string>();
foreach (var page in pages)
{
var letters = page.FoundLetters;
// 1. Gruppér tecken till rader baserat på Y-position
var lines = letters
.GroupBy(l => Math.Round(l.BoundingBox.Top / lineTolerance))
.OrderBy(g => g.Key);
foreach (var lineGroup in lines)
{
var lineLetters = lineGroup
.OrderBy(l => l.BoundingBox.Left)
.ToList();
// 2. Bygg ord genom att titta på avstånd mellan tecken
var words = BuildWords(lineLetters, wordSpacingFactor);
result.Add(string.Join(" ", words));
}
}
return result;
}
private static List<string> BuildWords(List<Letter> letters, double spacingFactor)
{
var words = new List<string>();
var currentWord = new List<Letter>();
for (int i = 0; i < letters.Count; i++)
{
var letter = letters[i];
currentWord.Add(letter);
if (i < letters.Count - 1)
{
var next = letters[i + 1];
// Avstånd mellan tecken
double gap = next.BoundingBox.Left - letter.BoundingBox.Right;
// Typisk teckenbredd
double avgWidth = letter.BoundingBox.Width;
// Om gapet är större än spacingFactor * teckenbredd → nytt ord
if (gap > avgWidth * spacingFactor)
{
//words.Add(new string(currentWord.Select(l => l.Value).ToArray()));
words.Add(string.Concat(currentWord.Select(l => l.Value)));
currentWord.Clear();
}
}
}
if (currentWord.Count > 0)
{
//words.Add(new string(currentWord.Select(l => l.Value).ToArray()));
words.Add(string.Concat(currentWord.Select(l => l.Value)));
}
return words;
}
}

View File

@ -7,6 +7,10 @@
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="PdfPigLetterExtractor.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.6" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.6" />

View File

@ -44,6 +44,9 @@
CommissionHeader = new ColumnHeader(); CommissionHeader = new ColumnHeader();
AmountToPayHeader = new ColumnHeader(); AmountToPayHeader = new ColumnHeader();
btnClose = new Button(); btnClose = new Button();
btnAnalysMany = new Button();
txtVald = new TextBox();
lblAntal = new Label();
SuspendLayout(); SuspendLayout();
// //
// btnChooseFile // btnChooseFile
@ -74,7 +77,7 @@
btnAnalysera.BackColor = Color.FromArgb(128, 255, 128); btnAnalysera.BackColor = Color.FromArgb(128, 255, 128);
btnAnalysera.FlatStyle = FlatStyle.Flat; btnAnalysera.FlatStyle = FlatStyle.Flat;
btnAnalysera.Font = new Font("Segoe UI", 13F); btnAnalysera.Font = new Font("Segoe UI", 13F);
btnAnalysera.Location = new Point(26, 86); btnAnalysera.Location = new Point(26, 126);
btnAnalysera.Name = "btnAnalysera"; btnAnalysera.Name = "btnAnalysera";
btnAnalysera.Size = new Size(143, 37); btnAnalysera.Size = new Size(143, 37);
btnAnalysera.TabIndex = 2; btnAnalysera.TabIndex = 2;
@ -84,10 +87,10 @@
// //
// txtFound // txtFound
// //
txtFound.Location = new Point(26, 149); txtFound.Location = new Point(26, 180);
txtFound.Multiline = true; txtFound.Multiline = true;
txtFound.Name = "txtFound"; txtFound.Name = "txtFound";
txtFound.Size = new Size(468, 334); txtFound.Size = new Size(468, 303);
txtFound.TabIndex = 3; txtFound.TabIndex = 3;
// //
// btnSaveToDb // btnSaveToDb
@ -95,7 +98,7 @@
btnSaveToDb.BackColor = Color.FromArgb(128, 255, 128); btnSaveToDb.BackColor = Color.FromArgb(128, 255, 128);
btnSaveToDb.FlatStyle = FlatStyle.Flat; btnSaveToDb.FlatStyle = FlatStyle.Flat;
btnSaveToDb.Font = new Font("Segoe UI", 13F); btnSaveToDb.Font = new Font("Segoe UI", 13F);
btnSaveToDb.Location = new Point(197, 86); btnSaveToDb.Location = new Point(351, 126);
btnSaveToDb.Name = "btnSaveToDb"; btnSaveToDb.Name = "btnSaveToDb";
btnSaveToDb.Size = new Size(143, 37); btnSaveToDb.Size = new Size(143, 37);
btnSaveToDb.TabIndex = 4; btnSaveToDb.TabIndex = 4;
@ -168,12 +171,36 @@
btnClose.UseVisualStyleBackColor = false; btnClose.UseVisualStyleBackColor = false;
btnClose.Click += btnClose_Click; btnClose.Click += btnClose_Click;
// //
// btnAnalysMany
//
btnAnalysMany.Location = new Point(0, 0);
btnAnalysMany.Name = "btnAnalysMany";
btnAnalysMany.Size = new Size(75, 23);
btnAnalysMany.TabIndex = 2;
//
// txtVald
//
txtVald.Location = new Point(0, 0);
txtVald.Name = "txtVald";
txtVald.Size = new Size(100, 27);
txtVald.TabIndex = 1;
//
// lblAntal
//
lblAntal.Location = new Point(0, 0);
lblAntal.Name = "lblAntal";
lblAntal.Size = new Size(100, 23);
lblAntal.TabIndex = 0;
//
// frmStockHistoryInit // frmStockHistoryInit
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
BackColor = Color.LightGray; BackColor = Color.LightGray;
ClientSize = new Size(1413, 553); ClientSize = new Size(1413, 553);
Controls.Add(lblAntal);
Controls.Add(txtVald);
Controls.Add(btnAnalysMany);
Controls.Add(btnClose); Controls.Add(btnClose);
Controls.Add(lwTransactions); Controls.Add(lwTransactions);
Controls.Add(btnSaveToDb); Controls.Add(btnSaveToDb);
@ -205,5 +232,8 @@
private ColumnHeader CommissionHeader; private ColumnHeader CommissionHeader;
private ColumnHeader AmountToPayHeader; private ColumnHeader AmountToPayHeader;
private Button btnClose; private Button btnClose;
private Button btnAnalysMany;
private TextBox txtVald;
private Label lblAntal;
} }
} }

View File

@ -12,14 +12,15 @@ using System.Drawing.Drawing2D;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
namespace StockHistory namespace StockHistory;
{
public partial class frmStockHistoryInit : Form public partial class frmStockHistoryInit : Form
{ {
private readonly IPdfOpener _pdfOpener; private readonly IPdfOpener _pdfOpener;
private readonly IPdfFormatter _pdfFormatter; private readonly IPdfFormatter _pdfFormatter;
private readonly ITransactionNotesServices _transactionNotes; private readonly ITransactionNotesServices _transactionNotes;
private List<PdfSida> _pdfSidor = new(); private List<PdfSida> _pdfSidor = new();
private List<PdfSida> localPdfSida = new();
public frmStockHistoryInit( public frmStockHistoryInit(
IPdfOpener pdfOpener, IPdfOpener pdfOpener,
@ -32,6 +33,7 @@ namespace StockHistory
_pdfFormatter = pdfFormatter; _pdfFormatter = pdfFormatter;
_transactionNotes = transactionNotes; _transactionNotes = transactionNotes;
btnAnalysera.Enabled = false; btnAnalysera.Enabled = false;
Shown += async (s, e) => await LoadBusinessAsync(); Shown += async (s, e) => await LoadBusinessAsync();
} }
@ -42,7 +44,7 @@ namespace StockHistory
{ {
ofdFiler.Title = "Välj en PDF-fil"; ofdFiler.Title = "Välj en PDF-fil";
ofdFiler.FileName = ""; ofdFiler.FileName = "";
ofdFiler.Filter = "PDF-filer (*.pdf)|*.pdf|Alla filer (*.*)|*.*"; ofdFiler.Filter = "PDF-filer (*.pdf)|*.pdf|ExcelTrans (*.csv)|*.csv|Alla filer (*.*)|*.*";
var result = ofdFiler.ShowDialog(); var result = ofdFiler.ShowDialog();
if (result == DialogResult.OK) if (result == DialogResult.OK)
@ -57,7 +59,9 @@ namespace StockHistory
{ {
_pdfOpener.OpenPdf(lblFileName.Text); _pdfOpener.OpenPdf(lblFileName.Text);
_pdfSidor = _pdfOpener.PdfSidor; _pdfSidor = _pdfOpener.PdfSidor;
btnAnalysera.Enabled = true; btnAnalysera.Enabled = true;
} }
else else
{ {
@ -65,9 +69,27 @@ namespace StockHistory
} }
} }
private void btnAnalysera_Click(object sender, EventArgs e) private void btnAnalysera_Click(object sender, EventArgs e)
{ {
string PdfResult = _pdfFormatter.ExtractFormattedTextUsingWords(_pdfSidor); string PdfResult=string.Empty;
if (lblFileName.Text.ToLower().Contains("avanza"))
{
//var Result = PdfPigLetterExtractor.ExtractLines(_pdfSidor);
//SelectPdfLetterData(Result);
}
else
{
PdfResult = _pdfFormatter.ExtractFormattedTextUsingWords(_pdfSidor);
SelectPdfData(PdfResult);
}
btnAnalysera.Enabled = false;
lblFileName.Text = string.Empty;
}
private void SelectPdfData(string PdfResult)
{
txtFound.Text = string.Empty; txtFound.Text = string.Empty;
List<string> result = PdfResult.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList(); List<string> result = PdfResult.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
//List<string> selected = new(); //List<string> selected = new();
@ -92,8 +114,6 @@ namespace StockHistory
} }
} }
btnAnalysera.Enabled = false;
lblFileName.Text = string.Empty;
} }
private async void btnSaveToDb_Click(object sender, EventArgs e) private async void btnSaveToDb_Click(object sender, EventArgs e)
@ -214,5 +234,6 @@ namespace StockHistory
{ {
this.Close(); this.Close();
} }
} }
}