This step-by-step tutorial will guide you to build a complete C# application with Visual Studio that analyzes an example invoice document and outputs the invoice details to the console.
The actual processing code is quite simple, but some information needs to be set-up initially about your company, the vendors from whom you expect to receive invoices, and the invoice fields in which you are interested.
| C# |
Copy Code |
|---|---|
... using System.Xml; using System.Runtime.Serialization; using Accusoft.FSInvoices; ... | |
| C# |
Copy Code |
|---|---|
... class Program { public static Licensing license { get; } = new Licensing(); public Program() { //license.SetSolutionName("YourSolutionName"); //license.SetSolutionKey(12345, 12345, 12345, 12345); //license.SetOEMLicenseKey("2.0.AStringForOEMLicensingContactAccusoftSalesForMoreInformation…"); } ... } | |
| C# |
Copy Code |
|---|---|
... static void Main(string[] args) { // Define the list of invoices we want to process. List<string> fileNames = new List<string> { @"C:\Users\Public\Documents\Accusoft\Common\Images\FSInvoices Beach Software.bmp", }; // Process the files. using (Processor processor = new Processor(license)) { processor.FormDefinition = GetMyFormDefinition(); processor.CompanyData = GetMyCompanyData(); processor.VendorList = GetMyVendorList(); Console.WriteLine("Processing..."); AnalyzeResults results = processor.Analyze(fileNames); Console.WriteLine("Done."); Console.WriteLine(); WriteErrors(results.Errors); WriteResults(results.FormResults); } Console.WriteLine("Press Enter to exit..."); Console.ReadLine(); } | |
| C# |
Copy Code |
|---|---|
... static FormDefinition GetMyFormDefinition() { return FormDefinition.CreateDefaultDefinition(); } ... | |
| C# |
Copy Code |
|---|---|
... static CompanyData GetMyCompanyData() { CompanyData companyData = new CompanyData(); companyData.CompanyId = "21043d2b-5dab-4961-b617-f9323af2af8d"; companyData.CompanyName = "Accusoft"; companyData.CompanyNameAliases = new List<string> { "Pegasus Imaging", "Accusoft Pegasus" }; companyData.Addresses = new List<Address> { new Address() { Line1 = "4001 North Riverside Drive", City = "Tampa", CountrySubDivisionCode = "FL", PostalCode = "33603", }, }; companyData.Phones = new List<PhoneRecord> { new PhoneRecord("phone", "813-875-7575"), new PhoneRecord("fax", "813-875-7705"), }; companyData.EmailAddresses = new List<IEmailAddress> { new EmailAddress("sales@accusoft.com"), new EmailAddress("support@accusoft.com"), }; return companyData; } ... | |
| C# |
Copy Code |
|---|---|
... static List<Vendor> GetMyVendorList() { List<Vendor> vendorList; using (var xml = XmlReader.Create(@"C:\Users\Public\Documents\Accusoft\FSInvoices\V2.0\Demo\VendorList.xml")) { var serializer = new DataContractSerializer(typeof(List<Vendor>)); vendorList = serializer.ReadObject(xml) as List<Vendor>; } return vendorList; } ... | |
| C# |
Copy Code |
|---|---|
... static void WriteErrors(List<AnalyzeError> errors) { if (errors.Count > 0) { Console.WriteLine($"{errors.Count} errors encountered"); foreach (var error in errors) { Console.WriteLine($"Document: {error.FileId}, Page {error.FilePageIndex}:"); Console.WriteLine(error.ErrorMessage); } Console.WriteLine(); } } ... | |
| C# |
Copy Code |
|---|---|
... static void WriteResults(List<FormResult> results) { foreach (var result in results) { Console.WriteLine($"Document: {result.Document[0].FileId}"); Console.WriteLine(); Console.WriteLine("Form Fields:"); foreach (var field in result.Fields) Console.WriteLine($"{field.Name}:\tLabel='{field.Label.Value}', Value='{field.Data.Value}'"); Console.WriteLine(); if (result.Tables.Count > 0) { Console.WriteLine("Line Item Table:"); // Merge header row and line item rows into a single list. var table = result.Tables.First(); var rows = new List<List<string>>(); rows.Add(table.ColumnHeaders.Select(cell => cell.Value).ToList()); rows.AddRange(table.Rows.Select(row => row.Cells.Select(cell => cell.Value).ToList())); // Determine the maximum content width of each column. int columnCount = rows[0].Count(); var columnWidths = Enumerable.Repeat(0, columnCount).ToList(); foreach (var row in rows) for (int i = 0; i < columnCount; i++) columnWidths[i] = Math.Max(columnWidths[i], row[i].Length); // Pretty print the table. foreach (var row in rows) { int columnIndex = 0; foreach (var value in row) { int columnWidth = Math.Min(columnWidths[columnIndex++], 32); var displayValue = value.Length > columnWidth ? value.Substring(0, columnWidth - 3) + "..." : value.PadRight(columnWidth); displayValue = displayValue.Replace('\n', ' '); Console.Write($"| {displayValue} "); } Console.WriteLine("|"); } Console.WriteLine(); } } } ... | |