DocumentData
Inheritance: java.lang.Object
All Implemented Interfaces: java.lang.Iterable
public class DocumentData implements Iterable<FieldData>
Represents data of the document. It consists of 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.
Constructors
Constructor | Description |
---|---|
DocumentData(Iterable |
Initializes a new instance of the FieldData class. |
Methods
Method | Description |
---|---|
getCount() | Gets the total number of the fields data. |
get(int index) | Gets the field data by an index. |
getFieldsByName(String fieldName) | Returns the collection of field data where the name is equal to fieldName . |
iterator() | Returns an iterator over the elements in this list in proper sequence. |
DocumentData(Iterable fields)
public DocumentData(Iterable<FieldData> fields)
Initializes a new instance of the FieldData class.
Parameters:
Parameter | Type | Description |
---|---|---|
fields | java.lang.Iterable<com.groupdocs.parser.data.FieldData> | The collection of fields data. |
getCount()
public int getCount()
Gets the total number of the fields data.
Returns: int - An integer value that represents the total number of the fields data.
get(int index)
public FieldData get(int index)
Gets the field data by an 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());
}
}
Parameters:
Parameter | Type | Description |
---|---|---|
index | int | The zero-based index of the field. |
Returns: FieldData - An instance of FieldData class.
getFieldsByName(String fieldName)
public List<FieldData> getFieldsByName(String fieldName)
Returns the collection of field data where the name is equal to 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.
Parameters:
Parameter | Type | Description |
---|---|---|
fieldName | java.lang.String | The name of the field. |
Returns: java.util.List<com.groupdocs.parser.data.FieldData> - A collection of FieldData objects; empty collection if no field data is found.
iterator()
public Iterator<FieldData> iterator()
Returns an iterator over the elements in this list in proper sequence.
Returns: java.util.Iterator<com.groupdocs.parser.data.FieldData> - an iterator over the elements in this list in proper sequence.