use of com.google.cloud.documentai.v1beta2.ProcessDocumentRequest in project java-document-ai by googleapis.
the class ParseTableBeta method parseTable.
public static void parseTable(String projectId, String location, String inputGcsUri) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (DocumentUnderstandingServiceClient client = DocumentUnderstandingServiceClient.create()) {
// Configure the request for processing the PDF
String parent = String.format("projects/%s/locations/%s", projectId, location);
TableBoundHint tableBoundHints = TableBoundHint.newBuilder().setBoundingBox(// Each vertice coordinate must be a number between 0 and 1
BoundingPoly.newBuilder().addNormalizedVertices(NormalizedVertex.newBuilder().setX(0).setX(0).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(1).setX(0).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(1).setX(1).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(0).setX(1).build()).build()).setPageNumber(1).build();
TableExtractionParams params = TableExtractionParams.newBuilder().setEnabled(true).addTableBoundHints(tableBoundHints).build();
GcsSource uri = GcsSource.newBuilder().setUri(inputGcsUri).build();
// mime_type can be application/pdf, image/tiff,
// and image/gif, or application/json
InputConfig config = InputConfig.newBuilder().setGcsSource(uri).setMimeType("application/pdf").build();
ProcessDocumentRequest request = ProcessDocumentRequest.newBuilder().setParent(parent).setTableExtractionParams(params).setInputConfig(config).build();
// Recognizes text entities in the PDF document
Document response = client.processDocument(request);
// Get all of the document text as one big string
String text = response.getText();
// Get the first table in the document
if (response.getPagesCount() > 0) {
Document.Page page1 = response.getPages(0);
if (page1.getTablesCount() > 0) {
Document.Page.Table table = page1.getTables(0);
System.out.println("Results from first table processed:");
List<Document.Page.DetectedLanguage> detectedLangs = page1.getDetectedLanguagesList();
String langCode = detectedLangs.size() > 0 ? detectedLangs.get(0).getLanguageCode() : "NOT_FOUND";
System.out.printf("First detected language: : %s", langCode);
Document.Page.Table.TableRow headerRow = table.getHeaderRows(0);
System.out.println("Header row:");
for (Document.Page.Table.TableCell tableCell : headerRow.getCellsList()) {
if (tableCell.getLayout().getTextAnchor().getTextSegmentsList() != null) {
// Extract shards from the text field
// First shard in document doesn't have startIndex property
System.out.printf("\t%s", getText(tableCell.getLayout(), text));
}
}
}
}
}
}
use of com.google.cloud.documentai.v1beta2.ProcessDocumentRequest in project java-document-ai by googleapis.
the class QuickStart method quickStart.
public static void quickStart(String projectId, String location, String inputGcsUri) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (DocumentUnderstandingServiceClient client = DocumentUnderstandingServiceClient.create()) {
// Configure the request for processing a single document
String parent = String.format("projects/%s/locations/%s", projectId, location);
GcsSource uri = GcsSource.newBuilder().setUri(inputGcsUri).build();
// mime_type can be application/pdf, image/tiff,
// and image/gif, or application/json
InputConfig config = InputConfig.newBuilder().setGcsSource(uri).setMimeType("application/pdf").build();
ProcessDocumentRequest request = ProcessDocumentRequest.newBuilder().setParent(parent).setInputConfig(config).build();
// Recognizes text entities in the PDF document
Document response = client.processDocument(request);
// Get all of the document text as one big string
String text = response.getText();
// Process the output
for (Document.Entity entity : response.getEntitiesList()) {
System.out.printf("Entity text: %s\n", getText(entity, text));
System.out.printf("Entity type: %s\n", entity.getType());
System.out.printf("Entity mention text: %s\n", entity.getMentionText());
}
}
}
Aggregations