Create Custom Template - C#
Document Parser SDK sample in C# demonstrating ‘Create Custom Template’
Program.cs
using System;
using ByteScout.DocumentParser;
// This example demonstrates data parsing from an invoice using the custom template.
namespace CustomTemplate
{
class Program
{
static void Main(string[] args)
{
string customTemplate = @".\SampleTemplate.yml";
string inputDocument = @".\SampleInvoice.pdf";
// Create DocumentParser instance
using (DocumentParser documentParser = new DocumentParser("demo", "demo"))
{
Console.WriteLine($"Loading template \"{customTemplate}\"...");
Console.WriteLine();
documentParser.AddTemplate(customTemplate);
Console.WriteLine($"Parsing \"{inputDocument}\"...");
Console.WriteLine();
// Parse invoice data in JSON format
string jsonString = documentParser.ParseDocument(inputDocument, OutputFormat.JSON);
// Display parsed data in console
Console.WriteLine("Parsing results in JSON format:");
Console.WriteLine();
Console.WriteLine(jsonString);
}
Console.WriteLine();
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
}
SampleTemplate.yml
templateName: My Custom Template
templateVersion: 4
templatePriority: 0
detectionRules:
keywords:
- Your Company Name
- Invoice No\.
- TOTAL
objects:
- name: total
objectType: field
fieldProperties:
fieldType: macros
expression: TOTAL{{Spaces}}{{Number}}
dataType: decimal
pageIndex: 0
- name: dateIssued
objectType: field
fieldProperties:
fieldType: macros
expression: Invoice Date {{SmartDate}}
dataType: date
dateFormat: auto-mdy
pageIndex: 0
- name: invoiceId
objectType: field
fieldProperties:
fieldType: macros
expression: Invoice No. {{Digits}}
pageIndex: 0
- name: companyName
objectType: field
fieldProperties:
fieldType: static
expression: Vendor Company
pageIndex: 0
- name: billTo
objectType: field
fieldProperties:
fieldType: rectangle
rectangle:
- 32.25
- 150
- 348
- 70.5
pageIndex: 0
- name: notes
objectType: field
fieldProperties:
fieldType: rectangle
rectangle:
- 32.25
- 227.25
- 531
- 47.25
pageIndex: 0
- name: table1
objectType: table
tableProperties:
start:
expression: Item\s+Quantity\s+Price\s+Total
regex: true
end:
expression: TOTAL
regex: true
row:
expression: ^\s*(?<description>\w+.*)(?<quantity>\d+)\s+(?<unitPrice>\d+\.\d{2})\s+(?<itemTotal>\d+\.\d{2})\s*$
regex: true