public class DocumentData extends Object
FieldData
objects which contain field data from document.
An instance of DocumentData
class is used as return value of Parser.parseByTemplate(Template)
and Parser.parseForm()
methods. See the usage examples there.Constructor and Description |
---|
DocumentData(Iterable<FieldData> fields)
Initializes a new instance of the
FieldData class. |
Modifier and Type | Method and Description |
---|---|
FieldData |
get(int index)
Gets the field data by an index.
|
int |
getCount()
Gets the total number of the fields data.
|
List<FieldData> |
getFieldsByName(String fieldName)
Returns the collection of field data where the name is equal to
fieldName . |
public int getCount()
public FieldData get(int index)
Iteration via all the fields:
// Print all extracted data
for (int i = 0; i < data.getCount(); i++) {
// Print field name
System.out.print(data.get(i).getName() + ": ");
// As we have defined only text fields in the template,
// we cast PageArea property value to PageTextArea
PageTextArea area = data.get(i).getPageArea() instanceof PageTextArea
? (PageTextArea) data.get(i).getPageArea()
: null;
System.out.println(area == null ? "Not a template field" : area.getText());
}
FieldData
class represents field data. Depending on the field PageArea property can contain any
of the inheritors of PageArea
class. For example, Parser.parseForm()
method extracts only text fields:
// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleFormsPdf)) {
// Extract data from PDF document
DocumentData data = parser.parseForm();
// Check if form extraction is supported
if (data == null) {
System.out.println("Form extraction isn't supported.");
return;
}
// Iterate over extracted data
for (int i = 0; i < data.getCount(); i++) {
System.out.print(data.get(i).getName() + ": ");
PageTextArea area = data.get(i).getPageArea() instanceof PageTextArea
? (PageTextArea) data.get(i).getPageArea()
: null;
System.out.println(area == null ? "Not a template field" : area.getText());
}
}
index
- The zero-based index of the field.FieldData
class.public List<FieldData> getFieldsByName(String fieldName)
fieldName
.
Find fields by a field name:
// Print prices
System.out.println("Prices:");
for (FieldData field : data.getFieldsByName("Price")) {
PageTextArea area = field.getPageArea() instanceof PageTextArea
? (PageTextArea) field.getPageArea()
: null;
System.out.println(area == null ? "Not a template field" : area.getText());
}
FieldData
class represents field data. Depending on the field PageArea
property can contain
any of the inheritors of PageArea
class. For example, Parser.parseForm()
method extracts only text fields.
fieldName
- The name of the field.FieldData
objects; empty collection if no field data is found.