Support multiple pages of credit card statement pdf
This commit is contained in:
parent
6b5aefd70b
commit
2e05e7833b
4 changed files with 34 additions and 2 deletions
|
|
@ -5,6 +5,7 @@ namespace Schaad.Finance.Api
|
||||||
{
|
{
|
||||||
public interface IPdfParsingService
|
public interface IPdfParsingService
|
||||||
{
|
{
|
||||||
|
int GetTotalPages(string file);
|
||||||
IReadOnlyList<string> ExtractText(string file, int page);
|
IReadOnlyList<string> ExtractText(string file, int page);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -44,5 +44,15 @@ namespace Schaad.Finance.Tests
|
||||||
Assert.That(transactions.Count, Is.EqualTo(2));
|
Assert.That(transactions.Count, Is.EqualTo(2));
|
||||||
Assert.That(transactions.Sum(t => t.Amount), Is.EqualTo(37.20m));
|
Assert.That(transactions.Sum(t => t.Amount), Is.EqualTo(37.20m));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//[Test]
|
||||||
|
//public void ReadFile_CembraPdf2()
|
||||||
|
//{
|
||||||
|
// var service2 = new CreditCardStatementService(new PdfParsingService());
|
||||||
|
// var transactions = service2.ReadFile(CreditCardProvider.CembraPdf, @"D:\Desktop\20190923_Cembra.pdf", System.Text.Encoding.UTF8);
|
||||||
|
|
||||||
|
// Assert.That(transactions.Count, Is.EqualTo(2));
|
||||||
|
// Assert.That(transactions.Sum(t => t.Amount), Is.EqualTo(37.20m));
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,20 @@ namespace Schaad.Finance.Services
|
||||||
|
|
||||||
private IReadOnlyList<CreditCardTransaction> ParseCembraPdf(string filePath)
|
private IReadOnlyList<CreditCardTransaction> ParseCembraPdf(string filePath)
|
||||||
{
|
{
|
||||||
var text = pdfParsingService.ExtractText(filePath, 2);
|
var transactionList = new List<CreditCardTransaction>();
|
||||||
|
var totalPages = pdfParsingService.GetTotalPages(filePath);
|
||||||
|
|
||||||
|
for (int i = 2; i < totalPages; i++)
|
||||||
|
{
|
||||||
|
transactionList.AddRange(ParseCembraPdfPage(filePath, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return transactionList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IReadOnlyList<CreditCardTransaction> ParseCembraPdfPage(string filePath, int page)
|
||||||
|
{
|
||||||
|
var text = pdfParsingService.ExtractText(filePath, page);
|
||||||
|
|
||||||
var transactionList = new List<CreditCardTransaction>();
|
var transactionList = new List<CreditCardTransaction>();
|
||||||
|
|
||||||
|
|
@ -109,7 +122,7 @@ namespace Schaad.Finance.Services
|
||||||
var transactionText = nextPart.part;
|
var transactionText = nextPart.part;
|
||||||
|
|
||||||
nextPart = GetNextPartUntilSpace(nextPart.rest, 2);
|
nextPart = GetNextPartUntilSpace(nextPart.rest, 2);
|
||||||
var amount = nextPart.part;
|
var amount = nextPart.part.Replace("'", "");
|
||||||
|
|
||||||
return new CreditCardTransaction
|
return new CreditCardTransaction
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,14 @@ namespace Schaad.Finance.Services
|
||||||
{
|
{
|
||||||
public class PdfParsingService : IPdfParsingService
|
public class PdfParsingService : IPdfParsingService
|
||||||
{
|
{
|
||||||
|
public int GetTotalPages(string file)
|
||||||
|
{
|
||||||
|
var document = new PdfDocument();
|
||||||
|
document.LoadFromFile(file);
|
||||||
|
var pages = document.Pages.Count;
|
||||||
|
return pages;
|
||||||
|
}
|
||||||
|
|
||||||
public IReadOnlyList<string> ExtractText(string file, int page)
|
public IReadOnlyList<string> ExtractText(string file, int page)
|
||||||
{
|
{
|
||||||
var document = new PdfDocument();
|
var document = new PdfDocument();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue