public void ExtractEnergyPurchased()
        {
            FileStream documentContents = File.OpenRead("Data/billEnergyPurchased.pdf");
            var        billDocument     = new BillDocument {
                AccountId = ACCOUNT_ID, PublishingDate = new LocalDate(2017, 10, 18)
            };
            int actual = BillDocumentServiceImpl.ExtractEnergyPurchasedOrSold(billDocument, documentContents);

            actual.Should().Be(535);
        }
        public async Task <int> fetchEnergyPurchasedOrSoldKWh(LocalDate billingIntervalEndDate)
        {
            LOGGER.Info("Fetching index of bill PDFs");
            IHtmlDocument billDocumentIndex = await client.billDocuments.fetchBillDocumentIndex();

            IEnumerable <BillDocument> billDocuments = findBillDocuments(billDocumentIndex);
            BillDocument billDocument = findBillDocumentForBillingInterval(billDocuments, billingIntervalEndDate);

            LOGGER.Info("Downloading bill PDF from {0}", billDocument.publishingDate);
            using Stream documentContents = await client.billDocuments.fetchBillDocument(billDocument);

            return(extractEnergyPurchasedOrSold(billDocument, documentContents));
        }
        internal static int extractEnergyPurchasedOrSold(BillDocument billDocument, Stream documentContents)
        {
            using var pdfReader = new PdfReader(documentContents);
            string pageText = PdfTextExtractor.GetTextFromPage(pdfReader, 1, new SimpleTextExtractionStrategy());
            Match  match    = USAGE_PATTERN.Match(pageText);

            if (!match.Success)
            {
                throw new OrangeRocklandException(
                          $"Could not parse Total Usage KWH from bill published on {billDocument.publishingDate:d}");
            }

            return(int.Parse(match.Groups[1].Value));
        }