private static string GetFieldText(DocumentData data, string fieldName) { // Get the field from data collection FieldData fieldData = data.GetFieldsByName(fieldName).FirstOrDefault(); // Check if the field data is not null (a field with the fieldName is contained in data collection) // and check if the field data contains the text return(fieldData != null && fieldData.PageArea is PageTextArea ? (fieldData.PageArea as PageTextArea).Text : null); }
public static void Run() { // Define a "price" field TemplateField priceField = new TemplateField( new TemplateRegexPosition("\\$\\d+(.\\d+)?"), "Price"); // Define a "email" field TemplateField emailField = new TemplateField( new TemplateRegexPosition("[a-z]+\\@[a-z]+.[a-z]+"), "Email"); // Create a template Template template = new Template(new TemplateItem[] { priceField, emailField }); // Create an instance of Parser class using (Parser parser = new Parser(Constants.SampleInvoicePdf)) { // Parse the document by the template DocumentData data = parser.ParseByTemplate(template); // Print prices Console.WriteLine("Prices:"); foreach (FieldData field in data.GetFieldsByName("Price")) { PageTextArea area = field.PageArea as PageTextArea; Console.WriteLine(area == null ? "Not a template field" : area.Text); } // Print emails Console.WriteLine("Emails:"); foreach (FieldData field in data.GetFieldsByName("Email")) { // As we have defined only text fields in the template, // we cast PageArea property value to PageTextArea PageTextArea area = field.PageArea as PageTextArea; Console.WriteLine(area == null ? "Not a template field" : area.Text); } } }