private void GetInvoiceKeyValuePairs(ExtractedPage page, InvoicePage invPage) { foreach (var kv in page.KeyValuePairs) { if (kv.Key.Count > 0) { if (FormOutputToInvoice.ContainsKey(kv.Key[0].Text)) { string dictionaryValue = FormOutputToInvoice[kv.Key[0].Text]; // Normally we will have to write a humoungous switch statement to set the value for the correspoding property // Instead, we are takning advantage of reflection to set the value to the property /* switch (dictionaryValue) * { * case "InvoiceDate": * invPage.InvoiceDate = kv.Value[0].Text; * break; * case "InvoiceNumber": * invPage.InvoiceNumber = kv.Value[0].Text; * break; * // and SO ON * default: * break; * } * */ // Use Reflection to get a reference to the property based on name of the property as a string PropertyInfo piInstance = typeof(InvoicePage).GetProperty(FormOutputToInvoice[kv.Key[0].Text]); if (kv.Value.Count > 0) { //TODO: Right now, I am changing all properties to string type to test. // Change these data types back and see how to type cast kv.Value[0].Text to the correct datatype and set the value // Concatenate the values if more than one value is there for a key, for example FROM and TO // using a linq expression here to concatenate the values instead of a for loop /* StringBuilder sb = new StringBuilder(); * foreach (var s in kv.Value ) * { * sb.Append(s.Text); * } * piInstance.SetValue(invPage, sb.ToString(), null); */ piInstance.SetValue(invPage, string.Join(", ", kv.Value.Select(v => v.Text)), null); // If there is only one value for a given key, the following code would have been sufficient //piInstance.SetValue(invPage, kv.Value[0].Text, null); } } } } }
private InvoicePage GetInvoicePage(ExtractedPage page) { InvoicePage invPage = new InvoicePage() { PageNumber = page.Number }; // Take the KeyValuePairs from page (returned by Form Recognizer) and set the properties of InvoicePage object GetInvoiceKeyValuePairs(page, invPage); // Take the Tables from page (returned by Form Recognizer) and assign each table info to InvoiceLineItem object // and add that line item object to Invoice Page line item collection GetInvoiceTables(page, invPage); return(invPage); }
private void GetInvoiceTables(ExtractedPage page, InvoicePage invPage) { foreach (var t in page.Tables) { // Each column will have the same number of entries. Take the entries in the first column (any column is OK) int numberOfRows = ((t.Columns.Count > 0 && t.Columns[0].Entries.Count > 0) ? t.Columns[0].Entries.Count : 0); if (numberOfRows > 0) { //Create number of InvoiceLineItems corresponding to number of rows in the table /* List<InvoiceLineItem> invoiceLineItems = new List<InvoiceLineItem>(); * for (int i = 0; i < numberOfRows; i++) * { * invoiceLineItems.Add(new InvoiceLineItem()); * } */ List <InvoiceLineItem> invoiceLineItems = Enumerable.Range(0, numberOfRows) .Select(i => new InvoiceLineItem()) .ToList(); // Fill each row foreach (var c in t.Columns) { if (FormOutputToInvoice.ContainsKey(c.Header[0].Text)) { PropertyInfo piInstance = typeof(InvoiceLineItem).GetProperty(FormOutputToInvoice[c.Header[0].Text]); for (int i = 0; i < numberOfRows; i++) { // As each entry contains only one value, we take the first value piInstance.SetValue(invoiceLineItems[i], c.Entries[i][0].Text, null); } } } invPage.invoiceLineItems = invoiceLineItems; } } }
private object GetInvoiceDetailsfromSample() { Invoice inv = new Invoice(); InvoicePage invPage = new InvoicePage() { InvoiceDate = "2018/12/24", InvoiceNumber = "544", Terms = "NET 30", Due = "2019/01/24", Subtotal = "3000.00", TaxPercent = "6.50", TaxAmount = "195.00", BalanceDue = "3195.00" }; //First line InvoiceLineItem invLineItem = new InvoiceLineItem() { SerialNumber = "1", ItemDescription = "Item 1", Quantity = "2", Price = "600.00", Amount = "1200.00" /*SerialNumber = 1, * ItemDescription = "Item 1", * Quantity = 2, * Price = 600.00, * Amount = 1200.00 */ }; invPage.invoiceLineItems.Add(invLineItem); // Second Line InvoiceLineItem invLineItem2 = new InvoiceLineItem() { SerialNumber = "2", ItemDescription = "Item 2", Quantity = "4", Price = "450.00", Amount = "1800.00" /*SerialNumber = 2, * ItemDescription = "Item 2", * Quantity = 4, * Price = 450.00, * Amount = 1800.00 */ }; invPage.invoiceLineItems.Add(invLineItem2); inv.invoicePages.Add(invPage); var jsonResult = JsonConvert.SerializeObject(inv); return(jsonResult); }