This sample shows how to extract structured fields from a PDF using a custom template via GroupDocs.Parser.
Java
// Open the PDF using the Parser class
try (Parser parser = new Parser("input.pdf"))
{
// Apply the parsing template to extract defined data
DocumentData data = parser.parseByTemplate(GetTemplate());
// Check if the template-based extraction is available
if (data == null) {
return;
}
// Work with the extracted data fields
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());
}
}
private static Template GetTemplate()
{
// Define detector settings for extracting the 'Details' section
TemplateTableParameters detailsTableParameters =
new TemplateTableParameters(new Rectangle(new Point(35, 320), new Size(530, 55)), null);
TemplateItem[] templateItems = new TemplateItem[]
{
new TemplateTable(detailsTableParameters, "details", null)
};
Template template = new Template(java.util.Arrays.asList(templateItems));
return template;
}