FormSuite for Invoices v2.1 - Updated
Tutorial: Create Your First Project
User Guide > Getting Started > Tutorial: Create Your First Project

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.

Prerequisites:

Steps:

  1. Create a new C# Console App in Visual Studio.
  2. In the project's Build properties, set the Platform target to x64.
  3. Add the Accusoft.FSInvoices2.Net component as a Reference to the project. If you have done a typical installation, it can be found here:
         C:\Users\Public\Documents\Accusoft\FSInvoices\V2.0\Components\DotNet
  4. Add the System.Runtime.Serialization component as a Reference to the project. This tutorial needs it to read and deserialize xml formatted vendor data from file.
  5. Open the file Program.cs from your project and add the following using statements near the top. Xml and Serialization are used in this tutorial to read and deserialize xml formatted vendor data from file, and are not needed if you have other means to obtain the data.
    C#
    Copy Code
    ...
    using System.Xml;
    using System.Runtime.Serialization;
    using Accusoft.FSInvoices;
    ...
  6. Add a Licensing object to the Program class. If you have an OEM license, you can initialize it in the constructor as shown in the commented code.
    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…");
        }
        ...
    }
  7. Add the following code to the function Main. It creates and initializes the processor, analyzes the input files, and outputs the results. In subsequent steps, we will define the missing functions that are used here.
    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();
    }
  8. Add the function to create the FormDefinition we want to use. This defines the form fields and line item table columns in which you are interested. You can define your own form definition (typically read from a file or database), but for brevity we use the default in this tutorial:
    C#
    Copy Code
    ...
    
    static FormDefinition GetMyFormDefinition()
    {
        return FormDefinition.CreateDefaultDefinition();
    }
    ...
  9. Add the function to create the CompanyData structure containing your company data. This is typically read from a file or database, but for educational purposes we have hard-coded it here:
    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;
    }
    ...
  10. Add the function to get our vendor list. This is typically read from a file or database, and as an example we read it from an xml formatted file that comes with the demo:
    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;
    }
    ...
  11. Add the function to write any errors to the console. This is just for feedback purposes.
    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();
        }
    }
    ...
  12. Add the function to write the results (extracted form fields and line items) to the console. This is just for feedback purposes.
    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();
            }
        }
    }
    ...
  13. Finally, we need to copy all dependencies. Find the folder where the executable from your project will be created, and copy all files and folders that are located in the Distributables folder into that folder. If you have done a typical installation, the Distributables folder can be found here:
        C:\Users\Public\Documents\Accusoft\FSInvoices\V2.0\Distributables
  14. Build and run the project.